我必须将这个EBNF转换为bison:
<compound-statement> ::= begin [ <statement> ( ; <statement> )*] end
<statement> ::=
| <assignment>
| <if-statement>
| <while-statement>
| <proc-func-call>
| <compound-statement>当我转换赋值、if、while语句和proc_func_时,bison中没有错误。但是,当我在bison中键入以下内容时,将转换复合语句:
compound_statement : BEGINKEY state ENDKEY ;
state : | statement stm ;
stm : | BQUESTIONMARK statement stm ;有一个reduce/reduce错误。
谁能给我解释一下,为什么会有一个reduce/reduce错误,因为它对我来说没有任何意义。我真的很感激。
提前谢谢。
发布于 2012-06-20 22:00:44
因此,您有一种Pascal式语言,其中分号是语句分隔符,而不是结束符。
我假设BQUESTIONMARK是分号(";")的标记。
我认为,使用一种需要第一条语句的生产,然后使用另一种左递归生产来提供可选的附加语句,您会做得最好。
我可能误解了什么,但是您的语法允许state和stm一样为epsilon (null),我认为这就是您的reduce/reduce错误的根源。
我会这样处理这个问题:
compound_statement : BEGINKEY first_statement statements ENDKEY
| BEGINKEY first_statement ENDKEY
;
first_statement : statement ;
statement : assignment
| if_statement
| while_statement
| proc_func_call
| compound_statement
;
statements : statements statement_with_semi
| statement_with_semi
;
statement_with_semi : BQUESTIONMARK statement ;https://stackoverflow.com/questions/11111876
复制相似问题