1

i tried to write a predicate and N={Expression,Number,Digit,Operator,Variable}

T={1,2,3,+,-,*,(,),X,Y,Z} and S is expression and program p defines as

Expression-->Number
**Expression-->(Expression) Operator (Expression)**
Number-->Digit
**Number --> Digit Number**
Digit-->1
Digit-->2
Digit-->3
Operator-->+
Operator-->-
Operator-->*
Variable-->X
Variable-->Y
Variable-->Z

I think that i implemented many parts however could not implement bold parts!!

my prolog code that describes the terminals by depending program P:

 expression(S,S).
    expression(S,R):-number(S,R).
    expression(S,R):-expression(S,R),-operator(S,['('|S]),expression(S,[')'|R]).
    expression(S,R):-operator(S,[','|S1]),expression(S1,R).
    expression(S,R):-variable(S,[','|S1]),expression(S1,R).
    number(S,R):-digit(S,R).
    digit(['1'|R],R).
    digit(['2'|R],R).
    digit(['3'|R],R).
    operator(['+'|R],R).
    operator(['-'|R],R).
    operator(['*'|R],R).
    variable(['X'|R],R).
    variable(['Y'|R],R).
    variable(['Z'|R],R).

Please help me.

limonik
  • 499
  • 1
  • 6
  • 28
  • The original grammar description is incomplete. You have variables defined but not used anywhere in the definition of expressions. – lurker Jul 02 '15 at 01:03
  • @lurker yes now i notice it. However in program definition variables are not used. Honestly i am really confused about dcg – limonik Jul 02 '15 at 10:01
  • 1
    Have you read any online tutorials or discussions about Prolog DCGs? They can be quite helpful. For example, [Tutorial - Using Definite Clause Grammars in SWI-Prolog](http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB8QFjAA&url=http%3A%2F%2Fwww.pathwayslms.com%2Fswipltuts%2Fdcg%2F&ei=XS6VVZLDGIy-ggTFhI3oDw&usg=AFQjCNE511aVfbhO5EBEJx2s5az0bKewcQ&bvm=bv.96952980,d.eXY). Googling "DCG Prolog" yields a lot of information that can help you understand DCGs. – lurker Jul 02 '15 at 12:29

1 Answers1

4

Here is probably the you meant:

:- set_prolog_flag(double_quotes, chars).

expression --> number.
expression --> variable.
expression --> "(", expression, operator, expression, ")".

number --> digit.
number --> digit, number.

digit --> "1".
digit --> "2".
digit --> "3".

operator --> "+"|"-"|"*".  % more compact notation
variable --> "X"|"Y"|"Z".

Use it like so to generate all sentences ordered by length:

?- length(L, N), phrase(expression, L).
   L = ['1'], N = 1
;  L = ['2'], N = 1
;  L = ['3'], N = 1
;  L = ['X'], N = 1
;  L = ['Y'], N = 1
;  L = ['Z'], N = 1
;  L = ['1', '1'], N = 2
;  L = ['1', '2'], N = 2
;  ... .

And to get most compact and readable answers see this for more. That is, download the module below:

?- use_module(double_quotes).
?- length(L, N), phrase(expression, L).
   L = "1", N = 1
;  L = "2", N = 1
;  L = "3", N = 1
;  L = "X", N = 1
;  L = "Y", N = 1
;  L = "Z", N = 1
;  L = "11", N = 2
;  L = "12", N = 2
;  ... .

To see how the DCG is implemented, say listing for each non-terminal. E.g.:

?- listing(expression).
expression(A, B) :-
        number(A, B).
expression(A, B) :-
        variable(A, B).
expression(['('|A], E) :-
        expression(A, B),
        operator(B, C),
        expression(C, D),
        D=[')'|E].
false
  • 10,264
  • 13
  • 101
  • 209
  • i have no permission to use --> in my prolog program. I could not understand how i can write Expression-->(Expression) Operator (Expression) and Number --> Digit Number in prolog program. – limonik Jul 01 '15 at 21:56
  • 1
    @limonik: **Who** says that you have not permission - or what Prolog are you using? – false Jul 01 '15 at 22:04
  • as i wrote it down it is my homework and unfortunately there are some restrictions like ---you should use this or not use.---- – limonik Jul 01 '15 at 22:07
  • 1 ?- listing(expression). expression(A, A). expression(A, B) :- number(A, B). expression(A, B) :- expression(A, B), -operator(A, ['('|A]), expression(A, [')'|B]). expression(A, C) :- operator(A, [','|B]), expression(B, C). expression(A, C) :- variable(A, [','|B]), expression(B, C). true. . It is really good method to test it. Thank you – limonik Jul 01 '15 at 22:15
  • 3
    @limonik: Tell your teacher that it is much better to first **use** DCGs to get a good grip of it. – false Jul 01 '15 at 22:18