CS5363 Final Exam
Dec 14, 2010
1. (7pts) Construct a regular expression for each of the following sets of strings.
(a) Identiers accepted by the C language (e.g., My id, var7).
(b) All strings over alphabet cfw_ a, b, c that do not contain substring aa.
Homework 2 Solution: CFG and Parsing
1. (10pts) Write a context-free grammar describing the syntax of variable declarations
in the C language. In particular, each variable declaration should start with a type,
followed by a sequence of variables and array
Homework 2: CFG and Parsing
Due before class Sep 28, 2010
1. (10pts) Write a context-free grammar describing the syntax of variable declarations
in the C language. In particular, each variable declaration should start with a type,
followed by a sequence o
Homework 1: Syntax descriptions
Due before class on Sep 9, 2010
1. (15pts) Construct a regular expression for each of the following languages.
(a) Integers that can divide 5. For example, 125, -35, 20 are all valid strings in the
language, but 59 and 8 ar
Exercise 3: IR
1. Suppose we have the following abstract grammar
E := E + E | E - E | E * E | var | val
Write a translation scheme that convert AST to stack-machine and three-address code.
Show the translation results for x * y - a + b;
1
Exercise 2: Semantics Analysis
1. Given the following grammar for a binary number generator
S := L
L := L B | B
B := 0 | 1
(a) Compute the value of each resulting number. E.g., if s . 1101, then the
value of s is 13
(b) Compute the contribution of each di
Exercise 1: Syntax descriptions
1. Construct a regular expression for each of the following languages.
(a) All strings over alphabet cfw_ a, b, ., z that do not contain the substring ab
(b) All strings over alphabet cfw_ a, b, ., z that do not contain the
tcfw_oy*otrton\( +
I tc,)
S
+
2
Ool1s\ rroos\oo\rr
A. exorq\r- Stt
u
0O I
\
1r
"
S+
s+oo\s+ooll\l
oo,r1\)$f\oo\tr
&B
L De!,t3t*J ogordo'
S
A
\
s
00 t\
\-L
t1
AST
+txe.
po[\J'(
Tt"'
at O*l^en eV4*9\e AS-T is
S+ cJot\s :)
(ro\
\
\(to
t4 ob\ \(t0
s=)
\'4
oo\
Hor"ewot'l<1
: S.l"hx dcacniplior\A
1.(q: (-rE) Lr-ql Lo-gl* (rterst?t9)
A o$hcfw_tX r<\oxed
OWh \aadr.'S Z0Y.6S
(-
Can be
lt) Lo-gl* (rlrl5l?te)
2-.
.
(olutc)x u (olulc)* u (otbl<)x
(c>
NFe
DFA
(rao: I oo*
J"-
( r\o)x
r
[^
orl)* o *
is
is
J
r
t)
(r ls\sl
Homework 6: Machine Independent Optimization
Due Nov 23, 2011
Suppose we have the following C code.
float cur[500];
bool foo(float *arr, int size, float v)
cfw_
float elem1, elem2, res;
bool change;
int i;
i = 0; change=false; res = v * v;
while (i < size
Homework 6: Machine Independent Optimization
Solution
1. Eliminate useless code in the given code. Build a control-ow graph for function foo
after the optimization.
float cur[500];
bool foo(float *arr, int size, float v)
cfw_
float elem1, elem2, res;
bool
Exercise 4: Procedure Abstractions
1. Draw the runtime stack of the following code after the call to h inside the body of g.
1: program main(input,output)
2:
var x : integar;
3:
function f(y : integer)
4:
begin
5:
f = (x + y) - 2
6:
end
7:
function g(func
Exercise 5: Code Generation And Dataow Analysis
1. Given the following C code.
void fee(int x, int y)
cfw_
int I = 0;
int z = x;
while (I < 100) cfw_
I = I + 1;
if (y < x) z = y;
A[I] = I;
(a) Translate the code to three-address machine code.
(b) Build
Exercise 6: Machine-independent Optimization
1. Given the following C code.
int A[100];
void fee(int x, int y)
cfw_
int I = 0, j = i;
int z = x + y, h =0;
while (I < 100) cfw_
I = I + 1;
if (y < x) j = z + y;
h = x + y;
A[I] = x + y;
return;
(a) Elimina
Project 3: Building a Translator/Compiler
Due the last day of class (Dec 5, 2010)
In this project, you are required to perform some program analysis or optimization to
the type-checked intermediate representation from project2 and then output the results
Project 2: Type checking and Intermediate Code Generation
Due Nov 11, 2010 (corrections may be made until nal)
In this project, you are required to apply type checking to the C code parsed by your
project1 and to generate an intermediate representation of
Project 1: syntax analysis (30pts)
Due Oct 14, 2010 (corrections may be made until nal)
In this project, you are required to build a scanner and a parser for a subset of the C
language (you may pick a dierent language other than C, but your language must
Homework 5: Dataow Analysis and Optimization
Due Nov 23, 2010
Suppose we have the following C code.
int lookahead;
int foo(int arg)
cfw_
int size, len, i, cur, res;
size = arg;
len = -1;
i = 0;
res = len;
while (i < size) cfw_
cur = lookahead + i;
if (len
CS5363
Homework 5 solutions
1.
Foo:
size := arg
len := -1
i := 0
res := len
B0:
If i < size goto B1
B1:
B4:
cur := lookahead + i
If len < cur goto B2
B2:
len := cur
B3:
cur := lookahead + i
cur := cur + len
res := res + len
i := i + 1
return res
2.
Foo:
s
Homework 4: Procedure and Data Abstraction(30pts)
Due Nov 4, 2010
1. For the following Pascal program, draw the set of activation records that are on the
stack just prior to the return from function Add. Include all entries in the ARs. Use
line numbers fo
Homework 4 - Solutions
1. Assuming the first add:
Main:
Caller ARP
AL
RA
RRA
Param input output
Local Var y=1.5
tmp
F3:
Caller ARP
AL
RA line 19
RRA
Param y=5
Local Var
tmp1
Apply:
Caller ARP
AL
RA line 15
RRA
Param g
Local Var x
tmp 3
Add:
Caller ARP
AL
Homework 3 : Attribute Grammar (30pts)
Due Oct 7, 2010
Suppose we have the following context-free grammar which accepts a list of variable
initializations.
Goal := single | Goal single
single := VAR "=" exp ";"
exp := VAR | INT | exp + exp
Here each V AR
Homework 3 : Attribute Grammar (30pts)
Due Oct 7, 2010
Suppose we have the following context-free grammar which accepts a list of variable
initializations.
Goal := single | Goal single
single := VAR "=" exp ";"
exp := VAR | INT | exp + exp
Here each V AR
Homework 1: Syntax descriptions
Due before class on Sep 9, 2010
1. (15pts) Construct a regular expression for each of the following languages. (a) Integers that can divide 5. For example, 125, -35, 20 are all valid strings in the language, but 59 and 8 ar
Homework 5: Dataow Analysis
Due Nov 9, 2011
Suppose we have the following C code.
float foo(float *arr, int size, float v)
cfw_
float cur[size], elem1, elem2, res;
bool change;
int i;
i = 0; change=false;
while (i < size) cfw_
elem1 = arr[i];
elem2 = elem
CS5363 Midterm Exam
Oct 12, 2011
1. (15pts) Answer the following questions, using a single sentence for each question.
(a) What are the key features of a functional programming language?
Create new variables instead of modifying existing ones; functions a
Lexical Analysis
Regular expressions and Finite
Automata
cs5363
1
Phases of compilation
Compilers
Read input program optimization translate into machine code
front end
Lexical
analysis
parsing
Characters
Words/strings
mid end
Semantic Code
gen
analysis
Se