January 2013
Intermediate to advanced
328 pages
8h 5m
English
In this section, we’re going to solve the same problem again, but this time using predicates in the lexer instead of the parser. The idea is that predicates in the lexer activate and deactivate tokens rather than phrases in the language. Our approach will be to deactivate enum as a keyword and match it as a regular identifier in pre--Java 5 mode. In Java 5 mode, we want to separate enum out as its own keyword token. This simplifies the parser considerably because it can match an identifier just by referencing the usual ID token, rather than an id rule.
| | stat: ID '=' expr ';' {System.out.println($ID.text+"="+$expr.text);} ; |
| | |
| | expr: ID |
| | | INT |
| | ; |
The lexer sends an
Read now
Unlock full access