0
votes

I have a simple grammar defined below using Antlr 3:

grammar i;

@header {
package com.data;
}

null        : 'null';
true        : 'true';
false       : 'false';
value       : true | false | null | STRING | INTEGER | FLOAT;
elements    : (value (',' value)*)MINIMUMDIGIT;
STRING  : ('a'..'z'|'A'..'Z')+;
MINIMUMDIGIT    : ('0'..'9');
INTEGER : '0'..'9'+;
FLOAT       : INTEGER'.'INTEGER;
WS      : (' '|'\t'|'\f'|'\n'|'\r')+ {skip();}; // handle white space between keywords

When I try to do the interpretation of the elements in ANTLRWorks, the elements displays correct. However, I also a a NoViableAltException.

I have tried:

true

It displays true and then a NoViableAltException

I have true, true, true, true and it displays true,true,true,true and then a NoViableAltException.

Can you help where I am going wrong? Looking at many similar posts, but unable to find the solution to this problem. The Exception is shown in the graph during the interpretation and no Exception shown on the Console.

EDIT:

The user has to enter a single value, but it can follow multiple values separted by a comma each time. According to Antlr, Antlr allows you define a optional (?), zero or more (*) or one or more (+). There is no such thing as only one. So the MINIMUMDIGIT is used to control the only one.

Example:

insert      : 'INSERT INTO table' 'VALUES' '('elements')'';';

What I am trying to achive is when the insert is executed, elements has to have atleast one value or it could have multiple values. According to the syntax defined above, it allows zero or more.

As soon as I change the elements to:

elements : (value (',' value)*)+;

this does not work. When I enter comma, the comma displays. It also accepts values with no commas. The comma is necessary. That's why I introduced the MINIMUMDIGIT.

1
Well, I need this minimum digit. Antlr does allows you to define zero, one or more or optional, but doesn't allow you to define one only. This is why I did it like this. - user1646481
so is there no solution at all? Really not sure what to do. Have looked online. Not any solution. - user1646481
I had a look at the following: stackoverflow.com/questions/9282087/…. I am using ANTLRWorks. No Exceptions in Console, only in graph. I think the interpreter does not work? Is this correct? - user1646481
"I really need this MINIUMUMDIGIT." What's the role that you want MINIMUMDIGIT to play? Could you add a handful of examples to show how it's role is necessary in solving your problem, e.g. showing what it should allow and disallow in your input? (In your question, please focus more on explaining the problem you're trying to solve and less on the solution that you're using.) - user1201210
Please see edit with example above. - user1646481

1 Answers

0
votes

According to Antlr, Antlr allows you define a optional (?), zero or more (*) or one or more (+).

Agreed.

There is no such thing as only one.

Not true. A referenced rule by default matches only once. So if elements is defined as elements: value;, then elements matches on one and only one value.

Likewise, if elements is elements : (value (',' value)*); (no trailing ?, +, or *), it will match each of the following lines:

value
value , value
value , value , value

It will not match any of the following lines:

(empty input)
value value
, value
value , , 

I think what you need is just elements: (value (',' value)*);.