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

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

Syntax-Directed Translation. Lecture 14

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COP5621 Exam 3 - Spring 2005

2068 (I) Attempt all questions.

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

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

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

Recursive Descent Parsers

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 2016

CSE 582 Autumn 2002 Exam 11/26/02

CSE 401 Midterm Exam Sample Solution 2/11/15

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

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

A simple syntax-directed

CPS 506 Comparative Programming Languages. Syntax Specification

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

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

CS 415 Midterm Exam Fall 2003

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Compiler Code Generation COMP360

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Syntax and Grammars 1 / 21

COMPILER DESIGN. For COMPUTER SCIENCE

Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

UNIVERSITY OF CALIFORNIA

LECTURE 3. Compiler Phases

Building a Parser Part III

Downloaded from Page 1. LR Parsing

COP4020 Programming Assignment 2 - Fall 2016

CS 415 Midterm Exam Spring SOLUTION

Compiler Design Aug 1996

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

Syntax-Directed Translation. Introduction

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

COP4020 Spring 2011 Midterm Exam

Compiler construction in4303 lecture 3

Compiler construction lecture 3

Syntax Analysis. Chapter 4

Syntax Analysis Part I

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

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

LL parsing Nullable, FIRST, and FOLLOW

Lexical and Syntax Analysis

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

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

COP4020 Programming Assignment 2 Spring 2011


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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Bottom-Up Parsing. Lecture 11-12

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

CSCE 531 Spring 2009 Final Exam

Qualifying Exam in Programming Languages and Compilers

syntax tree - * * * * * *

Project 2 Interpreter for Snail. 2 The Snail Programming Language

CS 406/534 Compiler Construction Putting It All Together

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

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

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

CS 4120 Introduction to Compilers

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

Principles of Programming Languages COMP251: Syntax and Grammars

CS 406: Syntax Directed Translation

Wednesday, September 9, 15. Parsers

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

CSE302: Compiler Design

Error Recovery. Computer Science 320 Prof. David Walker - 1 -

Syntax-Directed Translation Part II

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

Defining syntax using CFGs

CS 415 Midterm Exam Spring 2002

Using an LALR(1) Parser Generator

CSE P 501 Exam 8/5/04

Top down vs. bottom up parsing

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

Compiling Regular Expressions COMP360

Compilers. Compiler Construction Tutorial The Front-end

Syntax-Directed Translation Part I

VIVA QUESTIONS WITH ANSWERS

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

CSE P 501 Exam Sample Solution 12/1/11

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Syntax-Directed Translation

CSE302: Compiler Design

CSE302: Compiler Design

Question Points Score

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Lexical Scanning COMP360

Yacc: A Syntactic Analysers Generator

S Y N T A X A N A L Y S I S LR

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

Problem Score Max Score 1 Syntax directed translation & type

Compiler Design (40-414)

Introduction to Compiler Design

Transcription:

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 STUDENT ID: INSTRUCTIONS Please write your student ID on this page. Do not write it or your name on any other pages. Please answer directly on this exam. This exam has 4 questions, and is worth 30% of the course mark. NO AIDS ARE ALLOWED. A - General Concepts 40 B - Shift-Reduce parsing 15 C - Evaluation 20 D - Grammars 25 This exam has 7 pages including the front page

This exam has 7 pages including the front page

CPS 710 FINAL EXAM F96-3 Part A - General Concepts - 40 marks A1 (4 marks) What is a scanner? A3 (5 marks) Why would you design a compiler to generate intermediate code instead of directly generating machine code? A3 (5 marks) Draw a syntax tree for the expression p and q or not r A4 Given the grammar BOOLEAN AND-EXPR {or AND_EXPR} 0 AND_EXPR NOT_EXPR {and NOT_EXPR} 0 NOT_EXPR {not} 0 EXPR EXPR p q r Give the rightmost derivation of the expression p and q or not r

CPS 710 FINAL EXAM F96-4 A5 Explain the difference between syntax and semantic errors. Give 2 examples of each type of error. A6 Why is is better to organise the name table of a symbol table as self-organising list (i.e. list of identifiers where last used identifier is moved to the front) rather than as a simple list? A7 (10 marks) Explain 2 different ways of organising parsing and symbol table activities in order to implement lexical scoping in an interpretor.

CPS 710 FINAL EXAM F96-5 Part B - Shift-Reduce Parsing (15 marks) Given the LR(1) grammar with the following productions augmented with states (shown subscripted and outlined): (1) IF if e STATEMENTS else STATEMENTS (2) IF if e STATEMENTS (3) STATEMENTS STATEMENTS STATEMENT (4) STATEMENTS STATEMENT (5) STATEMENT s Fill in the LR table for this grammar: IF STATEMENTS STATEMENT if e else s $ There are 4 possible entries for each slot in the table: S state: shift token onto symbol stack and change state R production: reduce the production by popping n symbols off the symbol stack and n symbols off the state stack, and inserting the left-hand side of the production into the input stream HALT: to halt process nothing: to indicate a syntax error

CPS 710 FINAL EXAM F96-6 Part C - Evaluation - 20 marks - This has a short answer In this question you will be evaluating CASE statements in a new programming language. The section of the grammar which deals with CASE statements is: CASE -> case EXPRESSION COMPARISONS OTHERWISE ; COMPARISONS -> {COMPARISON COMPARISONS} 0 COMPARISON -> EXPRESSION : EXPRESSION OTHERWISE -> otherwise : EXPRESSION The non-terminals COMPARISON and COMPARISONS do not appear in any other part of the grammar. A syntax tree node produced by the parser has the structure: typedef struct node { int type; struct node *v1, *v2, *v3; } node; a CASE structure is organised as follows: - type = CASE - v1 points to an expression which will be compared against others - v2 points to a COMPARISON, or an expression corresponding to the otherwise clause of the case statement. a COMPARISON structure is organised as follows - type = COMPARISON - v1 points to an expression that will be compared to the value of v1 in the CASE structure. - v2 points to an expression that will be the value of the case structure if the CASE v1 is equal to the COMPARISON v1. - v3 points to the next COMPARISON structure, or an expression corresponding to the otherwise clause of the CASE statement. The CASE structure evaluates to the value of the first v2 in the list of comparisons for which the associated v1 is equal to the v1 in the CASE structure. If there is no such value, it evaluates to the value of the otherwise clause. You are writing int eval(node *tree), a function which evaluates a syntax tree. Eval returns an int which is the value of the syntax tree (you can assume that syntax trees all evaluate to integers). Eval is completely written except for the part which handles CASE and COMPARISON structures. Write C code for the section of eval which handles CASE structures, and whatever C code is necessary to handle COMPARISON structures. You do not need to do any memory management in your code. You can assume that the CASE expression you are evaluating is error-free.

CPS 710 FINAL EXAM F96-7 Part D - Grammars - 25 marks In this question, non-terminals are in UPPER-CASE and terminals are underlined. D1 What is a left-recursive grammar? Why would you not want a grammar to be leftrecursive? D2 Remove the left recursion from the following set of productions. You may need to introduce new non-terminals. S S S S S S S - S S { 1 } { 2 }

CPS 710 FINAL EXAM F96-8 D3 Left-factor the following set of productions fully. You may need to introduce new nonterminals. S IF o IF IF1 IF2 IF1 if c then S IF2 if c then S else S D4 (7 marks) Why is the following set of productions ambiguous? WHILE while e do STATS od STATS STAT STATS 0 STAT WHILE ASSIGNMENT EXPR ASSIGNMENT a := EXPR EXPR TERM {( + ) EXPR} 0 TERM ( + ) 0 a