问题
Working on a game project that involves a scripting language that I want to interpret into a virtual machine code that can be executed directly. I've included the grammar below. All the lexer rules are displaying properly in the syntax diagram, but when I click on the bodies of any of the parser rules, I get "Cannot display rule "X" because start state is not found" for a given parser rule X.
I'm not really sure why ANTLR is complaining about not having a start state. The grammar should plainly start from codeline, which isn't referenced by any other parser rule. Also, the box in the upper right is green, indicating there are no syntax errors.
I combed through some of the other message board posts, as well as many of the grammars provided in the ANTLRv3 sample grammar download, but none of them have any special code that indicates to ANTLR which one of the parser rules is the start state. I feel like something simple is broken, but I'm at an impasse as to what exactly that is.
Any advice or assistance would be much appreciated! Even if it's just along the lines of "go read this".
grammar RobotWarsGrammar;
EQUAL
options {
paraphrase = "=";
}
: '='
;
回答1:
Assuming you're using ANTLR 3.x.
All that:
options {
paraphrase = ...;
}
stuff is (AFAIK) old ANTLR 2 syntax -- try removing it.
Also, that !
in your comment
rule:
comment
: !(DIGIT | LETTER | SPACE)*
;
is a tree-rewrite-operator (see the cheat sheet). But that only works when you have:
options {
output=AST;
}
in your grammar (which you don't). So, remove that !
from your comment
rule as well. Unless you want to match the literal !
, in which case you need to wrap single quotes around it:
comment
: '!' (DIGIT | LETTER | SPACE)*
;
来源:https://stackoverflow.com/questions/4342949/antlrworks-language-grammar-errors