I have a simple grammar using Antlr 3:
grammar i;
@header {
package com.data;
}
null : 'null';
value : null | STRING;
elements : value? | (value ',' value)+;
STRING : ('a'..'z'|'A'..'Z')+;
WS : (' '|'\t'|'\f'|'\n'|'\r')+ {skip();}; // handle white space between keywords
What I am trying to achive is I have a value. So a value can be optional or value follows a comma follwed by another value. So for example:
value could be true or value could be true, true, true, true.
When I do the interpretation the following works:
true
or
true , true
When I trying true, true, true the tree is displayed with true, true but shows a NoViableAltException.
I have also tried:
elements: value? | (value ',' value)*;
but this doesn’t work either.
Any ideas where I am going wrong?
EDIT:
insert : 'INSERT INTO TABLE' 'VALUES' '('elements')'';';
The
elementsrule is written such that it derives only zero or one value, or any number of pairs that are separated by a comma, with no comma between the pairs.Apparently what you want is this:
This allows for nothing at all (due to the
?operator), or a single value followed by zero or more (*) occurrences of a comma and a value.