3
votes

I am using ANTLR to create lexer/parser. Expressions can have the format like this

if(a==1 || b==2 or c==3 && d==4 and e==5)

I have grammar for supporting && and || like this -

AND :   '&&'
OR  :   '||'

Need to know what changes will be required for supporting keyword "and" and "or".

2

2 Answers

3
votes

Just list "and" and "or" as alternatives in the definition of AND and OR

AND : '&&' | 'and'
OR  : '||' | 'or'
3
votes

Just add them as alternatives after the existing operators:

AND : '&&' | 'and';
OR  : '||' | 'or';

Be sure to add these two rules above a possible IDENTIFIER rule that could possibly match "and" and "or" as well. By adding them above an IDENTIFIER, the rules AND and OR get precedence over IDENTIFIER.