Marios Jim
Marios Jim

Reputation: 25

How to fix extraneous input ' ' expecting, in antlr4

Hello when running antlr4 with the following input i get the following error image showing problem

[image showing problem[1]

I have been trying to fix it by doing some changes here and there but it seems it only works if I write every component of whileLoop in a new line.

Could you please tell me what i am missing here and why the problem persits?

grammar AM;

COMMENTS :

'{'~[\n|\r]*'}' -> skip

;

body : ('BODY' ' '*) anything | 'BODY' 'BEGIN' anything* 'END' ;


anything : whileLoop | write ;

write : 'WRITE' '(' '"' sentance '"' ')' ;

read : 'READ' '(' '"' sentance '"' ')' ;

whileLoop : 'WHILE' expression 'DO' ;

block : 'BODY' anything 'END';

expression : 'TRUE'|'FALSE' ;

test : ID? {System.out.println("Done");};


logicalOperators : '<' | '>' | '<>' | '<=' | '>=' | '=' ;

numberExpressionS : (NUMBER numberExpression)* ;

numberExpression : ('-' | '/' | '*' | '+' | '%') NUMBER ;

sentance : (ID)* {System.out.println("Sentance");}; 

WS : [ \t\r\n]+ -> skip ;
NUMBER : [0-9]+ ;
ID : [a-zA-Z0-9]* ;



**`strong text`**

Upvotes: 2

Views: 11361

Answers (1)

CoronA
CoronA

Reputation: 8075

Your lexer rules produce conflicts:

body : ('BODY' ' '*) anything | 'BODY' 'BEGIN' anything* 'END' ;

vs

WS : [ \t\r\n]+ -> skip ;

The critical section is the ' '*. This defines an implicit lexer token. It matches spaces and it is defined above of WS. So any sequence of spaces is not handled as WS but as implicit token.

If I am right putting tabs between the components of whileloop will work, also putting more than one space between them should work. You should simply remove ' '*, since whitespace is to be skipped anyway.

Upvotes: 5

Related Questions