我写了一个语法来解析速度,它在“if elseif else”处有冲突
弹性文件:
%{
#include<stdio.h>
#include<string.h>
#include "context.h"
#include "bool.h"
#include "vtl4.tab.h"
%}
INT ([0-9]*)
%%
{INT} {return INTEGER;}
">" {return yytext[0];}
"(" {return yytext[0];}
")" {return yytext[0];}
"in" {return IN;}
"#foreach" {return FOREACH;}
"#end" {return END;}
"#if" {return IF;}
"#else" {return ELSE;}
"#elseif" {return ELSEIF;}
[^ \t] {yylval.string = yytext;return CONTENT;}
[ \t] {}
%%
野牛文件:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "parser.h"
#include "context.h"
#include "vtl4.tab.h"
extern FILE * yyin;
extern FILE * yyout;
extern int yylex();
extern int yywrap();
%}
%union {
struct simpleNode *ast;
double d;
int i;
bool b;
char* string;
struct symbol *sym;
}
%token <string> CONTENT NAME
%token IF ELSEIF ELSE END HASH DOLLAR PARENTHESIS PARENTHESIS_CLOSE LOGIC_EQUAL NEWLINE INTEGER GL
%token FOREACH IN
%type <ast> stmts stmt
%nonassoc ELSEIF
%nonassoc ELSE
%%
stmts
: stmt{}
| stmts stmt{}
;
stmt:CONTENT {}
|directive {printf("stmt ---directive\n");}
|INTEGER {}
;
directive:FOREACH '(' exp ')' stmts END {printf("directive ---foreach\n");}
|if {printf("directive ---if\n");}
;
if:IF '(' cond ')' stmts END {printf("if\n");}
|IF '(' cond ')' stmts ELSE stmts END {printf("if else end\n");}
|IF '(' cond ')' stmts elseif END {printf("if elseif end\n");}
|IF '(' cond ')' stmts elseif ELSE stmts END {printf("if elseif ... else end\n");}
;
elseif:ELSEIF '(' cond ')' stmts {printf("one elseif\n");}
|elseif elseif {printf("Mul elseif\n");}
;
cond:INTEGER '>' INTEGER {printf("cond\n");}
exp:INTEGER IN INTEGER {printf("exp\n");}
%%
int main(){
FILE *src;
src = fopen("test.vm","r");
yyin = src;
yyparse();
fclose(src);
return 1;
}
int yywrap(){
return 1;
}
输出文件说:
State 34 conflicts: 1 shift/reduce
...
state 34
12 elseif: . ELSEIF '(' cond ')' stmts
13 | . elseif elseif [ELSEIF, ELSE, END]
13 | elseif . elseif [ELSEIF, ELSE, END]
13 | elseif elseif . [ELSEIF, ELSE, END]
ELSEIF shift, and go to state 25
ELSEIF [reduce using rule 13 (elseif)]
$default reduce using rule 13 (elseif)
elseif go to state 34
我查了一些资料,并为ELSE和ELSEIF添加了优先级,但没有解决。请帮帮我!可能我不是很了解优先级机制