BISON : no shift/reduce conflicts

╄→гoц情女王★ 提交于 2019-12-12 01:37:08

问题


I wrote this code to create a pascal parser . When the bison analyse it, it doesn't show any conflict despite it's a left recursive and ambigous grammar. this is the code

    %{
#include<stdio.h>
int yyparse();
int yylex();
int yyerror(char *s);

%}

%token  ID;
%token  VAR;
%token INT;
%token FUNC;
%token  PROC;
%token  BEGIN;
%token  END;
%token OPAFFECT;
%token OPREL;
%token  OPADD;
%token  OPMUL;
%token PROGRAM;
%token NB;
%token  IF;
%token  THEN;
%token ELSE;
%token  WHILE;
%token  DO;
%token NOT;
%token PO;
%token PF;
%token P;
%token PV;
%token DP;
%token V;
%token PLUS;
%token MINUS;
%%

program : PROGRAM ID PV declaration compoundinstruction P
declaration : vardeclaration subprogramsdec
vardeclaration : vardeclaration VAR idlist DP INT PV | /*epsilon*/
idlist : ID | idlist V ID
subprogramsdec : subprogramsdec subprograsdec PV | /*epsilon*/
subprograsdec : subprograsheader declaration compoundinstruction
subprograsheader : FUNC ID arguments DP INT PV | PROC ID arguments PV
arguments : PO parameterslist PF
parameterslist : parametre | parameterslist PV parametre
parametre : ID DP INT | VAR ID DP INT
compoundinstruction : BEGIN optinstruction END 
optinstruction : instructionslist | /*epsilon*/
instructionslist : instruction | instructionslist PV instruction
instruction : variable OPAFFECT expression | procedurecall | compoundinstruction | IF instruction THEN instruction ELSE instruction | WHILE expression DO instruction 
variable : ID
procedurecall : ID | ID PO expressionslist PF
expressionslist : expression | expressionslist V expression
expression : simpleexpression | simpleexpression OPREL simpleexpression
simpleexpression : term | sign term | simpleexpression OPADD term
term : factor | term OPMUL factor 
factor : ID | ID PO expressionslist PF | NB | PO expression PF |  NOT factor 
sign : PLUS | MINUS

%%
int yyerror(char *s) {
    printf("yyerror : %s\n",s);
    return 0;
}

int main(void) {
    yyparse();
    return 0;
}

I tried other examples of ambiguous and left recursive grammars, bison displays those conflicts but not with this piece of code. And thank you!!!


回答1:


Left recusion is only an issue for LL parsing, and bison is LR (actually LALR), so left recursion is not a problem -- it does not cause any conflicts or other issues.

The fact that your grammar does not have any shift/reduce conflicts means that it is not ambiguous. What makes you think that it is?



来源:https://stackoverflow.com/questions/36259487/bison-no-shift-reduce-conflicts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!