也許這會做你想要什麼:
-module(parse).
-export([parse1/0, parse1/1, parse2/0, parse2/1]).
parse1() ->
parse([bexp], [2,6,3,7,3,2,4,6,3,2,4,4,99], fun get_sym_list1/1).
parse1(Input) ->
parse([bexp], Input, fun get_sym_list1/1).
parse2() ->
parse([bexp0], [2,6,3,7,3,2,4,6,3,2,4,4,99], fun get_sym_list2/1).
parse2(Input) ->
parse([bexp0], Input, fun get_sym_list2/1).
parse([], [], _) ->
true;
parse([], _, _) ->
false;
parse([X | TX], [X | TY], Fun) ->
io:format("+ Current:~w\tTokens:~w Input:~w~n", [X, TX, TY]),
parse(TX, TY, Fun);
parse([X | TX], Input, Fun) ->
io:format(" Current:~w\tTokens:~w Input:~w~n", [X, TX, Input]),
case lists:member(X, get_terminal_list()) of
true -> false;
false -> lists:any(fun(T) -> parse(T ++ TX, Input, Fun) end, Fun(X))
end.
get_terminal_list() ->
[1,2,3,4,5,6,7,8,9,10,11,12,99].
get_sym_list1(Prod) ->
case Prod of
bexp -> [[bexp1],[bterm],[bterm,bexp2]];
bexp1 -> [[99]];
bexp2 -> [[5,bterm,bexp2],[6,bterm,bexp2]];
bterm -> [[bfactor],[7,bterm]];
bfactor -> [[3,bexp,4],[bconst],[2,10,1],[2,12,1],[2,11,1],[2]];
bconst -> [[8],[9]]
end.
get_sym_list2(Prod) ->
case Prod of
bexp0 -> [[bterm,bexp1]];
bexp -> [[bterm,bexp1]];
bexp1 -> [[5,u1],[6,bexp,bexp1],[99]];
bterm -> [[u1,4],[2],[8],[9],[2,10,1],[2,12,1],[2,11,1],[7,bterm]];
u1 -> [[3,bexp]]
end.
但是,它看起來像語法或輸入列表是不正確的,因爲據我可以看到舊的和新的語法都不能解析輸入。它似乎是工作的罰款,因爲它會解析這樣一個輸入:
41> parse:parse2([2,6,8,5,3,bterm,5,3,9,99,99]).
Current:bexp0 Tokens:[] Input:[2,6,8,5,3,bterm,5,3,9,99,99]
Current:bterm Tokens:[bexp1] Input:[2,6,8,5,3,bterm,5,3,9,99,99]
Current:u1 Tokens:[4,bexp1] Input:[2,6,8,5,3,bterm,5,3,9,99,99]
Current:3 Tokens:[bexp,4,bexp1] Input:[2,6,8,5,3,bterm,5,3,9,99,99]
+ Current:2 Tokens:[bexp1] Input:[6,8,5,3,bterm,5,3,9,99,99]
Current:bexp1 Tokens:[] Input:[6,8,5,3,bterm,5,3,9,99,99]
Current:5 Tokens:[u1] Input:[6,8,5,3,bterm,5,3,9,99,99]
+ Current:6 Tokens:[bexp,bexp1] Input:[8,5,3,bterm,5,3,9,99,99]
Current:bexp Tokens:[bexp1] Input:[8,5,3,bterm,5,3,9,99,99]
Current:bterm Tokens:[bexp1,bexp1] Input:[8,5,3,bterm,5,3,9,99,99]
Current:u1 Tokens:[4,bexp1,bexp1] Input:[8,5,3,bterm,5,3,9,99,99]
Current:3 Tokens:[bexp,4,bexp1,bexp1] Input:[8,5,3,bterm,5,3,9,99,99]
Current:2 Tokens:[bexp1,bexp1] Input:[8,5,3,bterm,5,3,9,99,99]
+ Current:8 Tokens:[bexp1,bexp1] Input:[5,3,bterm,5,3,9,99,99]
Current:bexp1 Tokens:[bexp1] Input:[5,3,bterm,5,3,9,99,99]
+ Current:5 Tokens:[u1,bexp1] Input:[3,bterm,5,3,9,99,99]
Current:u1 Tokens:[bexp1] Input:[3,bterm,5,3,9,99,99]
+ Current:3 Tokens:[bexp,bexp1] Input:[bterm,5,3,9,99,99]
Current:bexp Tokens:[bexp1] Input:[bterm,5,3,9,99,99]
+ Current:bterm Tokens:[bexp1,bexp1] Input:[5,3,9,99,99]
Current:bexp1 Tokens:[bexp1] Input:[5,3,9,99,99]
+ Current:5 Tokens:[u1,bexp1] Input:[3,9,99,99]
Current:u1 Tokens:[bexp1] Input:[3,9,99,99]
+ Current:3 Tokens:[bexp,bexp1] Input:[9,99,99]
Current:bexp Tokens:[bexp1] Input:[9,99,99]
Current:bterm Tokens:[bexp1,bexp1] Input:[9,99,99]
Current:u1 Tokens:[4,bexp1,bexp1] Input:[9,99,99]
Current:3 Tokens:[bexp,4,bexp1,bexp1] Input:[9,99,99]
Current:2 Tokens:[bexp1,bexp1] Input:[9,99,99]
Current:8 Tokens:[bexp1,bexp1] Input:[9,99,99]
+ Current:9 Tokens:[bexp1,bexp1] Input:[99,99]
Current:bexp1 Tokens:[bexp1] Input:[99,99]
Current:5 Tokens:[u1,bexp1] Input:[99,99]
Current:6 Tokens:[bexp,bexp1,bexp1] Input:[99,99]
+ Current:99 Tokens:[bexp1] Input:[99]
Current:bexp1 Tokens:[] Input:[99]
Current:5 Tokens:[u1] Input:[99]
Current:6 Tokens:[bexp,bexp1] Input:[99]
+ Current:99 Tokens:[] Input:[]
true
BTW true
意味着輸入已被解析並false
,它也沒有。
Upvote,因爲它在erlang中。 – Harry
你可以添加一個你正試圖解析的輸入的例子嗎?什麼不起作用?你是否得到一個錯誤或算法似乎沒有產生你期望的結果(在這種情況下請說出你的期望)還是別的? – Amiramix
@Amiramix向問題添加樣本輸入。目前我認爲它進入一個循環,或者一旦它達到一個列表的產量就出現錯誤。 – Scy