Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 2210: Compiler Design Homework #3

Due Time: 11:59pm 11/6/2023

1.  (10+10=20 points) In modern program languages that support multiple levels of scopes, we may need to create a sub symbol table for each scope during compilation. (1) If we finish compiling a scope, do we delete its corresponding scope’s sub symbol table?  (2) How do we support static scoping using a stack?

2.  (5+10+10=25 poins) Assume we store arrays using row major in byte-addressed memory, and we have base=low1=low2= low3  = 0.  Given the following array declarations, please compute the corresponding addresses for the array items. Each character occupies one byte in memory.

char arr1[111];

char arr2[111][222];

char arr3[111][222][333];

...

(1) arr1[5];       (2) arr2[5][5];       (3) arr3[5][5][5];

3.  (5+5+5=15 points) Briefly define the following terminologies. You can use examples to simplify your explanation. (1) Synthesized attributes; (2) Inherited attributes; (3) L-attributed grammar.

4.  (10+10=20 points) Assume we use syntax directed translation to perform semantic analysis, and the syntax analysis scheme that we choose is LALR parsing. We use the following simplified version of attribute evaluation to replace the more detailed evaluation, as shown next.  In the exam, we will use

the simplified version again.

The simplified version:

X ! A B C D

Y ! A Y C Z D

C  ! c f / * c . a t t r 0   =  f 1 (A . a1 ) */ g D ! d f / * d. a t t r 0 = f 2 (C . a2 ) */ g

The detailed version:

X  ! A B fC . i =A . a1 g C fD . i =C . a2 g D

Y  ! A Y fC . i =A . a1 g C Z fD . i =C . a2 g D C  ! c f c . a t tr 0   =   f 1 ( C . i )   g

D  ! d f d . a t tr 0 = f2 (D . i )   g

(1) Do we need to introduce markers to assist attribute evaluation.  If yes, how many markers do we need to introduce. Why?

(2) Convert the attribute evaluation to syntax directed translation scheme,i.e., make it compatible with LALR parsing.

5.  (5+15=20 points) After reading the source code of Project 3, please answer the following questions.

a)  Is the approach we adopted in project 3 a syntax-directed translation scheme?

b)  How does the code use openBlock() and closeBlock() functions to manage the nested function declarations.