Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction...

14
Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations NFA. Non-deterministic finite automaton DFA. Deterministic finite automaton General Remarks Please write legibly. When asked to find a solution using an algorithm, make sure to show how you constructed the solution, i.e. simply submitting the final result is not sufficient (e.g. Subset Construction Algorithm). Discussion of the tasks between the lecture participants is welcome, but plagiarism will lead to a score of 0 points. 1 Lexical analysis 1. Construct deterministic finite automata for the following regular expressions. (a) ((ab|ac)(c|a) + ) * a (b) a(a|b) * ( (c (d | )a * )| ) (c) ((ab) * c(ba| )) + 2. For the following regular expressions: Build a NFA by means of the Thompson Construction Algorithm. Transform the NFA into a DFA by means of the Subset Construction Algorithm. (a) (a | b) + c (b | ) (b) c(a|(bcb) * ) * (c) (ab) * a b? (d) 0 * 1 | 1 + (e) a(b | a) + (c| ) (f) (a * |(bc) + ) * $ 3. Integers must be enclosed by the hash sign (’#’) and consist of a sequence of numerics (’0’-’9’). A list of integers consists of an arbitrary number of comma separated integers and is surrounded by curly brackets. Valid lists Invalid lists {} {#1#,} {#1#} {1,2,3} {#02#,#0#,#080#} {#02##0##080#} 1

Transcript of Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction...

Page 1: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

Compiler Construction

Collection of exercises

Version May 15, 2017

Compiled on 15/05/2017 at 3:21pm

Abbreviations

NFA. Non-deterministic finite automatonDFA. Deterministic finite automaton

General Remarks

• Please write legibly.

• When asked to find a solution using an algorithm, make sure to show how you constructedthe solution, i.e. simply submitting the final result is not sufficient (e.g. Subset ConstructionAlgorithm).

• Discussion of the tasks between the lecture participants is welcome, but plagiarism will leadto a score of 0 points.

1 Lexical analysis

1. Construct deterministic finite automata for the following regular expressions.

(a) ( ( a b | a c ) (c | a)+ )∗ a

(b) a ( a | b)∗ ( (c (d | ε) a∗) | ε )

(c) ( ( a b )∗ c (b a | ε) )+

2. For the following regular expressions:

• Build a NFA by means of the Thompson Construction Algorithm.

• Transform the NFA into a DFA by means of the Subset Construction Algorithm.

(a) (a | b)+ c (b | ε )

(b) c ( a | ( b c b )∗ )∗

(c) ( a b )∗ a b?

(d) 0∗ 1 | 1+

(e) a ( b | a )+ ( c | ε )

(f) ( a∗ | ( b c )+ )∗ $

3. Integers must be enclosed by the hash sign (’#’) and consist of a sequence of numerics(’0’-’9’). A list of integers consists of an arbitrary number of comma separated integersand is surrounded by curly brackets.

Valid lists Invalid lists{} {#1#,}

{#1#} {1,2,3}

{#02#,#0#,#080#} {#02##0##080#}

1

Page 2: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

• Use regular expressions to describe integers and lists of integers.

• Construct an equivalent DFA for these regular expressions using

– the direct method exploiting the syntax tree and

– the indirect method by first constructing the NFA by means of the ThompsonConstruction Algorithm and then transforming the NFA into a DFA by means ofthe Subset Construction Algorithm.

• Compare the two resulting DFAs.

4. For the following NFA:

• Indicate an equivalent regular expression.

• Transform the NFA into a DFA by means of the Subset Construction Algorithm.

(a) NFA1

(b) NFA2

(c) NFA3

2

Page 3: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

(a) NFA1

(b) NFA2

+

- 11

0

0ε ε

ε

ε

ε

1

2

3

4

5

6 7

(c) NFA3

5. Real constants are defined in the SML standard as follows:

An integer constant (in decimal notation) is an optional negation symbol (~) fol-lowed by a non-empty sequence of decimal digits 0,. . . ,9. [. . . ] A real constantis an integer constant in decimal notation, either followed by a point (.) and oneor more decimal digits possibly followed by an exponent symbol (E or e) and aninteger constant in decimal notation or followed by an exponent symbol (E or e)and an integer constant in decimal notation; at least one of the optional partsmust occur, hence no integer constant is a real constant.

Valid real constants Invalid real constants0.7 23

3.32E5 .3

3E~7 4.E5

01.0 1E2.0

3

Page 4: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

• Define a regular expression that fits real constants.

• Construct an equivalent NFA by means of the Thompson Construction Algorithm.

• Simplify the resulting NFA.

• Transform the NFA into a DFA by means of the Subset Construction Algorithm.

6. The following regular expression describes the token filename:

filename → /? file (/ file)∗file → (char + ( char + )∗)|( .+ )char → x

Valid filename Invalid filename.././x/x x /../ x x

• Construct an equivalent NFA by means of the Thompson Construction Algorithm. Youare allowed to make simplifications.

• Transform the NFA into a DFA by means of the Subset Construction Algorithm.

4

Page 5: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

7. Non-deterministic finite automaton (NFA1) with 0 as initial state and 4 and 7 as final states:

ε

ε

1

0

1

0

ε

0

’.’

1

0

0

1

2

3

4 5

6

7

• Transform the NFA1 into a DFA by means of the Subset Construction Algorithm.

• Indicate an equivalent regular expression.

• Explain that language is defined with that regular expression / automata and giveexamples.

• Construct a non-deterministic finite automaton (NFA2) from the regular expressionby means of the Thompson Construction Algorithm. What are the main differencesbetween NFA1 and NFA2?

5

Page 6: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

2 Syntactic analysis

1. For the following grammars (with S as start symbol):

• Transform the grammar to LL(1) if necessary.

• Compute the FIRST and FOLLOW sets.

• Build the LL(1) parsing table.

(a) S → E

E → E op E

E → FE → id

F → E ( E )

(b) S → C

C → // TC → /* A */

T → char TT → cr

A → char TA → cr TA → ε

(c) S → B

B → AB → CA → id

C → ( LL )

LL → B LRLL → εLR → , B LRLR → ε

(d) S → S , S

S → FS → id RR → : num

R → εF → id ( S)

(e) S → T

T → @ ( T , T )

T → @ LL → id

L → num

2. Explain why the following grammar cannot be transformed into LL(1)!

Z → $Z$Z → %Z%Z → ε

6

Page 7: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

3. For the following grammars (with S as start symbol):

• Transform the grammar to LL(1) if necessary and build the LL(1) parsing table.

• Build the SLR(1) parsing table from the original (unmodified) grammar.

(a) Grammar:S → b W SS → b

W → W a

W → ε

(b) Grammar:S → ( LO )

S → atom

LO → S LRLO → εLR → , S LRLR → ε

(c) Grammar:S → CS → assign

C → if P S S fi

C → if P S fi

P → pred

(d) Grammar:S → S, SS → char

4. For the following grammars (with S as start symbol):

• Remove all left recursions and apply left factoring.

• Construct a Recursive Descent Parser for the transformed grammar.

• Build the LL(1) parsing table for the transformed grammar.

• Build the SLR(1) parsing table for the original (unmodified) grammar.

(a) Grammar:S → idList statListidList → identifier | idList , identifierstatList → stat | statList ; statstat → identifier := expression

| if expression then stat else stat| if expression then stat

expression → identifier + identifier

(b) Grammar:S → formulaformula → formula ⊃ expr | formula ≡ expr | exprexpr → expr ∨ term | termterm → term ∧ factor | factorfactor → ¬ factor | (formula) | ⊥

7

Page 8: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

5. For the following grammars (with S as start symbol):

• Compute the SLR(1) parsing table.

• If there are any shift-reduce conflicts resolve them with the listed assumptions.

• Show that the given expression can be parsed correctly with your SLR(1) parsing table.

(a) Grammar:S → S # SS → S % SS → char

Assumptions:

• # has a stronger binding than %.

• Equivalent operators are left associative.

Expression:char % char # char

(b) Grammar:S → S # SS → S % SS → ? SS → bn

Assumptions:

• ? has a stronger binding than #

• # has a stronger binding than %.

• Equivalent operators are left associative.

Expression:bn % bn # ? bn

(c) Grammar:S → S # SS → S : ES → EE → a

E → b

E → E EAssumptions:

• : has a stronger binding than #

• Equivalent operators are left associative.

Expression:a # b : b

(d) Grammar:S → LL → L | CL → CC → cmd ALAL → str ALAL → ε

Assumptions:

• Operators are left associative.

Expression:cmd str str | cmd

8

Page 9: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

3 Attributed grammars

1. Infix, postfix and prefix notation

(a) Extend the following grammar with attributes that allow to translate prefix expres-sions to postfix expressions. Give your attributes meaningful names and state whichattributes are synthesized and which are inherited. Further, show an example of atranslation using your attributed grammar.

S → EE → ( O E E )

E → id

E → num

O → + | -

Prefix Postfix(+(+1 2)3) ((1 2+)3+)

(+1(+2 3)) (1(2 3+)+)

(b) Write an attributed grammar that translates expressions in postfix notation into infix.Define a proper grammar for the postfix notation. Give your attributes meaningfulnames and state which attributes are synthesized and which are inherited. Further,show an example of a translation using your attributed grammar.

Postfix Infix2 3 - 4 * (2 - 3) * 4

2. Given the following grammar:

W → [ W ]

W → ε

(a) Write an attributed grammar that returns the amount of pairs of parentheses. Give yourattributes meaningful names and state which attributes are synthesized and which areinherited. Further, show an example of a translation using your attributed grammar.

(b) Build the SLR parsing table. Indicate where you have to apply the rules defined in (a).

(c) Implement the attributed grammar as a recursive function which returns the numberof pairs of parentheses.

(d) Show for both implementations (b) and (c) how the number of parentheses are computedfor the term [[]].

3. The following grammar, with S as start symbol, defines binary numbers. Write an attributedGrammar that computes the decimal number of a binary number. Give your attributes mean-ingful names and state which attributes are synthesized and which are inherited. Further,

9

Page 10: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

show an example of a translation using your attributed grammar.

S → OP BB OB OEOP → + | − | εBB → 0 RBB → 1 RR → 0 RR → 1 RR → εOB → . BBOB → εOE → E OP BBOE → ε

Binary number Decimal number101.00001 5.03125

+1E-1 0.5

-0.01 -0.25

4. For the following grammar G:

• Write an attributed grammar that translates expressions of the grammar G into As-sembler code. Give your attributes meaningful names and state which attributes aresynthesized and which are inherited. The following Assembler instructions are avail-able:ADD R1 , R2 R1 := R1 +R2

MUL R1 , R2 R1 := R1 ∗R2

MOV M , R R := memory(M)LOA N , R R := N (Directly sets the value of a register)Ri names a register, M names a location in the main memory, and N is a number.Assume that you have an infinite number of registers. The function nextRegister()

returns the next register.

• Show the transformation of the Expression E into Assembler code.

(a) Grammar G:E → (E)E → E + EE → E ∗ EE → id

E → num

Expression E:(1+(x*5))

(b) Grammar G:E → ( E E ∗ )E → ( E num ∗ ∗ )E → id

E → num

The operator ** denominates the power function, e.g. (2 3 **) means 23.

Expression E:(2(2 3**)*)

10

Page 11: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

(c) Grammar G:P → E : ALE → + E EE → id

E → num

AL→ AE ; ALAL→ εAE → id := num

A program (P) consists of an expression (E) and a list of value assignments (AL) forall variables in the expression. Note that the values of the variables have to be definedbefore the expression can be evaluated.

Expression E:+ a + b 3; a=2; b=3;

5. Nested lists

(a) Given the following grammar with S as start symbol. Write an attributed grammarthat returns the length of the outermost list. Give your attributes meaningful namesand state which attributes are synthesized and which are inherited. Further, show anexample of a translation using your attributed grammar.

S → ( E )

L → ( E )

L → atom

E → L RE → εR → , L RR → ε

List Result() 0(atom,(atom),(atom)) 3(atom,(atom,atom)) 2

(b) Given the following grammar with L as start symbol. The empty list is defined as nil.atom names any element of the list that cannot be further divided.

L → nil

L → atom

L → [ LR ]

LR → L , LRLR → L

Write an attributed grammar that returns the list of atoms that are element of thegiven depth. Give your attributes meaningful names and state which attributes aresynthesized and which are inherited. Further, show an example of a translation usingyour attributed grammar.Examples:

11

Page 12: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

Original list N Result list[nil] 0 [nil]

[atom] 0 [atom]

[atom] 1 [nil]

[[atom]] 1 [atom]

[[atom,[atom,atom]]] 1 [atom]

[[atom,[atom,nil],atom]] 1 [atom,atom]

[[nil,[atom,[nil]],nil]] 2 [atom]

(c) Write a grammar for the following type of lists: Each list starts with a number followedby : and the virtual list. A list starts with ( and ends with ). The elements of alist can be both atoms (atom) and lists. Ensure with attributes that the number atthe beginning of the list definition is equal to the number of atoms in the list andthe sub-lists. Give your attributes meaningful names and state which attributes aresynthesized and which are inherited. Further, show an example of a translation usingyour attributed grammar.Examples:

List Result3:((atome)((atom)atom)) Valid1:() Invalid0:() Valid

12

Page 13: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

(d) Extend the following grammar with attributes so that given expressions are evaluatedaccording to the following examples. Give your attributes meaningful names and statewhich attributes are synthesized and which are inherited. Further, show an example ofa translation using your attributed grammar.The given grammar describes an operatorthat should be applied to all elements of a list. The list may contain sublists (withouta preceding operator). The result should also be a list.

E → O LL→ ( LE LR )LR→ , LE LRLR→ εLE → num

LE → LO → inc | dec

Expression Evaluated expressioninc(1,(1,2),3) (2,(2,3),4)

dec(1,(1,2),3) (0,(0,1),2)

6. Given the following grammar with S as start symbol which defines binary trees:

S → T LT → char tree

T → int tree

L → char

L → int

L → tree ( L , L )

Write an attributed grammar that returns the number of char or int according to the treetype. Give your attributes meaningful names and state which attributes are synthesizedand which are inherited. Further, show an example of a translation using your attributedgrammar.Examples:

Tree Resultchar_tree tree(int,char) 1int_tree tree(int,char) 1char_tree tree(int,int) 0int_tree tree(int,tree(int,char)) 2

7. Given the following simple programming language P which allows only sequences of assign-ments:

P → S ; newline PP → εS → id := EE → id

E → ( E op E )

E → ( uop E )

Write an attributed grammar that prints the set of dependencies as pairs (defined variable,

referenced variable) for a program written in P enriched by line numbers. Given theprogram in Listing 1, the grammar produces the output {(x,y,1), (y,x,2), (y,z,2)}.

13

Page 14: Compiler Construction - Graz University of Technology · 2017. 5. 18. · Compiler Construction Collection of exercises Version May 15, 2017 Compiled on 15/05/2017 at 3:21pm Abbreviations

Listing 1: Program in P

1 x:=y;

2 y:=(x+z);

Give your attributes meaningful names and state which attributes are synthesized and whichare inherited. Further, show an example of a translation using your attributed grammar.

8. Given the grammar G which defines a simple programming language. Write an attributedgrammar which returns the control flow graph (CFG) for a program written in G. Giveyour attributes meaningful names and state which attributes are synthesized and which areinherited. Further, show an example of a translation using your attributed grammar.Yourare allowed to define your own data structure for the CFG.

P → S ; PP → εS → id := ES → if E then { P } OelseS → while E do { P }Oelse → else { P }Oelse → εE → id

E → ( E op E )

E → ( uop E )

14