Re: Compiler with adjustable parsers

Aaron Sloman <aarons@cogs.sussex.ac.uk>
Thu, 8 Mar 90 02:55:31 GMT

          From comp.compilers

Related articles
[4 earlier articles]
Re: Compiler with adjustable parsers dhw@itivax.iti.org (1990-03-02)
Re: Compiler with adjustable parsers pgl@cup.portal.com (1990-03-15)
Re: Compiler with adjustable parsers ipser@vaxa.isi.edu (Ed Ipser) (1990-03-03)
Re: Compiler with adjustable parsers PIRINEN@CC.HELSINKI.FI (Pekka P. Pirinen) (1990-03-03)
Re: Compiler with adjustable parsers fineman@ptolemy.arc.nasa.gov (Charles Fineman) (1990-03-05)
Re: Compiler with adjustable parsers webber@athos.rutgers.edu (1990-03-07)
Re: Compiler with adjustable parsers aarons@cogs.sussex.ac.uk (Aaron Sloman) (1990-03-08)
| List of all articles for this month |
From: Aaron Sloman <aarons@cogs.sussex.ac.uk>
Date: Thu, 8 Mar 90 02:55:31 GMT
Keywords: parse, pop-11

In Newsgroup comp.compilers hackeron@ATHENA.MIT.EDU writes:
> Does anyone know of a compiler/language that allows you to specify changes
> to how the language is parsed (in part at least) from withing the program.


The language Pop-11, a Lisp-like language with a more readable
Pascal-like syntax admits macros like Lisp. It also has a more
powerful facility.


Pop-11 is compiled (incrementally) to a high level stack-oriented
virtual machine, which is then compiled to machine code. The
language includes procedures for planting code for the high level
virtual machine, and users can declare identifiers to be "syntax"
words which will then invoke procedures that read in items from the
input stream and then plant code.


For example, Pop-11 already allows lists, represented using
square brackets, e.g.


        [a list of numbers and words 1 2 3 and lists [a list []] [99]]


If you wished to define a new syntax construct for building lists
of words and numbers with repeated elements, using the following
format


        <_list <number>:<item> <number>:<item> .... list_>


So that


        <_list 3:cat 4:mouse 24:99 list_>


was equivalent to


        [cat cat cat mouse mouse mouse mouse 99 99 99 99 99 99 99 .....]


You could do it thus:


constant syntax list_> ; ;;; declare the closing bracket


define syntax <_list;
        lvars item, number, count = 0;


        repeat
                ;;; read a text item from input stream
                readitem() -> item;
                quitif(item == "list_>");


                if isinteger(item) then item -> number;
                        pop11_need_nextitem(":") ->; ;;; insist on colon, but
                                                                                    ;;; discard it
                        itemread() -> item;
                else
                        mishap('INTEGER EXPECTED in <_list ... list_>', [^item])
                endif;


                number + count -> count;


                repeat number times
                        ;;; plant instruction to push item onto Pop-11 stack
                        sysPUSHQ(item)
                endrepeat


        endrepeat;
        sysPUSHQ(count);
        sysCALLQ(conslist)
enddefine;


This can now be tested in a piece of program that includes the new
kind of list expression, builds the list, and prints it out:


        pr(<_list 3:cat 5:mouse 5:99 list_>);
        [cat cat cat mouse mouse mouse mouse mouse 99 99 99 99 99]


(It would require a bit more work to allow such expressions to be
nested, or to allow the numbers to be replaced by variables whose
values were used at run time in building the list.)


In a language like Lisp, macros allow you only to define new
constructs that are essentially abbreviations for what could have
been expressed in Lisp anyway. The Pop-11 extension mechanism does
not have this restriction, since what can be expressed in the target
virtual machine is richer than what can be expressed in Pop-11.


That is how the Poplog implementation of Pop-11 also allows the
implementation of Common Lisp, Prolog and ML, all in one process if
required, and all sharing comon data-structures, etc. Users can
employ the mechanisms either to extend the languages provided or
to implement new languages. E.g. one user claimed to have
implemented most of Scheme in a few weeks in his spare time. There
are several language extensions in the Poplog library, including an
object oriented extension to Pop-11.


For a brief introduction to Pop-11 (slightly out of date now) see


        A Barrett A Ramsay A Sloman
        POP-11: a Practical Language for Artificial Intelligence
        Ellis Horwood and John Wiley
        1985 (twice reprinted since)/




Aaron Sloman,
School of Cognitive and Computing Sciences,
Univ of Sussex, Brighton, BN1 9QH, England
        EMAIL aarons@cogs.sussex.ac.uk
                        aarons%uk.ac.sussex.cogs@nsfnet-relay.ac.uk
                        aarons%uk.ac.sussex.cogs%nsfnet-relay.ac.uk@relay.cs.net
        BITNET: aarons%uk.ac.sussex.cogs@uk.ac
        UUCP: ...mcvax!ukc!cogs!aarons
                        or aarons@cogs.uucp





Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.