What is wrong with this ANTLR Grammar? Conditional statement nested parenthesis -


i've been tasked writing prototype of team's dsl in java, thought try out using antlr. i'm having problems 'expression' , 'condition' rules.

the dsl defined keep close current spec possible.

grammar mydsl; // snippet of whole language, should give  // decent view of issue.  entry     : condition eof     ;  condition     : lparen condition rparen     | atomic_condition     | not condition     | condition , condition     | condition or condition     ;  atomic_condition     : expression compare_operator expression     | expression (is null | not null)     | identifier     | boolean     ;  compare_operator     : equals     | nequals     | gt | lt     | gtequals | ltequals     ;  expression     : lparen expression rparen     | atomic_expression     | prefix expression     | expression (multiply | divide) expression      | expression (add | subtract) expression     | expression concatenate expression     ;  atomic_expression     :  substr lparen expression comma expression (comma expression)? rparen     | identifier     | integer     ;  identifier     : word     ;  // function names substr: 'substr'; // control chars lparen : '('; rparen : ')'; comma  : ','; // literals , identifiers fragment digit : [0-9] ; integer: digit+; fragment letter : [a-za-z@$#]; fragment character : digit | letter | '_'; word: letter character*; boolean: 'true' | 'false'; // arithmetic operators multiply : '*'; divide   : '/'; add      : '+'; subtract : '-'; prefix: add| subtract ; // string operators concatenate : '||'; // comparison operators equals   : '=='; nequals  : '<>'; gtequals : '>='; ltequals : '<='; gt       : '>'; lt       : '<'; // logical operators not : 'not'; , : 'and'; or  : 'or'; // keywords  : 'is'; null: 'null'; // whitespace blank: [ \t\n\r]+ -> channel(hidden) ; 

the phrase i'm testing

(foo == 115 , (substr(bar,2,1) == 1 or substr(bar,4,1) == 1)) 

however breaking on nested parenthesis, matching first ( first ) instead of outermost (see below). in antlr3 solved semantic predicates seems antlr4 supposed have fixed need those.

antlrworks parse tree

i'd keep condition , expression rules separate if @ possible. have been able work when merged in single expression rule (based on examples here , elsewhere) current dsl spec has them different , i'm trying reduce possible differences in behaviour.
can point out how can working while maintaining separate rule conditions' andexpressions`? many thanks!

the grammar seems fine me.

there's 1 thing going wrong in lexer: word token defined before various keywords/operators causing precedence on them. place word rule @ end of lexer rules (or @ least after last keywords word match).


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -