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?
I usually place a 'skip' non terminal where space can occurs. Such skip usually discards comments as well as any other 'uninteresting' text.
To keep as simpler as possible:
% discard any number of spaces
s --> "" ; " ", s.
I prefer a short name, to keep the grammar clean. To discard newlines etc.. as well:
s --> "" ; (" ";"\t";"\n";"\r"), s.
A 'style' note: instead of
parse(Source, Tree) :-
expr(Tree, Source, []).
you could consider
parse(Source, Tree) :-
phrase(expr(Tree), Source).