help-bison
[Top][All Lists]
Advanced

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

Re: a easy way to treat flexible token order?


From: Hans Aberg
Subject: Re: a easy way to treat flexible token order?
Date: Tue, 11 Feb 2003 10:46:11 +0100

Reply-to: address@hidden

Please cc the Help Bison list so that those on that list may help.

At 10:04 -0700 2003/02/10, Jibin Han wrote:
>    The data file I am processing has some flexible token order, for
>example, a complete line can be:
>        firstname="something", middlename="something",
>lastname="something"
>    or
>        firstname="something", lastname="something",
>middlename="something"
>    or
>        lastname="something", firstname="something",
>middlename="something"
>
>    I am wondering if there is some easy way to handle this other than
>my current practice by writing a rule and action for each case?

One variation is to write out all possibilities
  item_list:
      firstname ',' lastname {...}
    | firstname ',' middlename ',' lastname {...}
      ...
  ;

  firstname: FIRSTNAME "=" STRING {...}
  ;

(The use of "=" assumes a declaration say %token ASSIGN "=")

The advantage of this approach is that you have more control over the
syntactic details.

A more general construct is to write
  item_list:
      item               {a0}
    | item ',' item_list {a1}
  ;
  item:
      name "=" STRING {...}
  ;
  name:
      FIRSTNAME {...}
      MIDDLENAME {...}
      LASTNAME {...}
  ;
Then actions a0, a1 build a structure that can hold all three strings.
Action a1 will check that no item is redefined, and if so, issue an YYERROR
to the parser.

This approach is better if you have more complicated construct, say with
many labels.

  Hans Aberg






reply via email to

[Prev in Thread] Current Thread [Next in Thread]