antlr3 - ANTLR v3 grammar for boolean/conditional expression -
i'm taking first stab @ creating grammar expressions like:
(foo = bar or (bar = "bar" , baz = 45.43)) , test = true
my grammar far looks like:
grammar filter; tokens { true = 'true'; false = 'false'; , = 'and'; or = 'or'; lt = '<'; gt = '>'; eq = '='; neq = '!='; pathsep = '/'; lbrack = '['; rbrack = ']'; lparen = '('; rparen = ')'; } expression : or_expression eof; or_expression : and_expression (or or_expression)*; and_expression : term (and term)*; term : atom ( operator atom)? | lparen expression rparen; atom : id | int | float | string | true | false; operator : lt | gt | eq | neq; int : '0'..'9'+; float : ('0'..'9')+ '.' ('0'..'9')*; string : '"' ('a'..'z'|'a'..'z'|'_'|' ')* '"'; id : ('a'..'z'|'a'..'z'|'_') ('a'..'z'|'a'..'z'|'0'..'9'|'_')*;
but in antlrworks 1.4.3, parse tree:
but life of me can't figure out wrong grammar. token missing here?
many in advance.
edit: clarify atom ( operator atom)?
alternative in atom
production, should perhaps mention atoms should able free-standing without comparison atom. e.g. a or b
valid expression.
i'm answering own question here. found 2 problems grammar. first easy spot; had put eof
@ end of top-level rule:
expression : or_expression eof;
the eof
missing token. solution remove eof
expression
rule, , instead introduce rule above it:
filter: expression eof;
the second problem or_expression
rule should be:
or_expression : and_expression (or and_expression)*;
and not
or_expression : and_expression (or or_expression)*;
the full corrected grammar is:
grammar filter; tokens { true = 'true'; false = 'false'; , = 'and'; or = 'or'; lt = '<'; gt = '>'; eq = '='; neq = '!='; pathsep = '/'; lbrack = '['; rbrack = ']'; lparen = '('; rparen = ')'; } filter: expression eof; expression : or_expression; or_expression : and_expression (or and_expression)*; and_expression : term (and term)*; term : atom (operator atom)? | lparen expression rparen; atom : id | int | float | string | true | false; operator : lt | gt | eq | neq; int : '0'..'9'+; float : ('0'..'9')+ '.' ('0'..'9')*; string : '"' ('a'..'z'|'a'..'z'|'_'|' ')* '"'; id : ('a'..'z'|'a'..'z'|'_') ('a'..'z'|'a'..'z'|'0'..'9'|'_')*;
and resulting parse tree is:
Comments
Post a Comment