Raven
Raven

Reputation: 3526

How to interpret ANTLR error-messages?

When I want to create a parser out of my grammar ANTLR gives me an error about a non-LL(*) conform content. I do understand that this means that there must be a point where the parser can't distinguish between two or more rules but what I don't understand is what ANTLR means by saying that this ambiguity is reachable from (for example) 'alts 1,2'.
Is there a way to interpret these numbers to actually find the particular input which causes this ambiguity so that I know what I have to fix? Because I find it really difficult to look at my grammar again and find out what causes this issue...

Best regards Raven

Upvotes: 0

Views: 92

Answers (2)

Raven
Raven

Reputation: 3526

If there is someone who still has some difficulties with these error-messages (like me) then this method could be quite useful for you.
It describes a way to visualize the syntax tree (quite similar to the build-in Xtext-syntaxGraph but this one highlights the actual part Antlr is complaining about.)

Best regards Raven

Upvotes: 0

Jiri Tousek
Jiri Tousek

Reputation: 12450

The message will state the line in the grammar file where the problem occured. 'alts 1,2' means ANTLR found ways two ways to match the input which differ in how to continue at given location, so it cannot decide which way to take. Examples of what is meant by alternatives 1 and 2:

rule1:
  ( choice1  // alternative 1
  | choice2  // alternative 2
  )
  ;

rule2:
  choice?  // alternative 1: go into choice rule
           // alternative 2: skip over choice rule
  ;

rule3:
  choice*  // alternative 1: go into choice rule (either first time or repeatedly)
           // alternative 2: skip over choice rule (either skip it or don't repeat anymore)
  ;

rule4:
  choice+  // alternative 1: repeat once more
           // alternative 2: don't repeat anymore
  ;

Upvotes: 1

Related Questions