用于表示逗号分隔列表的语法表达式
根据我的经验,正式语法通常以类似于以下的形式表达逗号分隔的列表:
foo_list -> foo ("," foo)*
有哪些替代方法可以避免两次提及 foo
?尽管这个人为的示例可能看起来很无辜,但我遇到的是非平凡的表达式而不是 foo
。例如:
foo_list -> ( ( bar | baz | cat ) ) ( "," ( bar | baz | cat ) )*
Based on my experience, formal grammars typically express comma-delimited lists in a form similar to this:
foo_list -> foo ("," foo)*
What alternatives are there to avoid mentioning foo
twice? Although this contrived example may seem innocent enough, I am encountering non-trivial expressions instead of foo
. For example:
foo_list -> ( ( bar | baz | cat ) ) ( "," ( bar | baz | cat ) )*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我记得我曾经使用过一个(专有的)解析器生成器,它会将这个产生式写为
“是”,就像那样。上面的实际元字符是有争议的,但我认为一般方法是可以接受的。
在编写另一个解析器生成器时,我考虑了一段时间类似的东西,但为了保持模型简单而放弃了它。
当然,语法图可以很好地表示它,而不会出现不必要的重复:
I remember a (proprietary) parser generator that I once worked with, which would have this production written as
Yes, exactly like that. The actual metacharacters above are disputable, but I deem the general approach acceptable.
When writing another parser generator, I considered something alike for a while, but dropped it in favor of keeping the model simple.
A syntax diagram of course can nicely represent it without the unwanted repetition:
在我的实验过程中,此语法显示了一些潜力:
...
标记引用前面的表达式(在本例中为( bar | baz | cat )
)。这不是一个完美的解决方案,但我将其放在那里供讨论。
During my experimentation, this syntax showed some potential:
The
...
token refers to the preceding expression (in this case,( bar | baz | cat )
).This is not a perfect solution, but I am putting it out there for discussion.