1

假设我有一个语法,它有一个解析器规则来匹配几个特定字符串之一。在语法中为每个特定字符串制定备用解析器规则,或者保持解析器规则通用并在访问者子类中解码字符串是正确的做法吗?

4

1 回答 1

2

If the specific strings are meaningful (e.g. a keyword in a DSL) it sounds like you want Tokens. Whatever rules you have in the grammar can reference the Tokens you created.

Generally, it's better to have your grammar do as much of the parser work as possible, rather than overly generalizing and having to write a bunch of extra code.

See the following: http://www.antlr.org/wiki/display/ANTLR4/Grammar+Structure

于 2013-10-31T17:03:22.040 回答