|
| 1 | +In this milestone, you extend the project to perform semantic |
| 2 | +analysis. The goal is to convert your program into an Intermediate |
| 3 | +Representation to be used by later stages (Final |
| 4 | +code generation, optimization, etc.). Once again, you will use |
| 5 | +"actions" in your lexer/parser to achieve the desired outcome. |
| 6 | + |
| 7 | +1. As you process the program, the information contained in the |
| 8 | + declarations is stored in the symbol table. This information |
| 9 | + includes the types of variables and functions and the sizes |
| 10 | + and offsets of variables. |
| 11 | + |
| 12 | +2. When you process the non-declarative part, then two things |
| 13 | + happen: |
| 14 | + |
| 15 | +(a) The information in the symbol table is used to ensure that |
| 16 | + the variables are used within the scope of their declarations |
| 17 | + and are used in a type-correct manner. If they are not, then |
| 18 | + the program is rejected. |
| 19 | + |
| 20 | +(b) If the program is syntactically and semantically (type and |
| 21 | + scope) correct, 3-address code is generated. This 3-address |
| 22 | + code structure (3AC) along with the information in the symbol |
| 23 | + table will be used to generate code. |
| 24 | + |
| 25 | +Note: If you prefer some other IR over 3AC, let me know. In that case, |
| 26 | + assume 3AC refers to your IR below. |
| 27 | +In this assignment, we shall construct the symbol table, do the |
| 28 | +semantic checks and then generate 3AC. You will need to define |
| 29 | +the 3AC instructions corresponding to various constructs in the |
| 30 | +program. |
| 31 | + |
| 32 | +You can create the following two-level symbol table structure. |
| 33 | +Again feel free to adapt to nitty gritty of your language. |
| 34 | + |
| 35 | +(a) A global symbol table (GST) that |
| 36 | +maps function names to their local symbol tables. |
| 37 | + |
| 38 | +(b) A local symbol table for every function that contains the |
| 39 | +relevant information for the parameters and the local variables |
| 40 | +of the function. |
| 41 | + |
| 42 | +Make sure your symbol table is "extensible" since you might |
| 43 | +discover the need to store new information as the project |
| 44 | +progresses. |
| 45 | + |
| 46 | +Along with the construction of the symbol table, process the |
| 47 | +non-declarative part of the program to create the 3AC. The 3AC |
| 48 | +should be kept in some data structure in memory (List, Vector |
| 49 | +etc.). Perform semantic checks to ensure that semantically |
| 50 | +incorrect programs are rejected. |
| 51 | + |
| 52 | +Your output for good programs will consist of: |
| 53 | + |
| 54 | +(a) A dump of the symbol table of each function as a CSV file |
| 55 | + (the columns of CSV can be of your choice), and |
| 56 | + |
| 57 | +(b) A dump of the 3AC of the functions in text format. |
| 58 | + |
| 59 | +For bad programs, the output should mention the error that caused the |
| 60 | + program to be rejected (not just "error at line |
| 61 | + YYY"). Acceptable errors include (but not limited to): |
| 62 | + |
| 63 | +"Type mismatch in line 72", |
| 64 | +"Incompatible operator + with operand of type string", |
| 65 | +"Undeclared variable on line 38". |
| 66 | + |
| 67 | +Here are some more things that you have to keep in mind: |
| 68 | + |
| 69 | +1. Operator/Function disambiguation: This will be a good time to |
| 70 | + pin down the exact operator function that will be used in an |
| 71 | + expression. For example, for x + y,: (a) If x and y are both |
| 72 | + ints, resolve the + to (say) +int. (b) If x and y are both |
| 73 | + floats, resolve the + to +float. |
| 74 | + |
| 75 | +2. Type Casting: Continuing with the above example of x + y, if x |
| 76 | + is an int and y is a float, cast x to a float and resolve the |
| 77 | + + to +float. The 3AC will be like: |
| 78 | + t1 = cast-to-float x |
| 79 | + t2 = t1 +float y. |
| 80 | + |
| 81 | +3. Here is an (incomplete) list of errors that you have to look |
| 82 | + out for: |
| 83 | + |
| 84 | +(a) All forms of type errors. Examples: Array index not being an |
| 85 | + integer. Variables being declared as being the void type. |
| 86 | + |
| 87 | +(b) All form of scoping errors. |
| 88 | + |
| 89 | +(c) Non-context-free restrictions on the language. For example, |
| 90 | + an array indexed with more indices than its dimension and |
| 91 | + functions being passed with less than the required number of |
| 92 | + parameters. |
0 commit comments