Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!swrinde!cs.utexas.edu!math.ohio-state.edu!jussieu.fr!oleane!pipex!warwick!griffin.nott.ac.uk!nott-cs!lut.ac.uk!usenet
From: COGAL <G.A.LUSCOMBE-94@student.lut.ac.uk>
Subject: Parsing - What am I doing wrong?
Sender: usenet@lut.ac.uk (Usenet-News)
Message-ID: <D936Lu.G6F@lut.ac.uk>
Date: Wed, 24 May 1995 14:22:41 GMT
Nntp-Posting-Host: co-pclab29.lut.ac.uk
Organization: Loughborough University, UK.
Lines: 64

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


