Arijit Das wrote:
Even though Bison is a bottom up parser, if I need to pass a variable's
content top-down, what are the possible ways (except using global
variable)?


Register_decl
        : REGISTER IDENTIFIER Register_decl_contd
                {
                        Char * register_name = $2;
                        // $2 has the 'register_name' now. I wanted that
name to be communicated down to the scope of the non-terminal
'Register_decl_contd'
                }
        ;


Register_decl_contd
        : '.' IDENTIFIER ...something...
                { ... }
        | '=' IDENTIFIER ...something...
                { ... }
        | '[' IDENTIFIER ...something...
                {
                        // I want to use get the content of
'register_name' here to be able to print user friendly error messages,
if required. // How can I get that info since register_name
is defined in a top level scope in the non-terminal hierarchy? Any help?
                }

        ;


Thanks
Arijit

Some quick thoughts, which may or may not be helpful:

- what's wrong with global variables?

- context-dependent information is generally handled better at a higher level than scanning. If you just use the scanner to build an AST, for example, then all the information you need is encoded into the AST, and the context is then obvious.

- look up "inherited attributes" (or "synthesised attributes"), which should fix your immediate problem. Basically, $0, $-1, etc let you access symbols on the stack to the left of the current rule. However, you should only really do this as a last resort.

- If you need to do a lot of this, and don't want to build an AST, it may be helpful to look at using Antlr instead of bison.

Evan


_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison

Reply via email to