I am working with Bison to build an AST for a compiler I am writing. What is the best way to build up the nodes in the AST? My question might be more clear with an example.
Given the following snippet:
field
: modifier type TOK_IDENT TOK_SEMICOLON
{
// I want to return a pointer to a node of type Field
// i.e. $$ = new Field(name, isVisible, isStatic, type);
}
;
modifier
: visibility_opt static_opt
{
// Should I make the new Field here and pass it up?
// Or a new type that contains both vis and static options?
}
;
visibility_opt
: /* default */ { $$ = true; }
| TOK_PUBLIC { $$ = true; }
| TOK_PRIVATE { $$ = false; }
;
static_opt
: /* default */ { $$ = false; }
| TOK_STATIC { $$ = true; }
;
In the above example I want the field rule to return a Field node, but I need some of the attributes of the modifier rule that will be passed up during parsing (i.e. these are synthesized attributes).
I can think of two ways to do this without changing the grammar.
In situations like this what is the preferred way to go?
Like others have suggested the preferred way would be to have a struct Modifier with visibility and static options. But I would probably make this a static modifier in that it won't be passed into field, but instead simply used to extract the values and then passed into Field. You can even allocate it in the stack, and reuse it to make this faster.
Something along the following lines:
static struct { boolean vis_opt; boolean static_opt; } mod;
field
: modifier type TOK_IDENT TOK_SEMICOLON
{
$$ = new Field(..., mod.vis_opt, mod.static_opt, ...);
}
;
modifier
: visibility_opt static_opt
{
mod.vis_opt = $1;
mod.static_opt = $2;
}
;
visibility_opt
: /* default */ { $$ = true; }
| TOK_PUBLIC { $$ = true; }
| TOK_PRIVATE { $$ = false; }
;
static_opt
: /* default */ { $$ = false; }
| TOK_STATIC { $$ = true; }
;
Also, unless you're pretty certain of the future of the language, you might want to consider making visibility an enum. You never know what kind of visibilit'ies' you might end up dreaming while developing the language, and at least if you have it in an enum is easier to be extended later.
Enjoy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With