University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Similar documents
1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

VIVA QUESTIONS WITH ANSWERS


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Question Bank. 10CS63:Compiler Design

Introduction to Bottom-Up Parsing

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

Compiler Design Aug 1996

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

UNIT-III BOTTOM-UP PARSING

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict

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

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

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

QUESTIONS RELATED TO UNIT I, II And III

2068 (I) Attempt all questions.

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

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

UNIT III & IV. Bottom up parsing

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

PRINCIPLES OF COMPILER DESIGN

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

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

Introduction to Syntax Analysis

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Intro to Bottom-up Parsing. Lecture 9

Parsing II Top-down parsing. Comp 412

Concepts Introduced in Chapter 4

Table-Driven Parsing

CMSC 330: Organization of Programming Languages

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

CMSC 330: Organization of Programming Languages

Bottom-Up Parsing. Lecture 11-12

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

INSTITUTE OF AERONAUTICAL ENGINEERING

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

BSCS Fall Mid Term Examination December 2012

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

LECTURE 3. Compiler Phases

CS143 Midterm Fall 2008

CS 44 Exam #2 February 14, 2001

CMSC 330: Organization of Programming Languages

CSE 401 Midterm Exam Sample Solution 2/11/15

Context-Free Grammars

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

EECS 6083 Intro to Parsing Context Free Grammars

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

ECE251 Midterm practice questions, Fall 2010

Introduction to Syntax Analysis. The Second Phase of Front-End

CMSC 330: Organization of Programming Languages. Context Free Grammars

CS606- compiler instruction Solved MCQS From Midterm Papers

Bottom-Up Parsing. Lecture 11-12

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

ECE 573 Midterm 1 September 29, 2009

Lecture 7: Deterministic Bottom-Up Parsing

Downloaded from Page 1. LR Parsing

General Overview of Compiler

VALLIAMMAI ENGINEERING COLLEGE

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

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

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

Principles of Programming Languages COMP251: Syntax and Grammars

Defining syntax using CFGs

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

Syntax Analysis Check syntax and construct abstract syntax tree

Top down vs. bottom up parsing

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

Lecture 8: Deterministic Bottom-Up Parsing

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

Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM-7/CS-701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70

Compiler Design Concepts. Syntax Analysis

CSE 3302 Programming Languages Lecture 2: Syntax

JNTUWORLD. Code No: R

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

QUESTION BANK CHAPTER 1 : OVERVIEW OF SYSTEM SOFTWARE. CHAPTER 2: Overview of Language Processors. CHAPTER 3: Assemblers

3. Parsing. Oscar Nierstrasz

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

LALR Parsing. What Yacc and most compilers employ.

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

n Parsing function takes a lexing function n Returns semantic attribute of 10/27/16 2 n Contains arbitrary Ocaml code n May be omitted 10/27/16 4

Bottom-Up Parsing II. Lecture 8

Introduction to Parsing

Ambiguity. Lecture 8. CS 536 Spring

Programming Languages and Compilers (CS 421)

n <exp>::= 0 1 b<exp> <exp>a n <exp>m<exp> 11/7/ /7/17 4 n Read tokens left to right (L) n Create a rightmost derivation (R)

Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

VETRI VINAYAHA COLLEGE OF ENGINEERING AND TECHNOLOGY

Lecture Notes on Shift-Reduce Parsing

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

LR Parsing Techniques

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

Transcription:

Subject:Compilers Design Division: All Branches Examiner:Dr. Abeer Tariq University of Technology Department of Computer Sciences 2102 Final Examination 2011-2012 1 st Term Year:Third Time: 3 Hours Date: / May /2012 حلول اسئلة الدور األول Q1. Define the following with simple example (choose five only): 1.)Ambiguity: A grammar that produces more than one parse tree for some sentences. An ambiguous grammar is one that produces more than one leftmost or more than one rightmost derivation for the same sentence. (1) three-address code Each statement contains at most 3 operands; in addition to : =, i.e., assignment, at most one operator. An easy and universal format that can be translated into most assembly languages A:=B+C (2) Compiler Is a program (translator) that reads a program written in one language, (the source language) and translates into an equivalent program in another language (the target language). (3) Operator grammar The operator-precedence parser is a shift reduce parser that can be easily constructed by hand. It used for a small class of grammars which is called operator grammar. These grammars have the property:

