I wrote parser in Prolog. I haven't finished yet. It is a part of code. The next step is killing all whitespace in string.
parse(Source, Tree) :- kill_whitespace(Source, CleanInput), % remove whitespaces
actual_parse(CleanInput, Tree).
actual_parse(CleanInput, Tree):- phrase(expr(Tree),CleanInput).
expr(Ast) --> term(Ast1), expr_(Ast1,Ast).
expr_(Acc,Ast) --> " + ", !, term(Ast2), expr_(plus(Acc,Ast2), Ast).
expr_(Acc,Ast) --> " - ", !, term(Ast2), expr_(minus(Acc,Ast2), Ast).
expr_(Acc,Acc) --> [].
term(Ast) --> factor(Ast1), term_(Ast1,Ast).
term_(Acc,Ast) --> " * ", !, factor(Ast2), term_(mul(Acc,Ast2),Ast).
term_(Acc,Ast) --> " ** ", !, factor(Ast2), term_(pol(Acc,Ast2),Ast).
term_(Acc,Acc) --> [].
factor(Ast) --> "(", !, expr(Ast), ")".
factor(D)--> [X], { X >= 48 , X=<57 , D is X-48 }.
factor(id(N,E)) --> "x", factor(N), ":=", expr(E), ";".
For example:
?- parse("x2:=4",T).
T = id(2, 4)
True! But, when I write:
?- parse("x2 := 4",T).
false.
It must be true as well and it should be a filter: kill_whitespace(Source, CleanInput)
.
Different solutions are inefficient. How can I do that?