Belgacem
Belgacem

Reputation: 193

no viable alternative at input ANTLR4?

I am creating my own language with ANTLR 4 and I would like to create a rule to define variables with their types for example.

string = "string"
boolean = true
integer = 123
double = 12.3
string = string // reference to variable

Here is my grammar.

// lexer grammar
fragment LETTER : [A-Za-z];
fragment DIGIT : [0-9];
ID : LETTER+; 
STRING : '"' ( ~ '"' )* '"' ; 
BOOLEAN: ( 'true' | 'fase');
INTEGER: DIGIT+ ;
DOUBLE: DIGIT+ ('.' DIGIT+)*;
// parser grammar
program: main EOF;
main: study ;
study : studyBlock (assignVariableBlock)? ;
simpleAssign: name = ID  '=' value = (STRING | BOOLEAN | INTEGER | BOOLEAN | ID);
listAssign: name = ID  '=' value = listString #listStringAssign;
assign: simpleAssign       #simpleVariableAssign
      | listAssign         #listOfVariableAssign
      ;
assignVariableBlock: assign+;
key: name = ID '[' value = STRING ']';
listString: '{' STRING (',' STRING)* '}';
studyParameters: (| ( simpleAssign (',' simpleAssign)*) );
studyBlock: 'study' '(' studyParameters  ')' ;

When I test with this example ANTLR displays the following error

study(timestamp = "10:30", region = "region", businessDate="2020-03-05", processType="ID")
bool = true
region = "region"
region = region
line 4:7 no viable alternative at input 'bool=true'
line 6:9 no viable alternative at input 'region=region'

How can I fix that?.

Upvotes: 2

Views: 716

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170278

When I test your grammar and start at the program rule for the given input, I get the following parse tree (without any errors or warnings):

enter image description here

You either don't start with the correct parser rule, or are testing an old parser and need to generate new classes from your grammar.

Upvotes: 2

Related Questions