201250093 谭子悦

Overview

The two major tasks in Lab 3 are to implement type checking and symbol renaming for SysY.

Type Checking

To implement type checking, we first need to maintain a symbol table with multiple scopes.

And then, with the symbol table in hand, we can check the type of symbol when traversing through non-terminal nodes.

My implementation uses three categories of classes and interfaces (Scope, Symbol, Type) to implement the symbol table, and uses the visitor pattern in SemanticVisitor to iterate through the code.

Symbol Renaming

Our program needs to rename the symbol according to the provided line number and column number.

My first idea was to modify the SyntaxVisitor and let it find the target symbol in the first run, then rename all the occurrences of it in the second run. But then I found that it's hard for SyntaxVisitor to recognize either two identities refer to the same symbol because it has no knowledge of either the symbol table or the scope layer.

Henceforth, in my later implementation, the SemanticVisitor will:

Then, the SyntaxVisitor will have the SemanticVisitor as its member variable. Each time it meets an identity terminal $I_k$, it will check whether $I_k \in f(S')$. If so, then it will change its name to the new name.

Mistakes

Mistake 1: Conditions

I only got a score of 1495 after doing all the stuff above. Then I reviewed my code carefully and found several mistakes, for example:

  1. I forgot to deal with the case where the return statement returns nothing, and hence my program will crash if there is one.
  2. I forgot to verify the condition expressions, where condition operators also require the types of their operands to be integers.
  3. I forgot to deal with the case where the calling function that has parameters without any argument.