ANTLR 解决非 LL(*) 问题和句法谓词
考虑解析器中的以下规则:
expression
: IDENTIFIER
| (...)
| procedure_call // e.g. (foo 1 2 3)
| macro_use // e.g. (xyz (some datum))
;
procedure_call
: '(' expression expression* ')'
;
macro_use
: '(' IDENTIFIER datum* ')'
;
表达式规则中的procedure_call
// Note that any string that parses as an <expression> will also parse as a <datum>.
datum
: simple_datum
| compound_datum
;
simple_datum
: BOOLEAN
| NUMBER
| CHARACTER
| STRING
| IDENTIFIER
;
compound_datum
: list
| vector
;
list
: '(' (datum+ ( '.' datum)?)? ')'
| ABBREV_PREFIX datum
;
fragment ABBREV_PREFIX
: ('\'' | '`' | ',' | ',@')
;
vector
: '#(' datum* ')'
;
和 Macro_rule 替代项会生成非 LL(*) 结构错误。我可以看到问题,因为 (IDENTIFIER)
将解析为两者。但即使当我用 + 而不是 * 定义两者时,它也会生成错误,即使上面的示例不应该再解析。
我想出了句法谓词的用法,但我不知道如何使用它们来实现这里的技巧。
像
expression
: IDENTIFIER
| (...)
| (procedure_call)=>procedure_call // e.g. (foo 1 2 3)
| macro_use // e.g. (xyz (some datum))
;
或
expression
: IDENTIFIER
| (...)
| ('(' IDENTIFIER expression)=>procedure_call // e.g. (foo 1 2 3)
| macro_use // e.g. (xyz (some datum))
;
这样的规则也不起作用,因为除了第一个规则之外,没有任何规则会匹配任何内容。有没有合适的方法来解决这个问题?
consider following rules in the parser:
expression
: IDENTIFIER
| (...)
| procedure_call // e.g. (foo 1 2 3)
| macro_use // e.g. (xyz (some datum))
;
procedure_call
: '(' expression expression* ')'
;
macro_use
: '(' IDENTIFIER datum* ')'
;
and
// Note that any string that parses as an <expression> will also parse as a <datum>.
datum
: simple_datum
| compound_datum
;
simple_datum
: BOOLEAN
| NUMBER
| CHARACTER
| STRING
| IDENTIFIER
;
compound_datum
: list
| vector
;
list
: '(' (datum+ ( '.' datum)?)? ')'
| ABBREV_PREFIX datum
;
fragment ABBREV_PREFIX
: ('\'' | '`' | ',' | ',@')
;
vector
: '#(' datum* ')'
;
the procedure_call and macro_rule alternative in the expression rule generate an non-LL(*) structure error. I can see the problem, since (IDENTIFIER)
will parse as both. but even when i define both with + instead of *, it generates the error, even though above example shouldn't be parsing anymore.
i came up with the usage of syntactic predicates, but i can't figure out how to use them to do the trick here.
something like
expression
: IDENTIFIER
| (...)
| (procedure_call)=>procedure_call // e.g. (foo 1 2 3)
| macro_use // e.g. (xyz (some datum))
;
or
expression
: IDENTIFIER
| (...)
| ('(' IDENTIFIER expression)=>procedure_call // e.g. (foo 1 2 3)
| macro_use // e.g. (xyz (some datum))
;
doesnt work either, since none but the first rule will match anything. is there a proper way to solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了 R5RS 的 JavaCC 语法,我用它(很快!)编写了一个 ANTLR 等效项
:可以使用以下类进行测试:
并生成词法分析器 &解析器,编译所有 Java 源文件并运行主类,执行以下操作:
控制台上没有打印任何内容,这意味着解析器(和词法分析器)没有发现所提供的源有任何错误。
请注意,我没有单元测试,仅测试了 Main 类中的单个方案源。如果您发现 ANTLR 语法中有错误,我很乐意听到这些错误,以便我可以修复语法。在适当的时候,我可能会将语法提交到官方 ANTLR Wiki。
I found a JavaCC grammar of R5RS which I used to (quickly!) write an ANTLR equivalent:
which can be tested with the following class:
and to generate a lexer & parser, compile all Java source files and run the main class, do:
The fact that nothing is being printed on the console means the parser (and lexer) didn't find any errors with the provided source.
Note that I have no Unit tests and have only tested the single Scheme source inside the
Main
class. If you find errors in the ANTLR grammar, I'd appreciate to hear about them so I can fix the grammar. In due time, I'll probably commit the grammar to the official ANTLR Wiki.