Re: How to parse expressions with parenthesis

From: alex (alexmartin_6_at_yahoo.com)
Date: 03/28/04

  • Next message: no-spam: "Re: Sublists question"
    Date: 27 Mar 2004 20:23:57 -0800
    
    

    "Martin Sondergaard" <nobody@nowhere.com> wrote in message news:<1080427630.28769.0@nnrp-t71-03.news.uk.clara.net>...
    > "alex" <alexmartin_6@yahoo.com> wrote :
    >
    > > Hi everybody.
    > > I'm new to prolog programming and I trying to make a program to
    > > simplify algebraic expressions. My problem is the following:
    > > I need to parse a term inside parenthesis, like X+10 in the
    > > following expression: (x+10)*x, but when I use some clauses to get the
    > > content, like:
    > >
    > > getTerms( (Term1)*Term2, Term1,Term2). %It just complains.
    > > getTerms(Term1*(Term2), Term1, Term2). %It just ignores parenthesis.
    > >
    > > Have you any idea about the way to make a clause that answer to the
    > > following query:
    > >
    > > ?- getTerms( (x+10)*x, Term1, Term2).
    > >
    > > In this way...
    > >
    > > Term1 = x+10
    > > Term2 = x
    > >
    > > Thanks all of you in advance.
    > >
    > > Alex
    >
    > Using SWI Prolog, it works fine for me.
    > Here is a copy of what SWI Prolog does :
    >
    > 3 ?- assert( getTerms( (Term1)*Term2, Term1,Term2) ).
    >
    > Term1 = _G447
    > Term2 = _G448
    >
    > Yes
    > 4 ?- getTerms( (x+10)*x, Term1, Term2).
    >
    > Term1 = x+10
    > Term2 = x
    >
    > Yes
    > 5 ?-
    >
    > So it works fine.
    >
    > Next I asserted the second clause of "getTerms" :
    >
    > 6 ?- assert( getTerms(Term1*(Term2), Term1, Term2) ).
    >
    > Term1 = _G447
    > Term2 = _G448
    >
    > Yes
    > 7 ?- listing(getTerms).
    >
    > :- dynamic getTerms/3.
    >
    > getTerms(A*B, A, B).
    > getTerms(A*B, A, B).
    >
    > Yes
    > 8 ?-
    >
    > As you can see, the two clauses are identical,
    > so the second clause is not needed.
    >
    > Here's another test, which works OK too :
    >
    > 11 ?- getTerms( 4*5*6*(y), Term1, Term2).
    >
    > Term1 = 4*5*6
    > Term2 = y
    >
    > Yes
    > 12 ?-
    >
    >
    > But this query will not work :
    >
    > ?- getTerms( 4+5+6*(y), Term1, Term2).
    >
    > No
    > 11 ?-
    >
    > That didn't work because it parses the "*" operator before
    > the "+" operators, so this query is
    > equivalent to "getTerms( 4+5+(6*y), Term1, Term2)",
    > which of course fails because "4+5+(6*y)" does not match "A * B".
    >
    > This works better :
    >
    > 10 ?- getTerms( (4+5+6)*(y), Term1, Term2).
    >
    > Term1 = 4+5+6
    > Term2 = y
    >
    > Yes
    >
    > 9 ?-
    >
    >
    > I hope this helps.
    >
    > --
    > Martin Sondergaard,
    > London.

    Thank you Martin for your convincing answer, I finally realized myself
    that the priority Prolog assigns to its symbols and operators
    corresponds to algebraic rules, so there is no need for parenthesis in
    any of my clauses.
    thanks again
    Alex.


  • Next message: no-spam: "Re: Sublists question"

    Relevant Pages

    • Re: negotiation by failure- list operations
      ... > from other Prolog systems, why it was worth to go through the trouble ... As soon as a clause is typed in it is instantly asserted into ... same full-screen editor window. ... software patents (did you know even tab controls are supposedly ...
      (comp.lang.prolog)
    • Re: Sublists question
      ... Since any of the variables can be s, this clause is certainly true. ... Here is an alternative predicate that does not have that problem. ... What Prolog are you using? ... Anyway, with the new sublist/2 predicate, the query, langford, ...
      (comp.lang.prolog)
    • Re: if statements in prolog
      ... JNY wrote: ... userEntry; so if you meant to "get the value that the user typed into ... Maybe you should tell us which Prolog system you use, ... If the "read" goal is part of a clause and thee ...
      (comp.lang.prolog)
    • Re: Problem to understand "Prolog Vocabulary"
      ... It has got the "header" with the name and formal parameters. ... Clause does not return a value. ... if a clause fails then the entire computation is reversed back to the state before the failure and prolog tries to "exercise" other matching clauses. ... It is "sort of" executing a program forward and backward. ...
      (comp.lang.prolog)
    • Re: Problem to understand "Prolog Vocabulary"
      ... Clause does not return a value. ... It either successes or fails. ... It is "sort of" executing a program forward and backward. ... a prolog "procedure " either ...
      (comp.lang.prolog)