Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR grammar: parser- and lexer literals

What's the difference between this grammar:

...
if_statement : 'if' condition 'then' statement 'else' statement 'end_if';
... 

and this:

...
if_statement : IF condition THEN statement ELSE statement END_IF;
...

IF : 'if';
THEN: 'then';
ELSE: 'else';
END_IF: 'end_if';
....

?

If there is any difference, as this impacts on performance ... Thanks

like image 706
BB. Avatar asked Feb 04 '23 05:02

BB.


1 Answers

In addition to Will's answer, it's best to define your lexer tokens explicitly (in your lexer grammar). In case you're mixing them in your parser grammar, it's not always clear in what order the tokens are tokenized by the lexer. When defining them explicitly, they're always tokenized in the order they've been put in the lexer grammar (from top to bottom).

like image 107
Bart Kiers Avatar answered Mar 21 '23 06:03

Bart Kiers