antlr3 - how to solve this simple antlr recusive issue -


i reading url(and trying copy) , failed...(great article on antlr too)...

https://supportweb.cs.bham.ac.uk/docs/tutorials/docsystem/build/tutorials/antlr/antlr.html

my solution before added parenthesis stuff

whereclause: expression -> ^(where_clause expression); expression: orexpr; orexpr: andexpr (or^ andexpr)*; andexpr: primaryexpr (and^ primaryexpr)*; primaryexpr: parameterexpr | inexpr | compexpr; 

my solution failed due infinite recursion (but thought lparen^ , rparen! supposed solve that???)....

whereclause: where^ (expression | orexpr); expression: lparen^ orexpr rparen!; orexpr: andexpr (or^ andexpr)*; andexpr: primaryexpr (and^ primaryexpr)*; primaryexpr: parameterexpr | inexpr | compexpr | expression; 

notice primaryexpr @ bottom has expression tacked on has lparen , rparen can orexpr or expression(ie. first expression can use or not use parens).

i sure simple issue dang typo keep staring @ hours or something. thanks, dean

i reading url(and trying copy) , failed...(great article on antlr too)...

note article explains antlr v2, has significant different syntax v3. better decent antlr v3 tutorial here: https://stackoverflow.com/questions/278480/antlr-tutorials

my solution failed due infinite recursion (but thought lparen^ , rparen! supposed solve that???)....

it have, if expression after while. however, orexpr causing problem in case (if remove it, that recursion error go away).

a parenthesized expression has highest precedence, , should therefor placed in primaryexpr rule, this:

grammar t; options { output=ast; } parse : whereclause eof!; whereclause : where^ expression; expression : orexpr; orexpr : andexpr (or^ andexpr)*; andexpr : primaryexpr (and^ primaryexpr)*; primaryexpr : bool | number | '('! expression ')'!; bool : true | false; true : 'true'; false : 'false'; : 'where'; lparen : '('; rparen : ')'; or : '||'; , : '&&'; number : '0'..'9'+ ('.' '0'..'9'*)?; space : (' ' | '\t' | '\r' | '\n')+ {skip();}; 

now both input "where true || false" , "where (true || false)" parsed in following ast:

enter image description here


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -