f1b417f1c3
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@812 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
24 lines
511 B
Plaintext
24 lines
511 B
Plaintext
|
|
:- object(calculator,
|
|
implements(parsep)).
|
|
|
|
|
|
parse(Expression, Value) :-
|
|
phrase(expr(Value), Expression).
|
|
|
|
|
|
expr(Z) --> term(X), "+", expr(Y), {Z is X + Y}.
|
|
expr(Z) --> term(X), "-", expr(Y), {Z is X - Y}.
|
|
expr(X) --> term(X).
|
|
|
|
term(Z) --> number(X), "*", term(Y), {Z is X * Y}.
|
|
term(Z) --> number(X), "/", term(Y), {Z is X / Y}.
|
|
term(Z) --> number(Z).
|
|
|
|
number(C) --> "+", number(C).
|
|
number(C) --> "-", number(X), {C is -X}.
|
|
number(X) --> [C], {0'0 =< C, C =< 0'9, X is C - 0'0}.
|
|
|
|
|
|
:- end_object.
|