[GRASS5] lex/yacc parser help ...

Glynn Clements glynn.clements at virgin.net
Sun Mar 10 18:28:20 EST 2002


Eric G. Miller wrote:

> I'm trying to write a lex/yacc parser for reading in reclass rules for a
> new s.reclass.  Having zero experience with lex and yacc, I'm having a
> little trouble trying to figure out how to have it be in one of three
> parse modes.  I'll know when calling the parser, which mode I want.
> 
> Basically, the reclass rules will allow sites to be reclass via existing
> category values, a decimal (double) attribute or a string attribute.
> The rules can be like:
> 
> item = cat
> item thru item = cat
> item item [item ...] = cat
> * = cat
> 
> The "*" is a default reclass rule, and is a singleton (if it is given).
> 
> So, a generic grammer...
> 
> rule:  item '=' cat
>     |  item "thru" item '=' cat
>     |  item_list '=' cat
>     |  '*' = cat
>     ;
> 
> item_list: item
>     | item_list item
>     ;

This is ambiguous; "item = cat" could either match the first rule or,
given that a single item can be an item list, the third rule.

> item: integer
>     | decimal
>     | string
>     ;
> 
> One problem, is this grammar doesn't apparently restrict all items to
> be the same type.  So, perhaps I have four rule types (default_rule,
> int_rule, decimal_rule, string_rule).  I still need each parse line
> to ensure the same type of rule is invoked.  I'm not clear on how
> one does that...

%token THRU

rule		: integer '=' cat
		| decimal '=' cat
		| string '=' cat
		| integer THRU integer '=' cat
		| decimal THRU decimal '=' cat
		| string THRU string '=' cat
		| integer_list '=' cat
		| decimal_list '=' cat
		| string_list '=' cat
		| '*' '=' cat

integer_list	: integer integer
		| integer integer_list

decimal_list	: decimal decimal
		| decimal decimal_list

string_list	: string string
		| string string_list

-- 
Glynn Clements <glynn.clements at virgin.net>



More information about the grass-dev mailing list