UNIVERSITY OF CALIFORNIA

Similar documents
UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

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

ECE251 Midterm practice questions, Fall 2010

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Configuration Sets for CSX- Lite. Parser Action Table

CS164: Midterm I. Fall 2003

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Lecture 8: Deterministic Bottom-Up Parsing

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

How do LL(1) Parsers Build Syntax Trees?

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Lecture 7: Deterministic Bottom-Up Parsing

Class Information ANNOUCEMENTS

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

Bottom-Up Parsing. Lecture 11-12

CS131 Compilers: Programming Assignment 2 Due Tuesday, April 4, 2017 at 11:59pm

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

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

Using an LALR(1) Parser Generator

Midterm I - Solution CS164, Spring 2014

CSE302: Compiler Design

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

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

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

Conflicts in LR Parsing and More LR Parsing Types

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

BSCS Fall Mid Term Examination December 2012

Monday, September 13, Parsers

Bottom-Up Parsing. Lecture 11-12

Syntax-Directed Translation. Lecture 14

COP 3402 Systems Software Syntax Analysis (Parser)

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

CS143 Midterm Fall 2008

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

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

Semantic Analysis. Lecture 9. February 7, 2018

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

CS143 Midterm Sample Solution Fall 2010

CS 230 Programming Languages

Compiler Lab. Introduction to tools Lex and Yacc

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Compiler Construction Using

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

CS143 Midterm Spring 2014

Downloaded from Page 1. LR Parsing

In One Slide. Outline. LR Parsing. Table Construction

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

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

Syntax. In Text: Chapter 3

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

Compiler construction in4020 lecture 5

shift-reduce parsing

Syntax Analysis. Chapter 4

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

Yacc: A Syntactic Analysers Generator

Wednesday, September 9, 15. Parsers

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

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

Syntax. A. Bellaachia Page: 1

Homework & Announcements

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

CSCI Compiler Design

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

Lecture Notes on Bottom-Up LR Parsing

2068 (I) Attempt all questions.

CSE 582 Autumn 2002 Exam 11/26/02

Midterm I (Solutions) CS164, Spring 2002

Chapter 3: Lexing and Parsing

TDDD55 - Compilers and Interpreters Lesson 3

List of Figures. About the Authors. Acknowledgments

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

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

Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

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

Compilers. Computer Science 431

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

CSCI Compiler Design

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CS 411 Midterm Feb 2008

(F)lex & Bison/Yacc. Language Tools for C/C++ CS 550 Programming Languages. Alexander Gutierrez

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

COMPILER CONSTRUCTION Seminar 02 TDDB44

MIDTERM EXAM (Solutions)

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Parser Tools: lex and yacc-style Parsing

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

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

Compiler Design Overview. Compiler Design 1

CS415 Compilers. Lexical Analysis

CSE 401 Midterm Exam 11/5/10

Parser Tools: lex and yacc-style Parsing

A simple syntax-directed

Transcription:

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS164 Fall 1997 P. N. Hilfinger CS 164: Midterm Name: Please do not discuss the contents of this test with anyone before the evening of Friday, 10 October. You have two hours to complete this test. Please put your login on each sheet, as indicated, in case pages get separated. Answer all questions in the space provided on the exam paper. Show all work (but be sure to indicate your answers clearly.) The exam is worth a total of 35+ points (out of the total of 200), distributed as indicated on the individual questions. You may use any notes, books, or computers you please--anything inanimate. We suggest that you read all questions before trying to answer any of them and work first on those about which you feel most confident. You should have 5 problems on 8 pages. 1. /9 2. /9 3. / 4. /9 5. /8 TOT. /35 1. [9 points] A certain language has the following terminal symbols: @ * # i / 1

A shift-reduce parser for this language processes the string i * i # @ i / and performs the following actions: 1. Shift i. 2. Reduce 0 symbols (on the stack), producing (the non-terminal symbol) N. Then reduce 2 symbols, producing T. Then reduce 1 symbol, producing F. 3. Shift *. Then shift i. Then shift #. 4. Reduce 1 symbol, producing N. Then reduce 2 symbols, producing T. 5. Shift @. Then shift i. 6. Reduce 0 symbols, producing N. Then reduce 2 symbols, producing T. Then reduce 1 symbol, producing F. Then reduce 5 symbols, producing F. 7. Shift /. 8. Reduce 2 symbols, producing E and accepting the input. Now for the questions: a. As far as one can tell from this sample, what is the grammar for the language being parsed? (Continued on next page) b. What is the parse tree for this string? 2. [9 points] Consider the following NFA: 2

a. Describe as succinctly as possible (in English) the language recognized by this NFA. b. Write a regular expression that describes the same language. You may use Lex notation. c. Write a BNF grammar that describes the same language. 4. [9 points] Consider the following grammar: 3

I am interested in finding the maximum number of '+' operations in any expression (expr) of a given program. For example, in the answer should be 2 (this assumes that ID denotes 1-character identifiers.) Fill in the recursive-descent compiler on the next page so that the input program gets checked for syntactic correctness and the right number gets printed. Be careful: the grammar is not LL(1); you can change it as needed, just so long as you end up recognizing the same language and get the right numbers. Assume that the following functions are available for your use: next() returns the syntactic category of the next (as yet unprocessed) symbol of the input: one of the values advances to the next token. scan(t) checks that next() is T, and reports an error if not. It then ERROR() reports an error. Write your program on the next page without using global variables. All assignments should be to local variables only. Do not introduce any new types. Here is the parser skeleton. Remember: no global variables; assign only to local variables; do not introduce new types. void start() { printf ("Maximum operators in any expression = %d\n", ); } pr 5. [8 points] In the following, do not worry about syntax trees or semantic actions; just consider the language being recognized. Symbols in single quotes or in all-upper-case are terminal symbols. It should not be necessary to build LALR(1) machines to answer any of these questions! a. I happened to have an LL(1) parser generator and put the following grammar through it: 4

The parser generator told me the grammar is not LL(1). Why not? b. In the language Alphard, x.y could be written y(x). Assuming we want a LALR(1) parser, why do we get a reduce/reduce conflict on the ')' token in the following grammar? Give an example of an input that runs into the error. (Warning: is it ambiguous?) c. In the following grammar, there is a shift/reduce conflict on '('. Bison says that the problem is in the following state: Identify the bug in the grammar. Give an example of an input that runs into the error (that is, that requires making a shift/reduce decision for a state and symbol that have a conflict). Propose a simple change to the grammar (and the language it describes) to fix the problem (that is, make an informed guess as to what was really intended). 5

d. The following expression grammar makes addition and multiplication left-associative and exponentiation (**) right associative. What causes the shift-reduce conflicts on the symbols +, *, and **? Give an example of an input that runs into one of the errors. (Be careful: TYPE and ID are distinct tokens.) 6