Parsing - What am I doing wrong? 
Author Message
 Parsing - What am I doing wrong?

I have written the following program which should simply respond with
yes if an expression can be parsed, and no if it can.  I cannot see
what is wrong with the program, and believe that it is basically
correct.  The program should respond with yes for the following
expression :

parse([a,':=',1],[n(block]).

and no for :

parse([a,':=',1,';',while,a,'<',3,do,a,':=',a,'+',1],[n(block)]).

As I'm fairly new to this language, I'm unsure of what is going wrong
with the following program which will always respond with no.  Any
advice that you may be able to give, or hints as to where the program
is failing would be most appreciated.

The Program :

rule(n(block),[t(begin),n(stmts),t(end)]).
rule(n(block),[n(stmt)]).

rule(n(stmts),[n(stmt),t(';'),n(stmts)]).
rule(n(stmts),[n(stmt)]).

rule(n(stmt),[t(while),n(expr),t(do),n(block)]).
rule(n(stmt),[n(ident),t(':='),n(expr)]).
rule(n(stmt),[t(iff),n(expr),t(then),n(block)]).
rule(n(stmt),[t(iff),n(expr),t(then),n(block),t(else),n(block)]).
rule(n(expr),[n(ident),n(op),n(expr)]).
rule(n(expr),[n(ident)]).

rule(n(op),[t('+')]).
rule(n(op),[t('*')]).
rule(n(op),[t('=')]).
rule(n(op),[t('<')]).
rule(n(ident),[t(a)]).
rule(n(ident),[t(b)]).
rule(n(ident),[t(c)]).
rule(n(ident),[t(0)]).
rule(n(ident),[t(1)]).
rule(n(ident),[t(2)]).
rule(n(ident),[t(3)]).

append([],L2,L2).
append([Hdl1|Tll1],L2,[Hdl1|Result]):-
 append(Tll1,L2,Result).

parse([Hd1|Tl1],[Hd2|Tl2]):-
 Hd2=t(x),
 Hd1=x,
 parse(Tl1,Tl2).

parse(Phrase,[Hd4|Tl4]):-
 Hd4=n(x),
 rule(Hd4,Listbody),
 append(Listbody,Tl4,Newlist),
 parse(Phrase,Newlist).

Thanks in advance,

Gavin



Sun, 09 Nov 1997 03:00:00 GMT  
 Parsing - What am I doing wrong?

Quote:

>I have written the following program which should simply respond with
>yes if an expression can be parsed, and no if it can.  I cannot see
>what is wrong with the program, and believe that it is basically
>correct.

.. (rules removed) ...

Quote:

>parse([Hd1|Tl1],[Hd2|Tl2]):-
> Hd2=t(x),
> Hd1=x,
> parse(Tl1,Tl2).

>parse(Phrase,[Hd4|Tl4]):-
> Hd4=n(x),
> rule(Hd4,Listbody),
> append(Listbody,Tl4,Newlist),
> parse(Phrase,Newlist).

Hi Gavin,

You get

?-parse([begin,
         a,':=',1,';',while,a,'<',3,do,a,':=',a,'+',1,
         end],[n(block)]).
yes

If you first of all change the small (non-variable!) x's in X's
in parse/2. Furthermore add a clause to end the recursion
when your lists are empty.
So the new definition of parse is:
 (I also removed some superfluous variables,
  by moving terms (t(X) and n(X)) into the head
  where possible):

parse([],[]).
parse([X|Tl1],[t(X)|Tl2]):-
 parse(Tl1,Tl2).
parse(Phrase,[n(X)|Tl]):-
 rule(n(X),Listbody),
 append(Listbody,Tl,Newlist),
 parse(Phrase,Newlist).



Fri, 14 Nov 1997 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. What am I doing wrong!

2. What am I doing wrong?

3. Stupid newb question: What am I doing wrong?

4. What am I doing Wrong

5. what am i doing wrong

6. Help - What am I doing wrong

7. Saving a Variable to a field in a record- what am i doing wrong

8. What am I doing wrong?

9. What am I doing wrong?

10. #CONTROL What am I doing wrong

11. What am i doing wrong.

12. What am I doing wrong?

 

 
Powered by phpBB® Forum Software