I'm very new to ANTLR4
and am trying to build my own language. So my grammar starts at
program: <EOF> | statement | functionDef | statement program | functionDef program;
and my statement
is
statement: selectionStatement | compoundStatement | ...;
and
selectionStatement
: If LeftParen expression RightParen compoundStatement (Else compoundStatement)?
| Switch LeftParen expression RightParen compoundStatement
;
compoundStatement
: LeftBrace statement* RightBrace;
Now the problem is, that when I test a piece of code against selectionStatement
or statement
it passes the test, but when I test it against program
it fails to recognize. Can anyone help me on this? Thank you very much
edit: the code I use to test is the following:
if (x == 2) {}
It passes the test against selectionStatement
and statement
but fails at program
. It appears that program
only accepts if...else
if (x == 2) {} else {}
Edit 2: The error message I received was
<unknown>: Incorrect error: no viable alternative at input 'if(x==2){}'