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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:52:10+00:00 2026-05-21T08:52:10+00:00

I am writing a compiler for a toy OO language. I am writing it

  • 0

I am writing a compiler for a toy OO language. I am writing it in C, using Flex and Bison.

Consider the following syntax:

class MyClass {
    int m_n;

    void MyFunc(int b) {
        m_n = 5;
        m_p = b;
    }   

    int m_p;
}

My current code will complain that in MyFunc, m_p has not yet been declared (with good reason). So, I came to the conclusion that I need a multi-pass parsing technique – something along the lines of:

1st pass – process variable declarations

2nd pass – process function definitions

First – is this the best way to solve the issue? Are there other methods that I should look into? Second – if this is a favorable solution, would I go about implementing it with a re-entrant lexer/parser?

Thanks

  • 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-05-21T08:52:11+00:00Added an answer on May 21, 2026 at 8:52 am

    I recently wrote a compiler for an OO language, we had multiple passes (depending on the complexity of the language of course):

    1. Collect all Classes
    2. Build up superclass hierarchy
    3. Collect all methods and fields
    4. Collect variables inside methods etc.

    There are reasons why we had to split up the whole process into 4 passes:

    1. You can’t build up the superclass hierarchy when not all classes have been processed yet (led to 2. pass)
    2. You can’t validate inherited methods (return value, parameters etc.) when the superclass is unknown (led to 2. pass)
    3. You can’t process variables when not all fields have been collected yet (led to 4. pass)

    You can leave out the second pass if you don’t have inheritance in your language of course.

    When I look at it now, it should’ve been possible to merge pass 2 and 3 as all data should be available for pass 3.

    The way we implemented it was just by walking through the AST and annotating it with the required symbol tables.

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

Sidebar

Related Questions

I'm writing a compiler for a small language, and my Parser class is currently
We're writing a compiler for Al Aho's compilers class, and we're considering the following
I'm writing a demo compiler for a toy programming language in C. What problems
I am experimenting with writing a toy compiler in ocaml. Currently, I am trying
I am writing a compiler in Python using Eclipse with PyDev. I've come to
I'm writing a compiler in ocaml, using ocamllex/yacc. Things are going well, but I've
I'm working on writing a compiler for a language running on .net and one
I would like to write a compiler for a toy-language for Java. I would
I'm currently writing a toy compiler targeting Java bytecode in the translation. I would
I am writing a compiler for a c-like language. Currently, the compiler supports arrays

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.