1
votes

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`**
1
did you use ANTLR Works to pass the same input and get the visual AST?Zinov
I Believe I am using antlr4. Though because of my lack of knowledge in this field I am not sure if I somehow using antlworksMarios Jim
try to install the ANTLR Works and that will give you the visual AST and where is failing your input in your grammar and where a specific token match with a certain rule. That is for debugging purposesZinov

1 Answers

5
votes

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.