Compiler construction in4303 answers

Similar documents
Compiler construction in4303 lecture 3

Compiler construction 2002 week 5

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Compiler construction in4020 lecture 5

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Compiler construction 2002 week 2

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Compiler construction in4303 lecture 9

Compiler construction lecture 3

More answers to exercises

IN4305 Engineering project Compiler construction

Compiler construction lecture 1

Compiler construction 2005 lecture 5

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE 401 Midterm Exam Sample Solution 2/11/15

Compiler construction in4020 course 2006

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

CS 314 Principles of Programming Languages. Lecture 9

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

A Simple Syntax-Directed Translator

Chapter 4. Lexical and Syntax Analysis

CS606- compiler instruction Solved MCQS From Midterm Papers

Compiler construction in4303 lecture 8

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

4. Lexical and Syntax Analysis

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

4. Lexical and Syntax Analysis

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

LECTURE 7. Lex and Intro to Parsing

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

CSCE 531 Spring 2009 Final Exam

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Question Bank. 10CS63:Compiler Design

Question Points Score

CSCI312 Principles of Programming Languages!

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Syntax Analysis Part IV

CS 314 Principles of Programming Languages. Lecture 3

LECTURE 11. Semantic Analysis and Yacc

CS 314 Principles of Programming Languages

Fundamental of Programming (C)

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

Lecture Compiler Middle-End

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Syntax-Directed Translation

Time : 1 Hour Max Marks : 30

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Monday, September 13, Parsers

Lexical Considerations

Compiler Design Aug 1996

1 Lexical Considerations

CS 406/534 Compiler Construction Putting It All Together

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

CMSC330 Spring 2014 Midterm 2 Solutions

Appendix Set Notation and Concepts

Context-free grammars

Wednesday, August 31, Parsers

Compiler Construction

Problem Score Max Score 1 Syntax directed translation & type

CS5363 Final Review. cs5363 1

Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Introduction; Parsing LL Grammars

Formal Languages and Compilers Lecture VI: Lexical Analysis

Using an LALR(1) Parser Generator

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Compiler Construction: Parsing

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

CSE P 501 Exam Sample Solution 12/1/11

Compilers. Predictive Parsing. Alex Aiken


Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Programmiersprachen (Programming Languages)

Fundamentals of Programming

Principles of Programming Languages

4. An interpreter is a program that

Compiler Construction

UNIVERSITETET I OSLO

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

Parsing How parser works?

Class Information ANNOUCEMENTS

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

Syntax-Directed Translation. Lecture 14

Lexical and Syntax Analysis

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

Transcription:

Compiler construction in4303 answers Koen Langendoen Delft University of Technology The Netherlands

Compiler construction in4303 lecture 1 Introduction Lexical analysis by hand Chapter 1 2.15

AST exercise (5 min.) expression grammar expression expression + term expression - term term term term * factor term / factor factor factor identifier constant ( expression ) example expression b*b (4*a*c) draw parse tree and AST

answer parse tree: b*b (4*a*c) expression expression - term term factor term * factor expression ( ) factor identifier identifier b 4*a*c b

Exercise (5 min.) write down regular descriptions for the following descriptions: an integral number is a non-zero sequence of digits optionally followed by a letter denoting the base class (b for binary and o for octal). a fixed-point number is an (optional) sequence of digits followed by a dot (. ) followed by a sequence of digits. an identifier is a sequence of letters and digits; the first character must be a letter. The underscore _ counts as a letter, but may not be used as the first or last character.

Answers base [bo] integral_number digit+ base? dot [.] fixed_point_number digit* dot digit+ letter [a-za-z] digit [0-9] underscore [_] letter_or_digit letter digit letter_or_digit_or_und letter_or_digit underscore identifier letter (letter_or_digit_or_und* letter_or_digit)?

Compiler construction in4303 lecture 2 Automatic lexical analysis Chapter 2.1.6-2.1.13

FSA exercise (6 min.) draw an FSA to recognize integers base [bo] integer digit+ base? draw an FSA to recognize the regular expression (a b)*bab

Answers integer S0 digit S1 digit [bo] S2 (a b)*bab a a b S0 b S1 a S2 b S3 a b

Exercise FSA construction (7 min.) draw the FSA (with item sets) for recognizing an identifier: identifier letter (letter_or_digit_or_und* letter_or_digit)? extend the above FSA to recognize the keyword if as well. if i f

Answers LD S0 ID L ((LDU)* (LD))? L \ { i } i LD \ { f } f S3 U ID L ((LDU)* (LD))? ID L (( LDU)* (LD))? LD S4 S1 ID L ((LDU)* (LD))? U U LD U S2 ID L (( LDU)* (LD))? ID L ((LDU)* ( LD))? accepting states S1, S3, and S4

