[antlr-interest] Ambiguous grammar or Antlr?

Gustaf Johansson gustaf.j at gmail.com
Thu Jul 2 01:09:00 PDT 2009


I have a complex grammar (900 lines) that parses incorrectly sometimes.
The grammar is basically just an Antlr transformation of the ETSI
defined TTCN3 BNF.

Here is a snippet from my grammar (modified for simplicity):

  assignment: ref '=' exp ';'? ;
  ref: ('a' .. 'z' | '_') ('.' ('a' .. 'z' | '_'))* ;
  exp: addExp ('+' addExp)* ;
  ... "a lot of math exps of different precedences"
  unaryExp: ('-' | '+')? primary ;
  primary: opCall | value | '(' exp ')' ;
  opCall: "this is a quite complex rule"
  value: INT | ref ;

Now if i parse:
  v_some_var = v_some_other_var;

I get:
  line x:y no viable alternative at input ';'

I suppose its because it expects '(' following a opCall-name or
something, though im not sure.

If i change the definition of primary to:
  primary: value | opCall | '(' exp ')' ;

It parses correctly.
I have backtrack and memoize set to true.

I just dont understand why Antlr wont even try the second option in
the 'primary' rule, before reporting an error, this seems wrong to me.
Could someone please shed some light on this for me?

Best Regards Gustaf


More information about the antlr-interest mailing list