解析器移位/减少减少/减少冲突

时间:2014-03-06 04:21:12

标签: parsing yacc lex

我给了一个用LexYacc编写的解析器,它恰好是含糊不清的。编译时,我得到几个shift / reduce和reduce / reduce冲突。我似乎无法在网上找到任何可以帮助我理解这意味着什么以及需要做些什么来解决它的问题。我希望有人可以帮我清理一下。

解析器:

%{
  #include <stdio.h>
  int yylex();
%}

%token ID INT_CONST REAL_CONST LPAREN RPAREN INT REAL VOID COMMA LCBRAC RCBRAC ASSIGN      SEMICOLON IF ELSE ADD RETURN SUBT MULT DIV FOR UNTIL 

%%                   /* beginning of rules section */

program : functionDeclarationS; 
functionDeclarationS : functionDeclaration | functionDeclaration functionDeclarationS;
functionDeclaration : typeSpecifier ID  LPAREN params RPAREN functionBody;
typeSpecifier : INT | VOID;
params : paramList | VOID;
paramList : param COMMA paramList | param;
param : typeSpecifier ID;
functionBody : LCBRAC localDeclarations statementS RCBRAC;
localDeclarations : varDeclarations SEMICOLON localDeclarations | ;
varDeclarations : INT varList;
varList : ID | ID COMMA varList;
statementS : statement statementS | ;
statement : assignment SEMICOLON | expression  SEMICOLON | ifStmt | return SEMICOLON;
assignment : ID ASSIGN expression;
expression : expression ADD expression | LPAREN expression RPAREN | expression;
expression : integer | real | ID | functionCall;
integer : sign INT_CONST;
real : sign REAL_CONST;
sign : ADD | SUBT| ;
functionCall : ID LPAREN argumentsList RPAREN;
argumentsList : expression | expression COMMA argumentsList | ;
ifStmt : IF LPAREN expression RPAREN block;
block : LCBRAC statementS RCBRAC;
return : RETURN expression ;

%%
int main()
{
  return(yyparse());
}

yyerror(s)
char *s;
{
  fprintf(stderr, "%s\n",s);
}

int yywrap()
{
  return(1);
}

冲突和警告信息:

冲突:11 shift/reduce7 reduce/reduce parser.y:30.17-26: 警告:由于冲突,规则在解析器中无效:argumentsList:expression **parser.y:33.10-26:** 警告:由于冲突,规则在解析器中无效:return:RETURN expression

1 个答案:

答案 0 :(得分:1)

规则expression: expression没有意义,并且使语法模糊不清,并且还导致解析器具有无限循环,因为argumentList将尝试无限扩展它(这就是为什么你得到关于{{1}的消息的原因无用 - 它被argumentsList: expression取代,所以永远无法减少。)