We are a couple of guys doing a University project, which led us to play around with ANTLR4. We are just trying to figure it out as we go, but have stumbled upon a issue we can’t seem to fix.
We were currently trying to figure out the "moveList" in the grammar, and we have been running a simple test.txt la = Path true ATTACK UP 5;.
Currently we are working with this .g4 file:
grammar hess;
program: line* EOF;
defineBoard: 'BOARD' '(' BOARDPOSITION ')';
line: statement | ifBlock | whileBlock | defineBoard;
statement: (assignment | functionCall) ';';
ifBlock: 'if' expression block ('else' elseIfBlock);
elseIfBlock: block | ifBlock;
whileBlock: 'while' expression block ('else' elseIfBlock);
assignment: IDENTIFIER '=' moveList | IDENTIFIER '=' expression;
functionCall: IDENTIFIER '(' (expression (',' expression))? ')';
expression:
constant # constantExpression
| IDENTIFIER # identifierExpression
| '(' expression ')' # parenthesizedExpression
| '!' expression # notExpression
| expression multOp expression # multiplicativeExpression
| expression addOp expression # additiveExpression
| expression compareOp expression # comparisonExpression
| expression boolOp expression # booleanExpression;
moveList: move | move moveTail;
moveTail: ',' move moveTail;
moveExtra:
INTEGER
| INTEGER direction INTEGER
| direction INTEGER;
move: Movetype COLLISION Attacktype direction moveExtra;
IDENTIFIER: [a-zA-Z][a-zA-Z0-9];
constant:
BOARDPOSITION
| INTEGER
| FLOAT
| LETTER
| STRING
| BOOL
| NULL;
BOARDPOSITION: LETTER INTEGER;
INTEGER: [1-9][0-9]* | [0];
FLOAT: [0-9]+ '.' [0-9]+;
STRING: ('"' ~'"'* '"') | ('\'' ~'\''* '\'');
LETTER: [a-zA-Z];
BOOL: 'true' | 'false';
COLLISION: BOOL;
NULL: 'null';
block: '{' line* '}';
WS: [ \t\r\n]+ -> skip;
multOp: '*' | '/' | '%';
addOp: '+' | '-';
boolOp: 'and' | 'or' | 'xor';
compareOp: '==' | '!=' | '>' | '<' | '>=' | '<=';
direction: 'UP' | 'LEFT' | 'RIGHT' | 'DOWN';
Movetype: 'Direct' | 'Path';
Attacktype: 'ATTACK' | 'MOVE' | 'ATKMOVE';
Our thought process, regarding the grammar, would be like so:
line -> statement -> assigment -> moveList
and then
"la" = IDENTIFIER
"Path" = Movetype
"ATTACK" = Attacktype
"UP" = Direction
"5" = INTEGER
We get the following Parse Tree when we debug/run the .g4 file: Parse Tree
As you can see in the Parse Tree it says unexpected values, for what in my eyes look like an expected value.
The problem is that your
COLLISIONrule is a lex rule, but thetruetoken in your input is already being tokenised as aBOOLtoken. If you rename theCOLLISIONrule in your grammar tocollision, like this:Then it becomes a parse rule, and the parsing works for your test input: