BISON:无移位/减少冲突 [英] BISON : no shift/reduce conflicts
问题描述
我写了这段代码来创建一个pascal解析器.当野牛分析它时,尽管它是一个左递归和模棱两可的语法,但它没有显示任何冲突.这是代码
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;
}
我尝试了歧义和左递归语法的其他示例,bison显示了这些冲突,但没有这段代码. 谢谢!!!
I tried other examples of ambiguous and left recursive grammars, bison displays those conflicts but not with this piece of code. And thank you!!!
推荐答案
左键回退只是LL解析的一个问题,而bison是LR(实际上是LALR),因此左递归不是问题-它不会引起任何问题冲突或其他问题.
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?
这篇关于BISON:无移位/减少冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!