/* Calc benchmark in Picat. From http://www.jekejeke.ch/idatab/doclet/prod/en/docs/05_run/15_stdy/06_bench/09_programs/11_calc/01_calc.p.html This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => calc. go2 => calc. /** * Prolog code for the DCG calculator. * * DCGs are a simple case of Colmerauers Metamorphosis grammars * an were presented to the community in the 1980 paper * Definite Clause Grammars for Language Analysis, by F.C.N. * Pereira and D.H.D. Warren. * * The DCG calculator is usually considered Prolog homework. * * Copyright 2011-2012, XLOG Technologies GmbH, Switzerland * Jekejeke Prolog 0.9.0 (a fast and small prolog interpreter) */ expr(Z) --> "-", !, term(X), {Y is -X}, expr_rest(Y,Z). expr(Y) --> term(X), expr_rest(X,Y). expr_rest(X,T) --> "+", !, term(Y), {Z is X+Y}, expr_rest(Z,T). expr_rest(X,T) --> "-", !, term(Y), {Z is X-Y}, expr_rest(Z,T). expr_rest(X,X) --> []. term(Y) --> factor(X), term_rest(X,Y). term_rest(X,T) --> "*", !, factor(Y), {Z is X*Y}, factor_rest(Z,T). term_rest(X,T) --> "/", !, factor(Y), {Z is X/Y}, factor_rest(Z,T). term_rest(X,X) --> []. factor(X) --> "(", !, expr(X), ")". factor(Y) --> digit(X), factor_rest(X,Y). factor_rest(X,T) --> digit(Y), !, {Z is X*10+Y}, factor_rest(Z,T). factor_rest(X,X) --> []. % digit(Y) --> [X], {48= [X], {ascii_digit(X), Y is X.to_int}. calc :- % phrase(expr(_),"-12+34*56+78"). expr(_,"-12+34*56+78",[]). calc :- % phrase(expr(_),"(-12+34)*(56+78)"). expr(_,"(-12+34)*(56+78)",[]).