Quex - Example

Example

Quex follows the syntax of the classical tools lex and flex for the description of regular expressions. The example in the section Flex can be translated into Quex source code as follows:

header { #include // C++ version of 'stdlib.h' } define { digit letter } mode X : { "+" => QUEX_TKN_PLUS; "-" => QUEX_TKN_MINUS; "*" => QUEX_TKN_TIMES; "/" => QUEX_TKN_SLASH; "(" => QUEX_TKN_LPAREN; ")" => QUEX_TKN_RPAREN; ";" => QUEX_TKN_SEMICOLON; "," => QUEX_TKN_COMMA; "." => QUEX_TKN_PERIOD; ":=" => QUEX_TKN_BECOMES; "=" => QUEX_TKN_EQL; "<>" => QUEX_TKN_NEQ; "<" => QUEX_TKN_LSS; ">" => QUEX_TKN_GTR; "<=" => QUEX_TKN_LEQ; ">=" => QUEX_TKN_GEQ; "begin" => QUEX_TKN_BEGINSYM; "call" => QUEX_TKN_CALLSYM; "const" => QUEX_TKN_CONSTSYM; "do" => QUEX_TKN_DOSYM; "end" => QUEX_TKN_ENDSYM; "if" => QUEX_TKN_IFSYM; "odd" => QUEX_TKN_ODDSYM; "procedure" => QUEX_TKN_PROCSYM; "then" => QUEX_TKN_THENSYM; "var" => QUEX_TKN_VARSYM; "while" => QUEX_TKN_WHILESYM; {letter}({letter}|{digit})* => QUEX_TKN_IDENT(strdup(Lexeme)); {digit}+ => QUEX_TKN_NUMBER(atoi(Lexeme)); . => QUEX_TKN_UNKNOWN(Lexeme); }

The brief token senders via the "=>" operator set the token ID of a token object with the token ID which follows the operator. The arguments following inside brackets are used to set contents of the token object. Note, that skipping whitespace can be achieved via skippers which are optimized to pass specific character sets quickly (see the "" tag). For more sophisticated token actions C-code sections can be provided, such as

... {digit}+ { if( is_prime_number(Lexeme) ) ++prime_number_counter; if( is_fibonacci_number(Lexeme) ) ++fibonacci_number_counter; self.send(QUEX_TKN_NUMBER(atoi(Lexeme))); } ...

which might be used to do some statistics about the numbers which occur in analyzed code.

Read more about this topic:  Quex

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)