# modification of parser.y

H. Peter Anvin (hpa@trantor.zytor.com)
Sat, 22 Jun 1996 21:14:42 PDT

- Messages sorted by:[ date ][ thread ][ subject ][ author ]

    Followup to:  <199606222159.OAA16729@wasteland.calbbs.com>
    By author:    Brian Buchanan <brian@wasteland.calbbs.com>
    In newsgroup: local.moo
    >
    > Could someone (Pavel?) who is familiar with LALR assist me with modifyi=
    ng
    > parser.y to recognize a new type of literal?
    > =20
    > The new literal is a table (TYPE_TABLE) which is represented like:
    > =20
    > [[key, value], [key, value], ...]
    > =20
    > where key and value are arbitrary values of any type.  An empty table
    > would just be [].
    > =20
    > I would be equally grateful to anyone who could point me to a man page
    > for LALR.  It seems to be conveniently missing on my system, as well as
    > every man pages server I've found on the web.  (ARGH!)
    > =20

    Sounds like you are trying to implement an associative array.  The
    grammar for that would look something like:

    (as a part of expr)
    		| '[' hash_list ']'	{ /* nonempty hash */ }
    		| '[' ']'		{ /* empty hash */ }
    	...	=09

    hash_list:	  hash_item		   { /* first entry */ }
    		| hash_list ',' hash_item  { /* subsequent entries */ }
    		;

    hash_item:	'[' expr ',' expr ']'	{ /* hash item */ }
    		;

    However, I would strongly discourage the syntax you have chosen; it is
    hard to read and pretty verbose in terms of how much typing is
    required.  Also, <shameless self-plug follows> LPMOO uses [...] for
    byte vectors; I am considering adding byte vectors to MOO itself, and
    would like to use the same or at least a similar syntax.

    I would rather suggest something like:

    << key, value ; key, value ; key, value >>

    (or just using commas throughout, but that has the disadvantage that
    an off-by-one error is hard to find.)

    which would be implemented as

    a. add the additional multi-character tokens '<<' (LL) and '>>' (RR)
    to the lexer; this is easy (when reading '<', check if the next
    character is also '<' if so return LL otherwise unget the second
    character and return '<'; ditto '>'.)

    b. add the following parser constructs:

    /* part of the expr construct */

    	| LL RR				{ /* empty hash */ }
    	| LL hash_list RR		{ /* nonempty hash */ }
    =09

    /* ... */

    hash_list:	  hash_item		  { /* first item */ }
    		| hash_list ';' hash_item { /* subsequent items */ }
    		;

    hash_item:	expr ',' expr		{ /* hash item */ }
    		;

    	-hpa

    --=20
    PGP public key available - finger hpa@zytor.com
    "The earth is but one country, and mankind its citizens."  --  Bah=E1'u'l=
    l=E1h
    Just Say No to Morden * The Shadows were defeated -- Babylon 5 is renewed=
    !!