Reputation: 1317
I am new to Antlr. I am defining a grammar for my company using Antlr 3.
Below is my grammar:
grammar Grammar;
@header {
package com.grammar;
}
true : 'true';
false : 'false';
null : 'null';
value : true | false | null | STRING | NUMBER;
query : (STATEMENT+) | STATEMENT?;
INSERT : 'INSERT INTO' TABLE 'VALUES' '('ELEMENTS')'';';
STATEMENT : INSERT;
STRING : ('a'..'z'|'A'..'Z')+;
INTEGER : '0'..'9'+;
ELEMENTS : value | value ',' ELEMENTS;
When I try to generate the code using ANTLRWorks, I get the following Exception:
error (211): Grammar.g.1:8: [fatal] rule Tokens has non-LL(*) decision due to recursive rule invocations reacable from alts 18,24. Resolve by left-factoring or using syntactic predicates or using backtrack=true option
I had a look at the following website:
http://www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtracking+from+your+grammar
Any suggestions what can be done? I couldn't find much online.
When I add the following:
grammar Grammar;
@options {
backtrack=true;
}
I get only one Exception:
error(100): Grammar.g:3:2: syntax error: antlr: NoViableAltException(55@[])
[12:03:20] error(100): Grammar.g:3:2: syntax error: antlr: MissingTokenException (inserted [@-1,0:0='<missing ACTION>',<50>,3:1] at options {)
[12:03:20] error(100): Grammar.g:3:2: syntax error: antlr: org.antlr.runtime.EarlyExitException
[12:03:20] error(150): grammar file Grammar.g has no rules
[12:03:20] error(100): Grammar.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(0!=3)
EDIT:
I have added value and ELEMENTS. When using ELEMENTS inside the graph in ANTLRWorks, the value cannot be seen. I can only see:
',' ELEMENTS
instead of:
value | value ',' ELEMENTS
Could this be the cause of the problem for the Tokens Exception? Is this actually allowed? What is the solution?
Upvotes: 1
Views: 951
Reputation: 13535
try
query : (STATEMENT)*;
a couple of advices:
avoid rules like
value | value ',' ELEMENTS
where both alternative starts with the same term. Move that term "out of brackets":
value (',' value ) *
all terms must be defined (NUMBER is not defined in your grammar)
only one rule can define term which is not referenced from other rules
Upvotes: 1