AntlrWorks & language grammar errors

放肆的年华 提交于 2020-01-05 07:50:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!