野牛:左递归语法没有冲突

时间:2016-03-28 14:43:14

标签: bison

我编写了这段代码来创建一个pascal解析器。当野牛对它进行分析时,尽管它是一个左递归和暧昧的语法,但它并没有表现出任何冲突。这是代码

    %{
#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;
}

我尝试了其他模糊和左递归语法的例子,野牛显示这些冲突但不是这段代码。 谢谢!!!

0 个答案:

没有答案