我打算用野牛解析一些腳本語言移減少衝突,在這種語言,我可以寫類似下面的代碼:我怎樣才能消除同一操作
a = input()
b = a + 1
function myfunc
a = input()
b = a + 1
end function
我發現,該塊
a = input()
b = a + 1
其中進出函數定義的同時出現可以通過相同的規則stmts
被減小,所以我寫如下代碼
%{
#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
%%
當然這個代碼不能工作,由於一個班次/減少衝突
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)
我認爲這是衝突由於我的兩個program
規則和stmts
規則中使用相同的終端CRLF
作爲「二元運算符」 ,所以我不能通過給操作員設置優先級來解決這個衝突。
也許我可以通過某種方式將另外兩個規則合併program
規則和stmts
規則一起指向stmt
stmts : function CRLF stmts
| function
但是我認爲這個方法(它是否能實際工作)是不是很漂亮,所以我問還有其他一些解決方案