MOO-cows Mailing List Archive

[Prev][Next][Index][Thread]

Re: modification of parser.y



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 modifying
> parser.y to recognize a new type of literal?
>  
> The new literal is a table (TYPE_TABLE) which is represented like:
>  
> [[key, value], [key, value], ...]
>  
> where key and value are arbitrary values of any type.  An empty table
> would just be [].
>  
> 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!)
>  

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 */ }
	...		


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 */ }
	

/* ... */

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

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


	-hpa

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



Follow-Ups: References:

Home | Subject Index | Thread Index