I intended to use bison to parse some scripting language, in this language I can write code like the following:
a = input()
b = a + 1
function myfunc
a = input()
b = a + 1
end function
I found that the block
a = input()
b = a + 1
which appear both in and out of the function definition can be reduced by the same rule stmts
, so I write code like the following
%{
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <stdarg.h>
#include <tuple>
using namespace std;
%}
%debug
%token CRLF EXP FUNCTIONBEGIN FUNCTIONEND
%start program
%%
stmt : EXP
|
stmts : stmt CRLF stmts
| stmt
function : FUNCTIONBEGIN CRLF stmts CRLF FUNCTIONEND
unit : function
| stmts
program : unit
| unit CRLF program
%%
Of course this code can't work due to one shift/reduce conflict
State 3
3 stmts: stmt . CRLF stmts
4 | stmt .
CRLF shift, and go to state 9
CRLF [reduce using rule 4 (stmts)]
$default reduce using rule 4 (stmts)
I thought this conflict is due to both my program
rule and stmts
rule using the same terminal CRLF
as a "binary operator", so I can't solve this conflict by set priority to operators.
Maybe I can merge program
rule and stmts
rule together by somehow adding another two rules to stmt
stmts : function CRLF stmts
| function
However I thought this method(whether it can practically work) is not very beautiful, so I ask if there's some other solutions