Compiler Construction

Similar documents
Compiler Construction

QUESTIONS RELATED TO UNIT I, II And III

Compiler Design Aug 1996

2068 (I) Attempt all questions.

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

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Chapter 2 - Programming Language Syntax. September 20, 2017

Appendix Set Notation and Concepts

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

A Simple Syntax-Directed Translator

Lexical Analysis. Introduction

Question Bank. 10CS63:Compiler Design

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Syntax and Parsing COMS W4115. Prof. Stephen A. Edwards Fall 2003 Columbia University Department of Computer Science

CS415 Compilers. Lexical Analysis

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Formal Languages and Compilers Lecture VI: Lexical Analysis

CS606- compiler instruction Solved MCQS From Midterm Papers

Midterm I - Solution CS164, Spring 2014

Semantic Analysis. Lecture 9. February 7, 2018

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

CSCI Compiler Design

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

COP 3402 Systems Software Syntax Analysis (Parser)

CSE 401 Midterm Exam Sample Solution 2/11/15

CS 314 Principles of Programming Languages. Lecture 3

A programming language requires two major definitions A simple one pass compiler

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Compiler phases. Non-tokens

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

GUJARAT TECHNOLOGICAL UNIVERSITY

Languages and Compilers

Languages, Automata, Regular Expressions & Scanners. Winter /8/ Hal Perkins & UW CSE B-1

Principles of Programming Languages COMP251: Syntax and Grammars

Midterm I (Solutions) CS164, Spring 2002

LECTURE NOTES ON COMPILER DESIGN P a g e 2

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

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

Index. caps method, 180 Character(s) base, 161 classes

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

Implementation of Lexical Analysis

Context-Free Grammar (CFG)

Compiler Design Overview. Compiler Design 1

CSE 401 Midterm Exam 11/5/10

CSCE 314 Programming Languages

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

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

Compiler Code Generation COMP360

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

shift-reduce parsing

Introduction to Parsing. Lecture 5

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Syntax-Directed Translation. Lecture 14

LECTURE 7. Lex and Intro to Parsing

Outline. 1 Scanning Tokens. 2 Regular Expresssions. 3 Finite State Automata

Compiler Construction

This book is licensed under a Creative Commons Attribution 3.0 License

Lexical Analysis (ASU Ch 3, Fig 3.1)

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

Bottom-Up Parsing. Lecture 11-12

A simple syntax-directed

More Assigned Reading and Exercises on Syntax (for Exam 2)

Decaf Language Reference

Principles of Programming Languages [PLP-2015] Detailed Syllabus

CS143 Midterm Sample Solution Fall 2010

UNIT -2 LEXICAL ANALYSIS

Lexical Analysis 1 / 52

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Syntax and Parsing COMS W4115. Prof. Stephen A. Edwards Fall 2004 Columbia University Department of Computer Science

ECS 120 Lesson 7 Regular Expressions, Pt. 1

Implementation of Lexical Analysis

Building Compilers with Phoenix

Introduction to Parsing. Lecture 5

Compiler Construction Using

Bottom-Up Parsing. Lecture 11-12

Lexical Analysis. Chapter 2

Compiler Construction

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Lecture 4: Syntax Specification

MIDTERM EXAM (Solutions)

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Introduction to Lexical Analysis

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

Transcription:

