如果我为具有一系列语句的类C语言编写语法,那么定义语法的最常用方法是什么?用于语句顺序的BNF语法
我的想法是做这样的事情:
<program> ::= <statement>
<statement> ::= <statement-head><statement-tail>
<statement-head> ::= <if-statement> | <var-declaration> | <assignment> | <whatever>
<statement-tail> ::= ; | ;<statement>
但感觉有点麻烦给我。我也考虑使
<program> ::= <statement>*
或
<statement> ::= <statement-head> ; | <sequence>
<sequence> ::= <statement> <statement>
类型作品。
有没有一个标准或可接受的方式来做到这一点。我希望我的AST尽可能地干净。
我喜欢这个。我唯一的问题是我不确定大多数解析器生成器(我使用TinyPG)是否支持/ *空* /生产。我的印象是它不那么犹太教。 – captncraig 2009-11-04 18:02:50
没关系。在看完C语法后,艾登发布了它可以是: :: = | –
captncraig
2009-11-04 18:05:44
我一直在使用我的Bison语法:-)实际上,我从O'Reilly的书籍* lex和yacc *中获得它,作者系统地使用/ * empty * /来强调空的规则真的存在为了某件事。如果您的解析器生成器不支持这种评论,那么您当然可以放弃它。 – 2009-11-04 18:13:51