简体   繁体   中英

Change antlr lexer parser for supporting “and” and “or” keywords

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".

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM