|
From: | Evan Lavelle |
Subject: | Re: Top Dowm Variable Communication |
Date: | Wed, 22 Aug 2007 09:50:17 +0100 |
User-agent: | Thunderbird 2.0.0.6 (Windows/20070728) |
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_nameis 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
[Prev in Thread] | Current Thread | [Next in Thread] |