Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!gatech!news.sprintlink.net!pipex!warwick!nott-cs!lut.ac.uk!usenet
From: COGAL <G.A.LUSCOMBE-94@student.lut.ac.uk>
Subject: Parsing in Prolog - Help Me Please!
Sender: usenet@lut.ac.uk (Usenet-News)
Message-ID: <D8qpA9.DoG@lut.ac.uk>
Date: Wed, 17 May 1995 20:37:21 GMT
Nntp-Posting-Host: co-pclab24.lut.ac.uk
Organization: Loughborough University, UK.
Lines: 215

As part of an assignment, I need to be able to produce a basic parser
in Prolog, using the following rules defined in these syntax diagrams.
These diagrams need to be expressed as Prolog syntax rules :

               ------           --------            ------
    Block     /      \          |      |           /      \
  ------------ begin   ---------|Stmts |-----------  end   -------------
       \      \      /          |      |           \      /      /
        \      ------           --------            ------      /
        |                                                       |
        |                                                       |
         \                      --------                       /
          \                     |      |                      /
           ---------------------| Stmt |----------------------
                                |      |
                                --------



                                --------                  
    Stmts                       |      |                   
  ------------------------------|Stmts |--------------------------------
                          /     |      |     \                    
                         /      --------      \                  
                        |                      |
                        |          __          |
                         \        /  \        /
                          \      /    \      /
                           ------  ;   ------
                                 \    /
                                  \__/

  

             ------    --------    ------     --------
    Stmt    /      \   |      |   /      \    |      |
  ---------- while  ---| Expr |---   do   ----|Block |-------------
       \    \      /   |      |   \      /    |      |      /
        \    ------    --------    ------     --------     /
         |                 __                             |
         |   -------      /  \      ------                |
         |\  |     |     /    \     |    |               /|
         | --|Ident|----   :=  -----|Expr|--------------- |
         |   |     |     \    /     |    |                |
         |   -------      \__/      ------                |
         |     __                                         |
         |    /  \     --------    ------     -------     |
         |\  /    \    |      |   /      \    |     |    /|
         | --  if  ----| Expr |---  then  ----|Block|---- |
         |   \    /    |      |   \      /    |     |     |
         |    \__/     --------    ------     -------     |
         |                                                |
         |    __                                          |
         |   /  \  ------  -----  -------  -----  ------- |
          \ /    \ |    | /     \ |     | /     \ |     | |
           -  if  -|Expr|- then  -|Block|- else  -|Block|/ 
            \    / |    | \     / |     | \     / |     |  
             \__/  ------  -----  -------  -----  -------  



              --------          --------           --------
    Expr      |      |          |      |           |      |
  ------------|Ident |----------|  Op  |-----------| Expr |-------------
              |      |   \      |      |           |      |      /
              --------    \     --------           --------     /
                          |                                     |
                          |                                     |
                           \                                   /
                            \                                 /
                             ---------------------------------
                                        
                           __           
                          /  \
     Op                  /    \
  -----------------------  +   ----------------------------------------
                  \      \    /     /
                   \      \__/     /
                    |             |
                    |      __     |
                    |     /  \    |
                    |\   /    \  /|
                    | ---  *   -- |
                    |    \    /   |
                    |     \__/    |
                    |             |
                    |      __     |
                    |     /  \    |
                    |\   /    \   |
                    | ---  =   -- |
                    |    \    /   |
                    |     \__/    |
                    |             |
                    |      __     |
                    |     /  \    |
                     \   /    \  /
                      ---  <   --
                         \    /
                          \__/


                           __           
                          /  \
     Ident               /    \
  -----------------------  a   ----------------------------------------
                  \      \    /     /
                   \      \__/     /
                    |             |
                    |      __     |
                    |     /  \    |
                    |\   /    \  /|
                    | ---  b   -- |
                    |    \    /   |
                    |     \__/    |
                    |             |
                    |      __     |
                    |     /  \    |
                    |\   /    \   |
                    | ---  c   -- |
                    |    \    /   |
                    |     \__/    |
                    |             |
                    |      __     |
                    |     /  \    |
                    |\   /    \  /|
                    | ---  1   -- |
                    |    \    /   |
                    |     \__/    |
                    |             |
                    |      __     |
                    |     /  \    |
                    |\   /    \   |
                    | ---  2   -- |
                    |    \    /   |
                    |     \__/    |
                    |             |
                    |      __     |
                    |     /  \    |
                     \   /    \  /
                      ---  3   --
                         \    /
                          \__/


I need to be able to parse the following :

[begin,a,':=',';',while,a,'<',3,do,a,':=',a,'+',1,end]
[begin,a,':=',2,';',if,a,'<',3,then,b,':=',1,else,b,':=',0,end]
[begin,if,a,'=',0,then,if,b,'=',0,then,c,':=',0,else,c,':=',1,end]
[a,':=',1]

and it should fail to parse :

[a,':=',1,';',while,a,'<',3,do,a,':=',a,'+',1]
[begin,a,':=',2,if,a,'<',3,then,b,':=',1,else,b,':=',0,end]

All it needs to do is simply give 'yes' or 'no' as the answer as to 
whether the parser was successful.

Here is what I've got so far, but as I'm fairly inexperienced with
this language, I'm sure there are quite a few errors :

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

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

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

rule(expr,[n(ident),n(op),n(expr)]).
rule(expr,[n(ident)]).   

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


append([],L2,L2).
append([HeadL1|TailL1],L2,[HeadL1|Result]):- 
append(TailL1,L2,Result).

parse([],[]).
parse(Block,[(Type)|Tail]):-      
	rule(n(Type),Expand),      
	append(Expand,Tail,Result),
	parse(Block,Result).
	
parse([Head|Tail],[Head|Stail]):-   
	parse(Tail,Stail).



The n() stands for a non-terminal, while all others are terminals.

Please could you give me some help as to how to correct this program,
or even how it should be written.

Thanks in advance,

Gavin




