|
|
@ -6,12 +6,12 @@ |
|
|
|
]). |
|
|
|
|
|
|
|
main(Args) -> |
|
|
|
[SNFile] = Args, |
|
|
|
[SNFile, WriteDir] = Args, |
|
|
|
case file:open(SNFile, [read, raw, binary, {read_ahead, 65536}, {'encoding', 'utf8'}]) of |
|
|
|
{ok, IoDevice} -> |
|
|
|
{Goto, Output} = dealEveryLine(IoDevice, _Goto=#{0 => #{}}, _Output=#{}, _State=0), |
|
|
|
Failure = genFailure(Goto), |
|
|
|
genErl(Goto, Failure, Output); |
|
|
|
genErl(WriteDir, Goto, Failure, Output); |
|
|
|
_Err -> |
|
|
|
io:format("genAcs open the file:~p error ~p~n", [SNFile, _Err]) |
|
|
|
end. |
|
|
@ -45,10 +45,8 @@ genGotoOutput([BinStr |Tail], Goto, Output, MaxState) -> |
|
|
|
genGotoOutput([], Goto, Output, _MaxState) -> |
|
|
|
{Goto, Output}. |
|
|
|
|
|
|
|
addPattern(<<Word/utf8, Tail/binary>> = BinStr, Goto, State, MaxState) -> |
|
|
|
addPattern(<<Word/utf8, Tail/binary>>, Goto, State, MaxState) -> |
|
|
|
#{State := Node} = Goto, |
|
|
|
<<Word1/utf8, Tail2/binary>> = BinStr, |
|
|
|
io:format("IMY*********Word~p ~p ~p~n", [Word, Word1, BinStr]), |
|
|
|
case Node of |
|
|
|
#{Word := NextState} -> |
|
|
|
addPattern(Tail, Goto, NextState, MaxState); |
|
|
@ -109,8 +107,56 @@ findFailureNode(Word, State, FailureState, Goto, Failure) -> |
|
|
|
end |
|
|
|
end. |
|
|
|
|
|
|
|
genErl(Goto, Failure, Output) -> |
|
|
|
ok. |
|
|
|
genHead() -> |
|
|
|
<<"-module(acsTrees).\n\n-export([goto/1, failure/1, output/1]).\n\n">>. |
|
|
|
|
|
|
|
genGoto(Goto, StrAcc) -> |
|
|
|
Kvs = maps:to_list(Goto), |
|
|
|
SortKvs = lists:sort(Kvs), |
|
|
|
doGenGoto(SortKvs, StrAcc). |
|
|
|
|
|
|
|
doGenGoto([], StrAcc) -> |
|
|
|
StrAcc; |
|
|
|
doGenGoto([{K, V}], StrAcc) -> |
|
|
|
<<StrAcc/binary, "goto(", (integer_to_binary(K))/binary, ") -> ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ".\n\n">>; |
|
|
|
doGenGoto([{K, V} | SortKvs], StrAcc) -> |
|
|
|
NewStrAcc = <<StrAcc/binary, "goto(", (integer_to_binary(K))/binary, ") -> ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ";\n">>, |
|
|
|
doGenGoto(SortKvs, NewStrAcc). |
|
|
|
|
|
|
|
genFailure(Goto, StrAcc) -> |
|
|
|
Kvs = maps:to_list(Goto), |
|
|
|
SortKvs = lists:sort(Kvs), |
|
|
|
doGenFailure(SortKvs, StrAcc). |
|
|
|
|
|
|
|
doGenFailure([], StrAcc) -> |
|
|
|
StrAcc; |
|
|
|
doGenFailure([{K, V}], StrAcc) -> |
|
|
|
<<StrAcc/binary, "failure(", (integer_to_binary(K))/binary, ") -> ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ".\n\n">>; |
|
|
|
doGenFailure([{K, V} | SortKvs], StrAcc) -> |
|
|
|
NewStrAcc = <<StrAcc/binary, "failure(", (integer_to_binary(K))/binary, ") -> ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ";\n">>, |
|
|
|
doGenFailure(SortKvs, NewStrAcc). |
|
|
|
|
|
|
|
genOutput(Goto, StrAcc) -> |
|
|
|
Kvs = maps:to_list(Goto), |
|
|
|
SortKvs = lists:sort(Kvs), |
|
|
|
doGenOutput(SortKvs, StrAcc). |
|
|
|
|
|
|
|
doGenOutput([], StrAcc) -> |
|
|
|
StrAcc; |
|
|
|
doGenOutput([{K, V}], StrAcc) -> |
|
|
|
<<StrAcc/binary, "output(", (integer_to_binary(K))/binary, ") -> ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ".\n\n">>; |
|
|
|
doGenOutput([{K, V} | SortKvs], StrAcc) -> |
|
|
|
NewStrAcc = <<StrAcc/binary, "output(", (integer_to_binary(K))/binary, ") -> ", (eFmt:formatBin(<<"~w">>, [V]))/binary, ";\n">>, |
|
|
|
doGenOutput(SortKvs, NewStrAcc). |
|
|
|
|
|
|
|
|
|
|
|
genErl(WriteDir, Goto, Failure, Output) -> |
|
|
|
HeadStr = genHead(), |
|
|
|
GotoStr = genGoto(Goto, HeadStr), |
|
|
|
FailureStr = genFailure(Failure, GotoStr), |
|
|
|
OutputStr = genOutput(Output, FailureStr), |
|
|
|
FileName = filename:join([WriteDir, "acsTrees.erl"]), |
|
|
|
file:write_file(FileName, OutputStr). |
|
|
|
|
|
|
|
|
|
|
|
|