summaryrefslogtreecommitdiff
path: root/prolog/problems/dcg/expr_3
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/dcg/expr_3')
-rw-r--r--prolog/problems/dcg/expr_3/common.py24
-rw-r--r--prolog/problems/dcg/expr_3/en.py14
2 files changed, 38 insertions, 0 deletions
diff --git a/prolog/problems/dcg/expr_3/common.py b/prolog/problems/dcg/expr_3/common.py
new file mode 100644
index 0000000..daa5b31
--- /dev/null
+++ b/prolog/problems/dcg/expr_3/common.py
@@ -0,0 +1,24 @@
+id = 171
+group = 'dcg'
+number = 78
+visible = False
+facts = None
+
+solution = '''\
+expr(N) --> term171(N).
+expr(N) --> term171(N1), [+], expr(N2), {N is N1 + N2}.
+
+term171(N) --> factor171(N).
+term171(N) --> factor171(N1), [*], term171(N2), {N is N1 * N2}.
+
+factor171(N) --> number171(N).
+factor171(N) --> ['('], expr(N), [')'].
+
+memb171(X, [X|_]).
+memb171(X, [_|T]) :-
+ memb171(X, T).
+
+digit171(N) --> [N], { memb171(N, [0,1,2,3,4,5,6,7,8,9]) }.
+number171(N) --> digit171(N).
+number171(N) --> number171(N1), digit171(D), { N is 10*N1 + D }.
+'''
diff --git a/prolog/problems/dcg/expr_3/en.py b/prolog/problems/dcg/expr_3/en.py
new file mode 100644
index 0000000..3f91977
--- /dev/null
+++ b/prolog/problems/dcg/expr_3/en.py
@@ -0,0 +1,14 @@
+id = 171
+name = 'expr/3'
+slug = 'arithmetic expressions with meaning'
+
+description = '''\
+<p>Write a DCG with the starting symbol <code>expr</code> for the language of arithmetic expressions consisting of numbers (without leading zeros), addition and multiplication. Subexpressions can be grouped using parentheses. The meaning of a word in this language is the numeric value of the represented arithmetic expression.</p>
+<p>Example words: <code>(1+2)*3</code>, <code>42*8*3</code>, <code>(2+1)*(3+4)</code>.</p>
+<pre>
+ ?- expr(N, ['(',2,'+',1,')','*','(',3,'+',4,')'], []). % (2+1)*(3+4) = 21
+ N = 21.
+</pre>
+'''
+
+hint = {}