antlr4-lab
antlr4-lab copied to clipboard
Simple grammar error
Hi,
Just wanted to test something simple at first. I'd like to test a program that only start with the word 'fsm' and it out an error saying "<missing 'fsm'>"
the grammar to be tested:
grammar Fsm;
fsm
: 'fsm' ID
;
ID
: Letter
;
fragment
Letter
: '\u0024' /* $ */ |
'\u0041'..'\u005a' /* A-Z */ |
'\u005f' /* _ */ |
'\u0061'..'\u007a' /* a-z */ |
'\u00c0'..'\u00d6' |
'\u00d8'..'\u00f6' |
'\u00f8'..'\u00ff' |
'\u0100'..'\u1fff' |
'\u3040'..'\u318f' |
'\u3300'..'\u337f' |
'\u3400'..'\u3d2d' |
'\u4e00'..'\u9fff' |
'\uf900'..'\ufaff'
;
The simple test is:
fsm Test
- Make sure you select the "Lexer" tab and clear out the contents. You did not, which is why you get the error message "<missing 'fsm'>".
- Make sure you have a whitespace rule that shunts spaces to the off-channel.
WS: [ \t\n] -> channel(HIDDEN);
- Make sure you use the
+
-operator in the definition of ID.ID: Letter+ ;
notID: Letter ;
- Make sure you use an EOF-terminated start rule in order to avoid the parser not reading all the input on failure.
fsm: 'fsm' ID EOF;
. If you do all three changes I listed before this fourth suggestion, and givefsm Test foobar
as input, the parse will succeed, but not fail onfoobar
.
Your grammar should looke like this:
grammar Fsm;
fsm : 'fsm' ID ;
ID: Letter+ ;
WS: [ \t\n] -> channel(HIDDEN);
fragment
Letter
: '\u0024' /* $ */ |
'\u0041'..'\u005a' /* A-Z */ |
'\u005f' /* _ */ |
'\u0061'..'\u007a' /* a-z */ |
'\u00c0'..'\u00d6' |
'\u00d8'..'\u00f6' |
'\u00f8'..'\u00ff' |
'\u0100'..'\u1fff' |
'\u3040'..'\u318f' |
'\u3300'..'\u337f' |
'\u3400'..'\u3d2d' |
'\u4e00'..'\u9fff' |
'\uf900'..'\ufaff'
;