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 9059661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:02:07+00:00 2026-06-16T15:02:07+00:00

I’m trying to implement a simple basic, but I’m afraid the operation of Bison

  • 0

I’m trying to implement a simple basic, but I’m afraid the operation of Bison is not clear for me. To implement a ‘while’ loop the examples I found uses the construction below (or similar):

while ‘(‘ expression ‘)’ statement { $$ = opr(WHILE, 2, $3, $5); }

But actually there are two hidden jumps I ought to assign tokens to. I mean as follows:

while                             // no token assigned
( expression )                    // take appropriate sequence of tokens
jump back if not met              // insert a conditional jump token - a jump to expression checking
statement                         // take appropriate sequence of tokens
jump unconditionally              // insert a token to jump to expression checking

However neither the expression length, nor the statement length is not known, I can record the address the ‘while’ keyword is entered, so last jump can be calculated. But what will provoke Bison to insert a conditional jump token?

  • 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-16T15:02:08+00:00Added an answer on June 16, 2026 at 3:02 pm

    It’s not clear what you are trying to do exactly but a while statement is often “compiled” into something like

    loop:
    .. evaluate condition ..
    JUMPC out
    .. body ..
    JUMP loop
    out:
    

    What I usually did with interpreters I wrote is to have a phase that solves all labels after the parsing so that just jumps and labels with dummy names are emitted while parsing the syntax tree, then it’s possible to caculate the address of each label and adjust all of them, so for example you would have

    510-580 .. evaluate condition ..
    590 JUMPC 820
    600-800 .. body ..
    810 JUMP 510
    820
    

    This can be done easily by using also a simple std::map to store all the labels so that after generating the parsing tree you just go through the code, computes all addresses of labels (which disappears from the AST) and then insert correct addresses in the jumps.

    Just to make clearer what I usually do:

    //parser.y
    
    while_stat:
      KW_WHILE T_LPAREN exp T_RPAREN block { $$ = new ASTWhileStat($3, $5); }
    ;
    
    //ASTWhileStat.h
    
    class ASTWhileStat : public ASTNode
    {
      ASTNode *m_condition;
      ASTNode *m_body;
    
    public:
      ASTWhileStat(ASTNode *condition, ASTNode *body) {
        m_condition = condition;
        m_body = body;
      }
    
      ASTWhileStat(const ASTWhileStat &other) {
        m_condition = other.m_condition;
        m_body = other.m_body;
      }
    
      ASTWhileStat &operator= (const ASTWhileStat &other) {
        if (&other != this) {
          m_condition = other.m_condition;
          m_body = other.m_body;
        }
    
        return *this;
      }
    
      ASTWhileStat *clone() {
        return new ASTWhileStat(*this);
      }
    
      u8 type() {
        return 0;
      }
    
      void populateSymbolTable() {
        m_condition->populateSymbolTable();
        if (m_body)
          m_body->populateSymbolTable();
      }
    
      void generateASM()
      { 
        u32 sCounter = ASTNode::labelCounter;
        u32 eCounter = ++ASTNode::labelCounter;
        ++ASTNode::labelCounter;
    
        printf("while%u:\n", sCounter);
        m_condition->generateASM();
        printf("NOT\n");
        printf("JUMPC while%u\n", eCounter);
        m_body->generateASM();
        printf("JUMP while%u\n", sCounter);
        printf("while%u:\n", eCounter);
    
        ++labelCounter; 
      }
    }
    

    Now, in my case I generate ASM text output of a lower level language because I have then another parser which parses this sort of ASM code into binary but you can just join these two steps. What happens in the ASM parser is the following:

    label:
      STRING T_COLON { ASSEMBLER->addLabelHere($1); }
    ;
    

    That does:

    void Assembler::addLabelHere(string str) {
      printf("[ASSEMBLER] Added label %s at %u\n",str.c_str(),curPos);
      labels[str] = curPos;
    }
    

    This because the assembler knows the internal state of the assembling code and knows the current offset from the beginning of the program.

    At the end of assembling phase all labels are solved:

    bool Assembler::solveLables()
    {
      map<string,u32>::iterator it, it2;
    
      for (it = jumps.begin(); it != jumps.end(); ++it)
      {
        it2 = labels.find((*it).first);
    
        if (it2 == labels.end())
        {
          printf("[ASSEMBLER] Unsolved label \"%s\"!\n", (*it).first.c_str());
          return false;
        }
    
        u32 address = (*it2).second;
        u32 pos = (*it).second;
    
        printf("[ASSEMBLER] Solved jump at %u with address %u\n",pos,address);
    
        memcpy(&code[pos], &address, sizeof(u32));
      }
    
      return true;
    }
    

    As you can see I brutally copy the address of the jump inside some bytes that I reserved when I assembled JUMP calls.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am doing a simple coin flipping experiment for class that involves flipping a

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.