0
我寫了這段代碼來創建一個pascal解析器。當野牛分析它時,儘管它是一個左遞歸和模糊的語法,它並沒有表現出任何衝突。這是代碼BISON:沒有移位/減少衝突
%{
#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;
}
我試圖曖昧和左遞歸語法,野牛顯示這些衝突的其他的例子,但不與本一段代碼。 並謝謝你!