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

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

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

CSE 582 Autumn 2002 Exam 11/26/02

CSE3322 Programming Languages and Implementation

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

Syntax-Directed Translation. Lecture 14

MIDTERM EXAM (Solutions)

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CS 4120 Introduction to Compilers

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

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

COP4020 Programming Assignment 2 - Fall 2016

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

Compiler Design Aug 1996

CS 406: Syntax Directed Translation

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE 5317 Midterm Examination 4 March Solutions

COP4020 Fall 2006 Final Exam

Recursive Descent Parsers

COP4020 Spring 2011 Midterm Exam

Question Points Score

CSCE 531, Spring 2015 Final Exam Answer Key

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

Midterm I - Solution CS164, Spring 2014

Top down vs. bottom up parsing

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

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Parsing III. (Top-down parsing: recursive descent & LL(1) )

CSE P 501 Exam 8/5/04

Bottom-Up Parsing. Lecture 11-12

Qualifying Exam in Programming Languages and Compilers

CSE 582 Autumn 2002 Exam Sample Solution

COP4020 Programming Assignment 2 Spring 2011

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

CSCI Compiler Design

UNIVERSITY OF CALIFORNIA

Syntax-Directed Translation Part II

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

LR Parsing LALR Parser Generators

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

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

CSCE 531 Spring 2009 Final Exam

1 Lexical Considerations

1. Consider the following program in a PCAT-like language.

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

CSE3322 Programming Languages and Implementation

LR Parsing - The Items

Compiler construction in4303 lecture 3

CS 415 Midterm Exam Fall 2003

Compiler construction lecture 3

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

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

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

Project 2 Interpreter for Snail. 2 The Snail Programming Language

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

CS 415 Midterm Exam Spring SOLUTION

COMPILER DESIGN. For COMPUTER SCIENCE

Comp 311: Sample Midterm Examination

Problem Score Max Score 1 Syntax directed translation & type

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

Lexical and Syntax Analysis

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

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

CSE 401 Final Exam. December 16, 2010

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

CPS 506 Comparative Programming Languages. Syntax Specification

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

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

A simple syntax-directed

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

Syntax Analysis. Chapter 4

COP5621 Exam 3 - Spring 2005

Lexical and Syntax Analysis. Bottom-Up Parsing

Lexical and Syntax Analysis. Top-Down Parsing

Introduction to Parsing. Lecture 8

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

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

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

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Lexical Analysis. Introduction

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Bottom-Up Parsing. Lecture 11-12

syntax tree - * * * * * *

WARNING for Autumn 2004:

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

CA Compiler Construction

Compiler Code Generation COMP360

Lecture 8: Deterministic Bottom-Up Parsing

CS 536 Midterm Exam Spring 2013

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Abstract Syntax Trees & Top-Down Parsing

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Parsing CSCI-400. Principles of Programming Languages.

LR Parsing E T + E T 1 T

Lecture 7: Deterministic Bottom-Up Parsing

Transcription:

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 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. It has 8 pages including this one NO AIDS ARE ALLOWED. A - General Concepts 30 B - Shift-Reduce parsing 20 C - Evaluation 25 D - Grammars 25

CPS 710 FINAL EXAM F97-2 Part A - General Concepts - 30 marks A1 (7 marks) Draw a transition diagram to recognise the regular expression a (a b)* b* a Show all a and b edges from all states of your diagram A2 (4 marks) Give two reasons why you would want to have the scanning module separate from the rest of the program. A3 (4 marks) What is a parser?

CPS 710 FINAL EXAM F97-3 A4 (5 marks) List 5 fields kept in a symbol table entry A5 (4 marks) List 4 errors that can only be detected once the symbol table is implemented. A7 What would be the output of the following program fragment: integer a = 1; integer b = 2; function f( integer a) { b := a + 5; return b; } function g(integer a) { integer b; return f(a); } display g(6); display b; If the language is statically scoped? If the language is dynamically scoped?