Compiler Construction Collection of exercises Version February 7, 26 Abbreviations NFA. Non-deterministic finite automaton DFA. Deterministic finite automaton Lexical analysis. 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) + ) (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 ( - 9 ). A list of integers consists of an arbitrary number of comma separated integers and is surrounded by curly brackets. Valid lists Invalid lists {} {##,} {##} {,2,3} {#2#, ##, #8#} {#2# ## #8#} Use regular expressions to describe integers and lists of integers. Construct an equivalent DFA for these regular expressions.

4. For the following NFA: Indicate an equivalent regular expression. Transform the NFA into a DFA by means of the Subset Construction Algorithm. (a) NFA (b) NFA 2 (c) NFA 3 (a) NFA + - 2 3 (b) NFA 2 4 6 7 5 (c) NFA 3 2

5. Real constants are defined in the SML standard as follows: An integer constant (in decimal notation) is an optional negation symbol (~) followed by a non-empty sequence of decimal digits,...,9. [... ] A real constant is an integer constant in decimal notation, possibly followed by a point (.) and one or more decimal digits, possibly followed by an exponent symbol (E or e) and an integer constant in decimal notation; at least one of the optional parts must occur, hence no integer constant is a real constant. Valid real constants Invalid real constants.7 23 3.32E5.3 3E~7 4.E5. E2. Define a regular expression that fits real constants. Construct an equivalent NFA by means of the Thompson Construction Algorithm. 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) f ile (char + ( char + ) ) (. + ) char x Valid filename.././x/x x Invalid filename /../ x x Construct an equivalent NFA by means of the Thompson Construction Algorithm. You are allowed to make simplifications. Transform the NFA into a DFA by means of the Subset Construction Algorithm. 3

7. Non-deterministic finite automaton (NFA ) with as initial state and 4 and 7 as final states: 3 2. 4 5 7 6 Transform the NFA 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 give examples. Construct a non-deterministic finite automaton (NFA 2 ) from the regular expression by means of the Thompson Construction Algorithm. What are the main differences between NFA and NFA 2? 4

2 Syntactic analysis. For the following grammars (with S as start symbol): Transform the grammar to LL() if necessary. Compute the FIRST and FOLLOW sets. Build the LL() parsing table. (a) S E E E op E E F F E ( E ) (b) S C C // T C /* A */ T char T T cr A char T A cr T A ɛ (c) S B B A B C A id C ( LL ) LL B LR LL ɛ LR, B LR LR ɛ (d) S S, S S F S id R R : num R ɛ F id ( S) (e) S T T @ ( T, T ) T @ L L id L num 2. Explain why the following grammar cannot be transformed into LL()! Z $Z$ Z %Z% Z ɛ 5

3. For the following grammars (with S as start symbol): Transform the grammar to LL() if necessary and build the LL() parsing table. Build the SLR() parsing table from the original (unmodified) grammar. (a) Grammar: S b W S S b W W a W ɛ (b) Grammar: S ( LO ) S atom LO S LR LO ɛ LR, S LR LR ɛ (c) Grammar: S C S assign C if P S S fi C if P S fi P pred (d) Grammar: S S, S S 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() parsing table for the transformed grammar. Build the SLR() parsing table for the original (unmodified) grammar. (a) Grammar: S idlist statlist idlist identifier idlist, identifier statlist stat statlist ; stat stat identifier := expression if expression then stat else stat if expression then stat expression identifier + identifier (b) Grammar: S formula formula formula expr formula expr expr expr expr term term term term factor factor factor factor (formula) 6

5. For the following grammars (with S as start symbol): Compute the SLR() 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() parsing table. (a) Grammar: S S # S S S % S S char Assumptions: # has a stronger binding than %. Equivalent operators are left associative. Expression: char % char # char (b) Grammar: S S # S S S % S S? S S bn Assumptions:? has a stronger binding than # # has a stronger binding than %. Equivalent operators are left associative. Expression: bn % bn #? (c) Grammar: S S # S S S : E S E E E E E E Assumptions: bn : has a stronger binding than # Equivalent operators are left associative. Expression: # : (d) Grammar: S L L L C L C C cmd AL AL str AL AL ɛ Assumptions: Operators are left associative. Expression: cmd str str cmd 7

3 Attributed grammars. Infix, postfix and prefix notation (a) Extend the following grammar with attributes that allow to translate prefix expressions to postfix expressions. Give your attributes meaningful names and state which attributes are synthesized and which are inherited. E ( O E E ) E num O + - Prefix Postfix (+(+ 2)3) (( 2+)3+) (+(+2 3)) ((2 3+)+) (b) Write an attributed grammar that translates expressions in postfix notation into infix. Define a proper grammar for the postfix notation. Postfix Infix 2 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. (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 number of pairs of parentheses. (d) Show for both implementations (b) and (c) how the number of parentheses are computed for the term [[]]. 3. The following grammar, with S as start symbol, defines binary numbers. Write an attributed Grammar that computes the decimal number of a binary number. S OP BB OB OE OP + ɛ BB R BB R R R R R R ɛ OB. BB OB ɛ OE E OP BB OE ɛ Binary number Decimal number. 5.325 +E-.5 -. -.25 8

4. For the following grammar G: Write an attributed grammar that translates expressions of the grammar G into Assembler code. Indicate for all attributes if it is synthesized or inherited. The following Assembler instructions are available: ADD R, R 2 R := R + R 2 MUL R, R 2 R := R R 2 MOV M, R R := memory(m) LOA N, R R := N (Directly sets the value of a register) R i 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 + E E E E E num Expression E: (+(x*5)) (b) Grammar G: E ( E E ) E ( E num ) E num The operator ** denominates the power function, e.g. (2 3 **) means 2 3. Expression E: (2(2 3**)*) (c) Grammar G: P E : AL E + E E E num AL AE ; AL AL ɛ A := num A program (P) consists of an expression (E) and a list of value assignments (AL) for all variables in the expression. Note that the values of the variables have to be defined before the expression can be evaluated. Expression E: + a + b 3; a=2; b=3; 9

5. Nested lists (a) Given the following grammar with S as start symbol. Write an attributed grammar that returns the length of the outermost list. Give your attributes meaningful names and state which attributes are synthesized and which are inherited. S ( E ) L ( E ) L atom E L R E ɛ R, L R R ɛ List Result () (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, LR LR L Write an attributed grammar that returns the list of atoms that are element of the given depth. Give your attributes meaningful names and state which attributes are synthesized and which are inherited. Examples: Original list N Result list [nil] [nil] [atom] [atom] [atom] [nil] [[atom]] [atom] [[atom,[atom,atom]]] [atom] [[atom,[atom,nil],atom]] [atom,atom] [[nil,[atom,[nil]],nil]] 2 [atom] (c) Write a grammar for the following type of lists: Each list starts with a number followed by : and the virtual list. A list starts with ( and ends with ). The elements of a list can be both atoms (atom) and lists. Ensure with attributes that the number at the beginning of the list definition is equal to the number of atoms in the list and the sub-lists. Examples: List Result 3:((atome)((atom)atom)) Valid :() Invalid :() Valid

(d) Extend the following grammar with attributes so that given expressions are evaluated according to the following examples. The given grammar describes an operator that should be applied to all elements of a list. The list may contain sublists (without a preceding operator). The result should also be a list. Which attributes are synthesized, which attributes are inherited? E O L L ( LE LR ) LR, LE LR LR ɛ LE num LE L O inc dec Expression Evaluated expression inc(,(,2),3) (2,(2,3),4) dec(,(,2),3) (,(,),2) 6. Given the following grammar with S as start symbol which defines binary trees: S T L T 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 tree type. Give your attributes meaningful names and state which attributes are synthesized and which are inherited. Examples: Tree Result char_tree tree(int,char) int_tree tree(int,char) char_tree tree(int,int) int_tree tree(int,tree(int,char)) 2 7. Given the following simple programming language P which allows only sequences of assignments: P S ; P P ɛ S id := E 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. For example, the program x:=y; y:=x+z; produces the output {(x,y,), (y,x,2), (y,z,2)}.

8. Given the grammar G which defines a simple programming language. Write an attributed grammar which returns the control flow graph (CFG) for a program written in G. Give your attributes meaningful names and state which attributes are synthesized and which are inherited. Your are allowed to define your own data structure for the CFG. P S ; P P ɛ S id := E S if E then { P } Oelse S while E do { P } Oelse else { P } Oelse ɛ E ( E op E ) E ( uop E ) 2