Re: How to parse expressions with parenthesis
From: alex (alexmartin_6_at_yahoo.com)
Date: 03/28/04
- Previous message: Martin Sondergaard: "Re: How to parse expressions with parenthesis"
- In reply to: Martin Sondergaard: "Re: How to parse expressions with parenthesis"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Previous message: Martin Sondergaard: "Re: How to parse expressions with parenthesis"
- In reply to: Martin Sondergaard: "Re: How to parse expressions with parenthesis"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|