Compiler construction in4303 lecture 3 Top-down parsing Chapter 2.2-2.2.4

Exercise (4 min.) determine the FIRST set of the nonterminals in our expression grammar input expression EOF expression term rest_expression term IDENTIFIER ( expression ) rest_expression + expression ε and, for this grammar S A B A A a ε B B b b

Answers FIRST(input) = { IDENT, ( } FIRST(expression) = { IDENT, ( } FIRST(term) = { IDENT, ( } FIRST(rest_expression) = { +, ε } FIRST(S) = { a, b } FIRST(A) = { a, ε } FIRST(B) = { b }

Exercise (4 min.) determine the FIRST set of the nonterminals in our expression grammar input expression EOF expression term rest_expression term IDENTIFIER ( expression ) rest_expression + expression ε and, for this grammar S A B A A a ε B B b b

Answers FIRST(input) = { IDENT, ( } FIRST(expression) = { IDENT, ( } FIRST(term) = { IDENT, ( } FIRST(rest_expression) = { +, ε } FIRST(S) = { a, b } FIRST(A) = { a, ε } FIRST(B) = { b }

Exercise (7 min.) make the following grammar LL(1) expression expression + term expression - term term term term * factor term / factor factor factor ( expression ) func-call identifier constant func-call identifier ( expr-list? ) expr-list expression (, expression)* and what about S if E then S (else S)?

Answers substitution F ( E ) ID ( expr-list? ) ID constant left factoring E E( + - )T T T T ( * / ) F F F ( E ) ID ( ( expr-list? ) )? constant left recursion removal E T (( + - ) T )* T F (( * / ) F )* if-then-else grammar is ambiguous

Compiler construction in4303 lecture 4 Bottom-up parsing Chapter 2.2.5

Exercise (8 min.) complete the transition diagram for the LR(0) automaton can you think of a single input expression that causes all states to be used? If yes, give an example. If no, explain.

Answers (fig 2.89) S0 Z E $ E E + T E T T ( S2 E T T S7 T ( E ) E E + T E T T i T ( E ) E i S1 T i i ( i T i T ( E ) E ( S3 Z E $ E E + T + S4 E E + T T i T ( E ) + S8 T ( E ) E E + T $ T ) S6 S5 S9 Z E $ E E + T T ( E )

Answers The following expressions exercise all states ( i + i ) $ ( i ) + I $ i + ( i ) $

Exercise (6 min.) derive the SLR(1) ACTION/GOTO table (with shift-reduce conflict) for the grammar: S A x b A a A b x

Answers state 0 1 2 3 4 5 6 7 stack symbol / look-ahead token a b x $ A s4 s1 s3 s2/r4 r4 r2 r1 s4 s5 s6 r4 r4 s7 r3 r3 1: S A 2: S x b 3: A a A b 4: A x FOLLOW(S) = {$} FOLLOW(A) = {$,b}

Compiler construction in4303 lecture 5 Semantic analysis Assignment #1 Chapter 6.1

Exercise (3 min.) complete the table expression construct constant variable identifier (non-variable) &lvalue *rvalue V[rvalue] rvalue + rvalue lvalue = rvalue result kind (lvalue/rvalue) rvalue lvalue rvalue rvalue lvalue V rvalue rvalue V stands for lvalue or rvalue

Answers complete the table expression construct constant variable identifier (non-variable) &lvalue *rvalue V[rvalue] rvalue + rvalue lvalue = rvalue result kind (lvalue/rvalue) rvalue lvalue rvalue rvalue lvalue V rvalue rvalue V stands for lvalue or rvalue

Exercise (5 min.) multiple lines: %% %token DIGIT %token REG %right '=' lines: %left '+' line %left '*' lines line %% ; line expr : expr REG '=' '\n' expr { printf("%d\n",?? $1);} } ; expr '+' expr { $$ = $1 + $3;} expr : expr '*' '+' expr { $$ = $1 + * $3;} '(' expr expr '*' ')' expr { $$ = $2;} $1 * $3;} '(' REG expr ')' { $$ = $2;}?? ;} DIGIT ; %% Extend the interpreter to a desk calculator with registers named a z. Example input: v=3*(w+4)

Answers %{ int reg[26]; %} %token DIGIT %token REG %right '=' %left '+' %left '*' %% expr : REG '=' expr { $$ = reg[$1] = $3;} expr '+' expr { $$ = $1 + $3;} expr '*' expr { $$ = $1 * $3;} '(' expr ')' { $$ = $2;} REG { $$ = reg[$1];} DIGIT ; %%

Answers %% yylex() { int c = getchar(); } if (isdigit(c)) { yylval = c - '0'; return DIGIT; } else if ('a' <= c && c <= 'z') { yylval = c - 'a'; return REG; } return c;

Exercise (5 min.) eval( Expr *e) {??? } write the code to evaluate an expression

Answers eval( Expr *e) { static int reg[26]; switch (e->type) { case EXPR_REG: return reg[e->reg]; case EXPR_DIG: return e->dig; write the code to evaluate an expression case EXPR_BIN: switch (e->op) { case '=': return reg[e->reg] = eval(e->right); case '+': return eval(e->left) + eval(e->right); case '*': return eval(e->left) * eval(e->right); } } }

Compiler construction in4303 lecture 6 AST processing attribute grammars Chapter 3.1

Exercise (5 min.) draw the other dependency graphs for the integral-number attribute grammar

Answers base Digit_Seq value base Digit_Seq value base Digit_Seq value base Digit value base Digit value base Digit value Base_Tag base Base_Tag base O D DIGIT repr

Exercise (4 min.) WHILE Number.value is not set: walk number(number); how many tree walks are necessary to evaluate the attributes of the AST representing the octal number 13O? how many for 1234D?

Answers any integral number can be evaluated with two walks

Compiler construction in4303 lecture 7 AST processing manual methods Chapter 3.2

Exercise (5 min.) draw the control flow graph for while C do S od propagate initial stack when C represents y>x and S stands for x=7 y x 5

Answers y x 5 b WHILE condition y x 5 BODY y x 5 y x 5 7 ELIHW y x 5 y x 5

Exercise (5 min.) + Answers determine the KILL and GEN sets for the property V is live here for the following statements statement S v = a b v = M[i] M[i] = v f(a 1,...,a n ) v = f(a 1,...,a n ) if a>b then goto L 1 else goto L 2 L: goto L KILL GEN {v} {a,b} \ KILL[S] {v} {i} \ KILL[S] {} {i,v} {} {a 1,...,a n } {v} {a 1,...,a n } \ KILL[S] {} {a,b} {} {} {} {}

Exercise (7 min.) draw the control-flow graph for the following code fragment double average(int n, double v[]) { int i; double sum = 0.0; } for (i=0; i<n; i++) { sum += v[i]; } return sum/n; perform backwards live analysis using the KILL and GEN sets from the previous exercise

Answers live: n 1 sum = 0.0; i = 0; OUT(N) = M successor[n] IN(M) IN(N) = OUT(N) \ KILL(N) GEN(N) live: n, i, sum statement KILL GEN 2 if (i<n) 1 sum, i live: n, i, sum live: n, i, sum 2 i, n 3 sum += v[i]; i++; 3 4 sum, i sum, i sum, n live: n, sum 4 return sum/n;

Answers process nodes top to bottom (from 0 to 4) stat em ent KILL GEN iteration 1 OUT IN iteration 2 OUT IN iteration 3 OUT IN 0 n 1 sum, i i, n n i, n, sum n 2 i, n i, n i, n, sum i, n, sum i, n, sum i, n, sum 3 sum, i sum, i i, n i, n, sum i, n, sum i, n, sum i, n, sum i, n, sum 4 sum, n n, sum n, sum n, sum processing nodes in reverse (from 4 to 0) requires only two iterations

Compiler construction in4303 lecture 8 Interpretation Simple code generation Chapter 4 4.2.4

Exercise (5 min.) generate code for the expression - b*b 4*a*c b * * b 4 * on a register machine with 32 registers numbered R1.. R32 a c

Answers Load_Mem b, R1 Load_Mem b, R2 Mul_Reg R2, R1 Load_Const 4, R2 Load_Mem a, R3 Load_Mem c, R4 Mul_Reg R4, R3 Mul_Reg R3, R2 Sub_Reg R2, R1

Exercise (4 min.) determine the number of registers needed to evaluate the expression a + a*2 + a*(b+3)

Answers + 3 a 1 + 3 * * 2 2 a 2 1 1 a 1 + 2 three registers are needed but only two if you apply left-factoring! a + a*2 + a*(b+3) = a*(b+6) b 3 1 1

Compiler construction in4303 lecture 9 Code generation Chapter 4.2.5, 4.2.7, 4.2.11 4.3

Exercise (7 (5 min.) given the code fragment x := a*a + 2*a*b + b*b; y := a*a 2*a*b + b*b; draw the dependency graph before and after common subexpression elimination.

Answers dependency graph after CSE x y + - * * a * b 2

Exercise (7 min.) { int tmp_2ab = 2*a*b; int tmp_aa = a*a; int tmp_bb = b*b; } x := tmp_aa + tmp_2ab + tmp_bb; y := tmp_aa - tmp_2ab + tmp_bb; given that a and b are live on entry and dead on exit, and that x and y are live on exit: (a) construct the register interference graph (b) color the graph; how many register are needed?

Answers a tmp_2ab x y b tmp_aa tmp_bb 4 registers are needed

Compiler construction in4303 lecture 10 Imperative & OO languages: context handling Chapter 6.2

Exercise (4 min.) Give the method tables for Rectangle and Square abstract class Shape { boolean IsShape() {return true;} boolean IsRectangle() {return false;} boolean IsSquare() {return false;} abstract double SurfaceArea(); } class Rectangle extends Shape { double SurfaceArea {... } boolean IsRectangle() {return true;} } class Square extends Rectangle { boolean IsSquare() {return true;} }

Answers Method table for Rectangle Method table for Square IsShape_Shape_Shape IsShape_Shape_Shape IsRectangle_Shape_Rectangle IsRectangle_Shape_Rectangle IsSquare_Shape_Shape IsSquare_Shape_Square SurfaceArea_Shape_Rectangle SurfaceArea_Shape_Rectangle

Exercise (4 min.) given an object e of class E, give the compiled code for the calls e.m1() e.m3() e.m4()

Answers e.m1() (*(e->dispatch_table[0]))((class_c *) e) e.m3() (*(e->dispatch_table[2]))( (class_d *)((char *)e + sizeof(class_c))) e.m4() (*(e->dispatch_table[3]))( (class_d *)((char *)e + sizeof(class_c)))

Compiler construction in4303 lecture 11 Imperative & OO languages: routines & control flow Chapter 6.3-6.4

Exercise (5 min.) Given the GNU C routine: void A(int a) { void B(int b) { void C(void) { printf( C called, a = %d\n, a); } if (b == 0) C() else B(b-1); } B(a); } a) draw the stack that results from the call A(2) b) how does C access the parameter (a) from A?

Answers dynamic links static links A 2 B 2 B 1 B 0 C FP->static_link->static_link->param[#a]

Exercise (3 min.) while -statement WHILE condition DO statement END WHILE give a translation schema for while statements

Answers while -statement WHILE condition DO statement END WHILE test_label: GOTO test_label; loop_label: Boolean control code( condition, No_label, end_label) Code statement( statement) test_label: GOTO test_label; end_label: Boolean control code( condition, loop_label, No_label)

Compiler construction in4303 lecture 12 Memory Management Chapter 5

Exercise (5 min.) give the pseudo code for free() when using free lists indexed by size.

Answers PROCEDURE Free (Block pointer): SET Chunk pointer TO Block pointer Admin size; SET Index TO 2 log(chunk pointer.size); IF Index <= 10: SET Chunk pointer.next TO Free list[index]; SET Free list[index] TO Chunk pointer; ELSE SET Chunk pointer.free TO True; // Coalesce subsequent free chunks

Compiler construction in4303 lecture 13 Functional programming Chapter 7

Exercise (5 min.) infer the polymorphic type of the following higher-order function: filter f [] = [] filter f (x:xs) = if not(f x) then filter f xs else x : (filter f xs)

Answers filter f [] = [] filter f (x:xs) = if not(f x) then filter f xs else x : (filter f xs) filter :: t 1 -> t 2 -> t 3 filter f [] = [] filter :: t 1 -> [a] -> [b] filter f (x:xs) = if not(f x) then filter f xs else x : (filter f xs) x :: a f :: a -> Bool filter :: (a -> Bool) -> [a] -> [a]

Exercise (6 min.) infer the strict arguments of the following recursive function: g x y 0 = x g x y z = g y x (z-1) how many iterations are needed?

Answers g x y 0 = x g x y z = g y x (z-1) g x y z = if z == 0 then x else g y x (z-1) step 1 2 3 4 assumption {x,y,z} {x,z} {z} result {x,z} {z} {z}

Compiler construction in4303 lecture 14 Logic programs Chapter 8

Exercise (5 min.) Specify Prolog rules for the binary relations offspring (nakomeling) spouse (echtgenoot) using the parent relation. The bait hides the hook parent(dick,koen). parent(lia,koen). parent(koen,allard). parent(koen,linde). parent(koen,merel).

Answers offspring(x,y) :- parent(x,y). offspring(x,y) :- parent(x,z), offspring(z,y). spouse(x,y) :- parent(x,z), parent(y,z), X \= Y,!. X \= Y to rule out spouse(koen,koen)! to rule out duplicates (multiple children)