用于Scheme 准引号的 ANTLR 语法
考虑 EBNF 中的以下语法:
http://pauillac.inria.fr/cdrom_a_graver/www/bigloo /manual/r5rs-8.html
给出的准引用语法不是上下文无关的:
<quasiquotation> --> <quasiquotation 1>
<qq template 0> --> <expression>
<quasiquotation D> --> `<qq template D>
| (quasiquote <qq template D>)
<qq template D> --> <simple datum>
| <list qq template D>
| <vector qq template D>
| <unquotation D>
<list qq template D> --> (<qq template or splice D>*)
| (<qq template or splice D>+ . <qq template D>)
| '<qq template D>
| <quasiquotation D+1>
<vector qq template D> --> #(<qq template or splice D>*)
<unquotation D> --> ,<qq template D-1>
| (unquote <qq template D-1>)
<qq template or splice D> --> <qq template D>
| <splicing unquotation D>
<splicing unquotation D> --> ,@<qq template D-1>
| (unquote-splicing <qq template D-1>)
是否可以在 ANTLR 中为此定义一个语法,尽管它是专为上下文无关语法设计?
Considering following grammar in EBNF:
http://pauillac.inria.fr/cdrom_a_graver/www/bigloo/manual/r5rs-8.html
The quasiquotations grammar given there is not context free:
<quasiquotation> --> <quasiquotation 1>
<qq template 0> --> <expression>
<quasiquotation D> --> `<qq template D>
| (quasiquote <qq template D>)
<qq template D> --> <simple datum>
| <list qq template D>
| <vector qq template D>
| <unquotation D>
<list qq template D> --> (<qq template or splice D>*)
| (<qq template or splice D>+ . <qq template D>)
| '<qq template D>
| <quasiquotation D+1>
<vector qq template D> --> #(<qq template or splice D>*)
<unquotation D> --> ,<qq template D-1>
| (unquote <qq template D-1>)
<qq template or splice D> --> <qq template D>
| <splicing unquotation D>
<splicing unquotation D> --> ,@<qq template D-1>
| (unquote-splicing <qq template D-1>)
Is it possible to define a grammar for this in ANTLR, although it is designed for context-free grammars?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在其后添加
[int d]
向词法分析器和解析器规则添加参数,如下所示:foo[int d] : ... ;
。将参数1
传递给规则foo
就像parse : foo[1];
一样简单:当然,它不必是
>int
,但可以是任何类型。因此,在您的情况下,请执行以下操作:
请注意,还有一些语法谓词
( ... )=>
来解释语法中的一些歧义。有关 ANTLR 中 R5RS 规范的完整版本,请参阅 这个答案。
You can add parameters to both lexer- and parser rules by adding
[int d]
after it, like this:foo[int d] : ... ;
. Passing the parameter1
to rulefoo
is as simple asparse : foo[1];
:Of course, it need not be an
int
, but can be any type.So, in your case, do something like this:
Note that there are also some syntactic predicates,
( ... )=>
, to account for some ambiguities in the grammar.For a complete version of the R5RS spec in ANTLR, see this answer.