Antlr4 How to Capture Evaluation Order of expressions -
i have following grammar
grammar expr; prog: expr; expr: lp expr rp | expr lp expr rp | lp expr rp expr | expr '*' '{' ',' expr | expr op=not expr | expr op=and expr | expr op=or expr | id ; newline:'\r'? '\n' ; not: '~'; and: '&'; or: '|'; lp : '('; rp : ')'; // lexer/terminal rules start upper case letter id : ( 'a'..'z' | 'a'..'z' | '0'..'9' | ' ' | ('+'|'-'|'*'|'/'|'_') | '=' | '~' | '{' | '}' | ',' )+ ; ws : [ \t\n]+ -> skip ;
i want extract nodes of expression being evaluated , need in order in evaluated. expression such 1*{a42,a53,a16,a3}&(a26|a41)&(a51=p&a2=f|a7=c) evaluated in following order
a26 | a41 a51 & a2=f a51 & a2f | af=c 1*{a42,a53,a16,a3}&(a26|a41) 1*{a42,a53,a16,a3}&(a26|a41)&(a51=p&a2=f|a7=c)
the main interest figuring out order in expression evaluated.
how go this. tried writing visitor implementation not know how extract order.
each time recurse through 'expr' rule, antlr generate new context object. order of contexts provide information want. each context contains non-null terminalnode id represent evaluation success. so, in simplest case, walk parse tree , in 'entereveryrule'watch id. in 'entereveryrule' , 'exiteveryrule' watch highest inflection points (ascending switching descending , vice versa) between successes distinguish between valid , unused conjunction symbols.
fwiw, first 3 rule alts ambiguous, fourth unlikely expect, , 5-7 can combined simplify analysis.
Comments
Post a Comment