- That no production right side is ε - And no production right side has two adjacent nonterminals. Example: The following grammar for expressions: E EAE (E) E id A + - * / ^ (4) Handle Definition: a handle is a substring that 1- matches a right hand side of a production rule in the grammar and 2- Whose reduction to the nonterminal on the left hand side of that grammar rule is a step along the reverse of a rightmost derivation. (5) Conflicts "Conficts" occur when an ambiguity in the grammar creates a situation where the parser does not know which step to perform at a given point during parsing. There are two kinds of conflicts that occur 1. shift-reduce: a shift reduce conflict occurs when the grammar indicates that different successful parses might occur with either a shift or a reduce at a given point during parsing. The vast majority of situations where this conflict occurs can be correctly resolved by shifting. 2- reduce-reduce : a reduce-reduce conflict occurs when the parser has two or more handles at the same time on the top of the stack. Whatever choice the parser makes is just as likely to be wrong as not. In this case it is usually best to rewrite the grammar to eliminate the conflict, possibly by factoring. Q.2) briefly answer the following (choose three only) A) Draw the diagram showing the position of parser in compiler model. B) What is the difference between static and dynamic semantic? Static semantic checks are performed at compile time

Type checking Every variable is declared before used Identifiers are used in appropriate contexts Check subroutine call arguments Dynamic semantic check are performed at run time, and the compiler produces code that performs these checks Array subscript values are within bounds Arithmetic errors, e.g. division by zero A variable is used but hasn't been initialized When a check fails at run time, an exception is raised C) Explain the three techniques of loop optimization with simple examples. Code Motion An important modification that decreases the amount of code in a loop is code motion. This transformation takes an expression that yields the same result independent of the number of times a loop is executed and places the expression before the loop. Ex1: While ( I <= limit-2) Sol: t = limit-2 While (I <= t ) Induction Variable Ex: X = 2 * y + 2 * h + 4 Sol: X = 2 * (y + h) +4 Note: use the algebra method to optimize the mathematical expression. Reduction in strength Reduction in strength, which replaces an expensive operation by a cheaper one, such as a multiplication by an addition. Ex: t4 = 4*j - 4 D) List the four error routines in the bottom-up parsing table, and what is the suitable solution for each type.

Q.3) Consider the following grammar G. 1. Eliminate left recursion: 2. Eliminate left factoring. 3. Prove that your grammar is LL(1) by whatever method you like. 3. After finding first and follow we will find that in first nonterminal that contain

empty there is not find the same terminal in the follow of that nonterminal so the grammar is LL(1). Q.4) Consider the following grammar: Parse the string " abaaba" by using predictive parsing method. 1. Eliminate the left recursion E TE' E' a b F E' e T FT' T' b a T' e F a b [ E ] 2.Find first and follow First Follow L [, a, b $, ] L' a, e $, ] T [, a, b a, $, ] T' b, e a, $, ] S [, a, b a, b, $, ] 3.parsing table a b [ ] $ L E TE' E TE' E TE' L' E' a b F E' E' e E' e T T FT' T FT' T FT' T' T' e T' b a T' T' e T' e S F a F b F [E] 4.parse the sentence Stack Input Output $E abaaba $ $ E' T abaaba $ E TE' $ E' T'F abaaba $ T FT' $ E' T'a abaaba $ F a $ E' T' $ E' T' a b baaba $ baaba $ T' b a T' $ E' T' a $ E' T' aaba $ aba $ T' e $ E' $ E' F b a aba $ aba $ E' a b F E' $ E' F b $ E' F ba $ a $ F a $ E' a $ E' a $ $ E' e $ $ The sentence is accept

Q.5) Given the following grammar: The terminals are = {a,b,0,1}, and the nonterminals are={s,a,b}. Parse the sentence "aa1bbbb" using SLR parsing method.

Q.6) A: Write the triple and quadruple form for the following statement: 1. Triple A[j] = B+( c + d ) * X[i] Y/Z op Arg 1 Arg2 (0) + c d (1) =[ ] A i (2) * 0 1 (3) / Y Z (4) + B 2 (5) - 4 3 (6) [ ]= A J = 6 5 2. Quadruple op Arg 1 Arg2 result + c d T1 =[ ] A i T2 * T1 T2 T3 / Y Z T4 + B T3 T5 - T5 T4 T6 [ ]= A J T7 = T6 T7

B: Show a successful bottom up parse (stack) of input " bcac " using the parsing table below: