I'm writing a PHP like interpreter and have some problems with shift/reduce and reduce/reduce conflicts. may someone help me to understand shift/reduce and reduce/reduce conflicts.
I have to write and interpreter that ship/echo non-valuables and valuate expressions starting with the "magic" @ character e.g. @if(cond) ... @end;. So "if" must be echoed while @if(cond) should be interpreted
Problem: scriptlang.y contains 21 shift/reduce conflicts and 2 reduce/reduce conflicts.
%union {
char* sval;
}
%token <sval> IDENTIFIER
%token <sval> RBRACKET
%token <sval> LBRACKET
%token <sval> KWSWITCH
%token <sval> KWIF
%token <sval> MAGICESC
%token MAGIC
%token ENDSTM
%type <sval> filechar
%start script
%%
script:
commands
;
commands:
/* empty */
| command
| commands command
;
command:
filechar {
analyser_echo($1,"filechar",analyser_canEcho);
}
| magic_command {}
;
filechar:
IDENTIFIER
| LBRACKET
| RBRACKET
| KWSWITCH
| KWIF
| MAGICESC
;
magic_command:
MAGIC valuation
| MAGIC alternative
;
valuation:
LBRACKET IDENTIFIER RBRACKET {
fprintf(yyout, "<val>");
}
;
alternative:
switch_alternative
| if_alternative
;
switch_alternative:
switch_block end_stm
;
switch_block:
switch_stm
| switch_stm commands
;
switch_stm:
KWSWITCH LBRACKET IDENTIFIER RBRACKET {}
;
if_alternative:
if_block end_stm
;
if_block:
if_stm
| if_stm commands
;
if_stm:
KWIF LBRACKET IDENTIFIER RBRACKET {}
;
end_stm:
ENDSTM
;
%%
flex file content:
"@@" {
yylval.sval = "@";
return MAGICESC;
}
"@" {
return MAGIC;
}
"(" {
yylval.sval = yytext;
return LBRACKET;
}
")" {
yylval.sval = yytext;
return RBRACKET;
}
"@end;" {
return ENDSTM;
}
"if" {
yylval.sval = yytext;
return KWIF;
}
"switch" {
yylval.sval = yytext;
return KWSWITCH;
}
[a-zA-Z][_a-zA-Z0-9]* {
yylval.sval = yytext;
return IDENTIFIER;
}
\n|. {
if(analyser_canEcho>0){
ECHO;
}
}
%%