Pattern-Action Pairs

A pattern-action pair consists of

  • a regular expression (the pattern) that describes the set of matching lexemes.

  • an action being either a ‘token sending’, ‘mode transition’, or something specified in a source code fragment.

If a regular expression is followed by the operator => tokens sending and mode transitions can be specified in a convenient manner. More sophisticated actions can be specified using source code in curly brackets {} directly following the regular expression. The two syntactic means are displayed in the code fragment below.

mode EXAMPLE {
    // pattern:   action:
    //
    [a-z]+     => QUEX_TKN_IDENTIFIER(Lexeme);           // => token sending
    \"         => GOTO(STRING);                          // => mode transition
    [0-9]+     { self.send_string(TKN_NUMBER, Lexeme); } // { source code }
}

The first two pattern-action pairs show examples of => syntax. A token with id QUEX_TKN_IDENTIFER is sent upon detection of a sequence of lower-case letters. The STRING mode is entered upon detection of a double quote. The last pattern-action pair is an example for direct source code specification. When a sequence of digits is detected, a token with token-id TKN_NUMBER is sent with the lexer’s member function .send_string(). The following sections elaborate further on how to specify pattern-action pairs.