开发者

Flex/Bison Multi-pass Class Parsing

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 c开发者_JAVA百科urrent 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