I have the following grammar:
grammar Demo;
program: command
IDENTIFIER
;
command:
| 'add'
| 'remove'
;
IDENTIFIER: [a-zA-Z][a-zA-Z0-9]* ;
WHITESPACE: [ \t\n\r]+ -> skip;
Now I can entered something like "add foo" and I get the right result. But he also accept "foo" only. I thought that the parser will throw an exception, because the value have to start with one of the commands? Is there an option to fix the problem? Or have I a fallacy?
As indicated in the comments by Seelenvirtuose, the problem is that your rule for command
allows an empty command, it should be:
command:
'add'
| 'remove'
;
The pipe symbol ( |
) is a separator between alternatives, in your original code it separated an empty production from 'add'
.
However, as is, the grammar would still allow to only match foo
which is because you don't have an explicit EOF token in your program
rule (similar to the $
option in a regular expression). Without the EOF token the parser happily matches what it can and ignores the rest. So, if that is no the desired behavior always end your main rule with EOF:
program: command IDENTIFIER EOF;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.