CPS 710 FINAL EXAM F97-4 Part B - Shift-Reduce Parsing ( 20 marks) Given the LR(1) grammar with the following productions augmented with states (shown subscripted and outlined): (1) CASE case id CLAUSES end (2) CLAUSES CLAUSES CLAUSE (3) CLAUSES CLAUSE (4) CLAUSE constant : stat Fill in the LR table for this grammar: CASE CLAUSES CLAUSE case id end constant : stat $ 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 F97-5 Part C - Evaluation - 25 marks - This has a short answer In this question you will be evaluating simplified DO expressions in Scheme. The section of the grammar which deals with DO expressions is: DO -> ( do ( INDEX-LOOP) ( EXIT-CONDITION FINAL-VALUE ) DO_BODY ) INDEX-LOOP -> ( VAR-NAME INITIAL-VALUE NEXT-VALUE ) VAR-NAME -> Identifier INITIAL-VALUE -> EXPRESSION NEXT-VALUE -> EXPRESSION EXIT-CONDITION -> EXPRESSION FINAL-VALUE -> EXPRESSION DO-BODY -> EXPRESSION * Tokens are in bold-face. The non-terminal EXPRESSION is defined somewhere else in the grammar. No other productions use the INDEX-LOOP non-terminal. A syntax tree node produced by the parser has the structure: typedef struct node { int type; struct node *v1, *v2, *v3 *v4; } node; a DO structure is organised as follows: - type = DO - v1 points to an INDEX_LOOP structure - v2 points to the exit condition which must evaluate to TRUE for the loop to exit - v3 points to the final value of the entire loop - v4 points to the do body which is evaluated at each loop iteration an INDEX_LOOP structure is organised as follows: - type = INDEX_LOOP - v1 points to the name of the loop index - v2 points to the initial value of the loop index - v3 points to an expression describing how to calculate the next value of that index at each iteration - v4 is NULL The evaluation rules for these two structures are: 1. The loop index is initialised, which means that a new index variable is created whose name is defined in the INDEX_LOOP v1 field and initial value in its v2 field. The symbol table functions used to do this are: (you can assume identifier tokens are of type *node) void newsymbol( node *identifier ), and void setvalue( node *identifier, int newvalue ) 2. The EXIT-CONDITION is evaluated 2.1 If it evaluates to TRUE, the final value is evaluated and returned 2.2 Otherwise 2.2.1 The do body is evaluated 2.2.2 The next value is evaluated and the loop index set to it 2.2.3 Continue execution at 2. You are writing int eval(node *tree), a function which evaluates a syntax tree. Eval returns an int which represents the value of the syntax tree (you can assume that all values of all types can be represented by integers). Eval is completely written except for the part which handles DO and INDEX_LOOP structures. Write C code for the section of eval which handles DO structures, and whatever C code is necessary to handle INDEX_LOOP structures. You do not need to do any memory management in your code. You can assume that the DO expression you are evaluating is error-free.

CPS 710 FINAL EXAM F97-6

CPS 710 FINAL EXAM F97-7 Part D - Grammars - 25 marks In this question, non-terminals are in UPPER-CASE and terminals are underlined. D1 What is an ambiguous grammar? Why would you not want a grammar to be ambiguous? D2 Why is the following set of productions ambiguous? CASE case identifier in CLAUSES endcase CLAUSES CLAUSE CLAUSES ε CLAUSE EXPRESSION : STATEMENT STATEMENT identifier := EXPRESSION EXPRESSION ( + ) 0 constant {( + ) EXPRESSION} 0

CPS 710 FINAL EXAM F97-8 D3 Left-factor the following set of productions fully. You may need to introduce new non-terminals. B C t f C ( t f ) C C ( t f ) C D4 Remove the right recursion from the following set of productions. You may need to introduce new non-terminals. You can make the grammar left recursive if you wish. S E { + S } 0 E T { * E } 0 E ( S ) T 0 1 2