Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7714281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:05:20+00:00 2026-06-01T02:05:20+00:00

I am working with Bison to build an AST for a compiler I am

  • 0

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.

  1. Make the non-terminal modifier have type Field, create the new Field here, fill in what I can, and pass it up to field to fill in the rest.
  2. Let modifier have its own type that holds two bool values and pass this up extracting the data when creating the new Field in the field rule.

In situations like this what is the preferred way to go?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:05:22+00:00Added an answer on June 1, 2026 at 2:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just been re-working an old bit of compiler-like code written using bison.
I am looking for a very short working example of flex and bison with
Working on a simple poker script in PHP and need a way to determine
I am working with Node.js to build a web socket server that uses mongodb.
Working example: http://alpha.jsfiddle.net/gTpWv/ Both of the methods work separately, but once regexp for smilies
Hi I'm working on a small bison to learn how it works. The bison
I'm writing an assembler for a custom micro controller I'm working on. I've got
My day job includes working to develop a Pascal-like compiler. I've been working all
Working on Transactions in .net. Had a question on flowing transactions through sub functions.
Working on a new way of using an already existing searchengine in an application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.