🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #4150

Re: Parsers using OOP . What objects to use ? Examples ?

#4150
From: Paul Johnson
Date: Fri, 14 Jun 1991 08:23
38 lines
2471 bytes
In article <1991Jun11.185402.21089@eagle.lerc.nasa.gov> sshws@convx1.lerc.nasa.gov (Herb Schilling) writes:

>We would like to implement a parser using Turbo Pascal. It seems that
>using OOP concepts could naturally be applied to the problem.  [...]
>Has anybody out there done or thought about writing a parser using
>objects ?

Yup.  Look at the Eiffel libraries.  Briefly:

The Eiffel library lexer is a lex(1) style tool which will either
build the lexer on the fly or load an existing one in from a file
(Eiffel makes storing and reloading complex objects a trivial matter).
The parser library provides a base class CONSTRUCT and derived classes
SEQUENCE (for sequences of 0-n other constructs), AGGREGATE (for
grammer productions of the form A ::= BCD), CHOICE (for productions of
the form A ::= B|C|D), TERMINAL for lexical tokens and KEYWORD for
keywords.  The SEQUENCE, AGGREGATE and CHOICE classes are the
interesting ones.  They store the current lexer position (for later
backtracking) and then read the sequence of lexer tokens and try to
match them.  If they succeed they set a flag and return.  If they fail
they clear the flag, tell the lexer to back up and then return.  To
use these classes you derive your own which contain functions to
generate a list of the expected objects.

Of course there is a bit more than this.  There is a procedure
`commit' which is used for syntax error detection (meaningful syntax
error messages are generated automatically).  CONSTRUCT also contains
deferred functions `preaction' and `postaction' which get called once
the parsing has been done.  You put your semantic actions in them.

Paul.


-- 
Paul Johnson |            Isn't modern education wonderful: one size fits all!
-------------^------------------v-------------------------v-------------------
GEC-Marconi Research is not 	| Telex: 995016 GECRES G  | Tel: +44 245 73331
responsible for my opinions.	| Inet: paj@gec-mrc.co.uk | Fax: +44 245 75244
Technical Details
Message-ID: <962@snap.mrcu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!ukc!mucs!logitek!hrc63!mrcu!paj
References: <1991Jun11.185402.21089@eagle.lerc.nasa.gov>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!ukc!mucs!logitek!hrc63!mrcu!paj
From: paj@mrcu (Paul Johnson)
Newsgroups: comp.lang.misc
Subject: Re: Parsers using OOP . What objects to use ? Examples ?
Message-ID: <962@snap.mrcu>
Date: 14 Jun 91 08:23:05 GMT
References: <1991Jun11.185402.21089@eagle.lerc.nasa.gov>
Reply-To: paj@uk.co.gec-mrc (Paul Johnson)
Organization: GEC-Marconi Research Centre, Great Baddow, UK
Lines: 37