help-bison
[Top][All Lists]
Advanced

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

Re: mid-rule actions


From: Hans Aberg
Subject: Re: mid-rule actions
Date: Sat, 25 Nov 2000 19:11:13 +0100

At 13:52 -0600 0-11-24, Davy Durham wrote:
>simple_class_declaration
>    : class IDENT
>        { // mid-rule action
>            ...
>            // do something now that I know a class is being defined
>            int a=...;
>            ...
>        } '{' class_body '}'
>    {  // rule's real action
>        ...
>            ... use a's value ... (unfortunately, it can't be used)
>        ...
>    }
>    ;
...
>I really wish there were a way to use 'a' in the rule's real action...
>    Any sugguestions....  It would be quite convenient not to have to
>store a's value somewhere then getting it back thus creating another
>point-of-failure...

You want to create semantic dependencies in your grammar:

Part of that seems to be what the semantic parser was for.

Also, Flex has the ability to set "start conditions" for rules -- but I do
not know if Bison has something similar.

Otherwise, you could declare "a" to be global, which will work as long as
Bison is deterministic. -- Akim wrote in the "Bison back-tracking" thread:
>See the nodes about lexical tie-ins in the documentation.

You could avoid the mid-rules by

int a;

%%
simple_class_declaration:
    class_key '{' class_body '}' { /* Use "a" */ }

class_key : class IDENT { /* Set "a" */ }

Because Bison is deterministic, "a" cannot get a value from another rule
even if you have more than one, and the value "a" must be set before you
use it in the simple_class_declaration rule.

Another way to create a grammar with semantic dependencies is to manipulate
the lexical analyzer. If you use Flex, you could have a rule
{identifier}  {  if (defined(yytext, yyleng))  return DEFINED_IDENTIFIER;
                 else return UNDEFINED_INDENTING; }
-- With Flex, one can also do the same thing using its "start conditions"
which turn on/off the various lexemes

Bison will not know that this is a context dependency, but will treat them
as different tokens, so it is possible let Bison execute different rule or
different non-terminal variables for what in reality is the same text in
the file.

  Hans Aberg





reply via email to

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