On 27 Feb 2007, at 16:51, Bob Rossi wrote:
I'm using bison along with it's semantic actions. In particular, I'm
using the $$, $1, $2 constructs in order to help me build a parse
tree.
In the past, I've place the top level AST type as a global object, and
the top level rule would assign it's $$ to that global variable.
However, I'm wondering if it's possible, after the parse is done,
to ask
bison to give you back the top level's rule $$ variable. That way, I
don't have to keep around an extra global variable.
Is this question clear enough to be answerable? :)
Sure. In the past it was not possible. Perhaps if you try the pure
parser. I tweaked my own skeleton file so that class parser contains:
#if !YYLSP_NEEDED
int parse(parse_type& p, semantic_type& s);
int parse(parse_type& p) {
semantic_type s;
return parse(p, s);
}
#else
int parse(parse_type& p, semantic_type& s = semantic_type(),
location_type& l = location_type());
#endif
This way, one can get semantic and location values back as arguments.
In addition, I added some operators:
std::istream& operator>>(parse_type& d);
friend parser& operator>>(std::istream&, parser&);
friend std::istream& operator>>(std::istream&, parse_type&);
This way, invocation can take place as:
std::ifstream ifs(...);
semantic_value sv;
parser p(...);
...
ifs >> p >> sv; // Read and parse smenatic value
which I think is intuitive.
Hans Aberg
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison