CS 164, Spring 2010 CS 164: Test #1 Solutions P. N. Hilfinger DO NOT. Copyright (c) 2010 by P. N. Hilfinger. Copying and reproduction prohibited.

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

Syntax-Directed Translation. Lecture 14

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

Recursive Descent Parsers

CSE 3302 Programming Languages Lecture 2: Syntax

Syntax. In Text: Chapter 3

Downloaded from Page 1. LR Parsing

UNIVERSITY OF CALIFORNIA

Lecture 6: Top-Down Parsing. Beating Grammars into Programs. Expression Recognizer with Actions. Example: Lisp Expression Recognizer

CSCI312 Principles of Programming Languages!

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

Chapter 3. Describing Syntax and Semantics ISBN

Introduction to Parsing. Lecture 8

Programming Language Syntax and Analysis

A simple syntax-directed

Syntax Errors; Static Semantics

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

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Part 5 Program Analysis Principles and Techniques

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Top down vs. bottom up parsing

Chapter 4. Lexical and Syntax Analysis

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

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

COP 3402 Systems Software Syntax Analysis (Parser)

CSE302: Compiler Design

Introduction to Lexing and Parsing

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

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

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

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

Lecture 8: Deterministic Bottom-Up Parsing

Describing Syntax and Semantics

Lexical Analysis. Lecture 2-4

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

Lecture 7: Deterministic Bottom-Up Parsing

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Lexical and Syntax Analysis

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

Chapter 3. Parsing #1

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

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

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CSE431 Translation of Computer Languages

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 2. Lexer design. Syntax Analysis: Context-Free Grammars. HW2 (out, due Tues)

LECTURE 3. Compiler Phases

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

Principles of Programming Languages COMP251: Syntax and Grammars

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

4. Lexical and Syntax Analysis

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Introduction to Lexical Analysis

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

CPS 506 Comparative Programming Languages. Syntax Specification

Front End. Hwansoo Han

4. Lexical and Syntax Analysis

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

Bottom-Up Parsing. Lecture 11-12

EECS 6083 Intro to Parsing Context Free Grammars

Lexical Analysis. Chapter 2

Introduction to Lexical Analysis

CSE P 501 Exam 11/17/05 Sample Solution

CS 230 Programming Languages

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

A Simple Syntax-Directed Translator

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

CS 406: Syntax Directed Translation

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

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

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

Dr. D.M. Akbar Hussain

Syntax. 2.1 Terminology

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Introduction to Parsing

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

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Abstract Syntax Trees & Top-Down Parsing

CSE 401 Midterm Exam Sample Solution 2/11/15

Theory and Compiling COMP360

1 Lexical Considerations

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Chapter 3: Lexing and Parsing

Lexical Analysis. Introduction

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CS Exam #1-100 points Spring 2011

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Syntax Analysis Check syntax and construct abstract syntax tree

CS 4120 Introduction to Compilers

Lexical Analysis. Lecture 3-4

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

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

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

Transcription:

S 164, Spring 2010 S 164: P. N. Hilfinger 1. [2 points] onsider the following very simple grammar: p : s s : /* Empty */ s : ( s ) Suppose that we have a shift-reduce parser for this grammar. Write a regular expression that describes the possible contents of the parsing stack just after a shift or reduce. "("+s")" "("*s? or, if you include the final shifting of and reduction to p, "("+s")" "("*s? s p 2. [1 point] I have a language implementation that involves a Flex lexer. new revision of the language standard requires me to double the number of different kinds of token returned by the lexer. What effect will this have on the lexer s speed and why? (ssume that both new and old lexer action rules (the things in curly braces) all take about the same time to execute.) Very little if any difference. These lexers are table-driven, doing one table lookup for each character. Unless the average length of a lexeme increases, the time spent will therefore tend not to change. 3. [1 point] From a given grammar, I am able to produce two different derivations of a certain string, S. Under what circumstances would this indicate that the grammar is ambiguous? If both were the same kind of derivation both leftmost or both rightmost, for example the grammar would have to be ambiguous. In general, the derivations would have to expand the same instance of a particular nonterminal in two different ways. 1

2 4. [3 points] n elevator travels back and forth between two floors. We can describe its operation in the abstract as a sequence of events. It receives a command () to move up or down to the other floor, it starts moving up (U) or down (D), and it arrives at the other floor (), after which the cycle can repeat. fter the elevator starts moving and before it arrives, it can receive a command () to return to the floor it just came from, in which case it will start back in the opposite direction after it arrives () at the floor it s going to. So in the abstract, we can describe a day on the elevator with a sequence of letters from the alphabet {,,D,U}. Design a DF that recognizes the legal sequences of events, assuming that the elevator starts on the lower floor (motionless), and ends on either floor with no commands pending. Ignore the possibility of redundant commands (receiving two s that command the same thing before finishing the first). For example, UDU is valid, but UD, UU, U, and U are not. U D

3 5. [1 point] What is the name of the dealer you d go to for...answers oracular, Bogies spectacular, Tetrapods tragical, Mirrors so magical, Facts astronomical, Solemn or comical,...? John Wellington Wells (Gilbert & Sullivan, The Sorcerer). 6. [5 points] The following grammar describes the structure of a tree-like specification language: spec : block block : TG ID attrs kids attrs : /* EMPTY */ attr attrs kids : /* EMPTY */ BEGIN blocklist blocklist : END block blocklist attr : ID = NUM Upper-case names and quoted symbols denote terminal symbols (returned by the lexer). ssume the following are already implemented: # The syntactic category of the next token (ID, BEGIN, END, =, etc.). def peek():... # If token is not None, check that peek() == token. Then discard the # current value of peek() and replace it with the next token in the input. # Indicate an error if the check fails. def scan(token = None):... # Report error def ERROR():... On the next page, complete a recursive-descent parser for this language. Do recognition only; don t worry about semantic values.

4 def spec(): block(); scan( ) # omplete the implementation here. def block(): scan(tg); scan(id); attrs(); kids() def attrs(): if peek() == ID: attr(); attrs() # The following two lines are optional---the error could be caught later. elif peek() not in [ BEGIN, TG, END, ]: ERROR() def kids(): if peek() == BEGIN: scan(begin); blocklist() # The following two lines are optional---the error could be caught later. elif peek() not in [ TG, END, ]: ERROR() def blocklist(): if peek() == END: scan(end) else: block(); blocklist() # or replace the last two lines with # elif peek() == TG: # block(); blocklist() # else: # ERROR def attr(): scan(id); scan( = ); scan(num)

5 7. [4 points] onsider the following reverse derivation: I P I I N P I t P I I N P I t P t I N P I t I N P I t as I N P I t as I t P I t as I t P t t as I t t as a t as e p Upper-case letters (and ) are terminal symbols; lower-case letters are nonterminals. a. Show the parse tree corresponding to this derivation. e as as a p t t t t t t Problem continues on next page I P I I N P I

6 b. Show as much as you can of the grammar corresponding to this derivation. p : e e : t as t : I t : N t : t P t as : ǫ as : as a a : I t c. Is the grammar ambiguous? Why or why not? Yes. The production t : t P t for example makes parsing I P I P I ambiguous. d. Starting with p, show the first five derivation steps (the first five steps) in the leftmost derivation that corresponds to the same parse tree. p e t as t P t as I P t as.

7 8. [4 points] very primitive text formatter for mathematics allows one to write things like x^2+y_{i+1}^{3}-c_{q_{j}} and get x 2 +y 3 i+1 c q j. The grammar below gives the syntax for the input language: /* Semantic values are tuples (<BOX>, <WIDTH OF BOX>) */ eqn : text { output($1[0]); } text : /*EMPTY*/ { $$ = (box(), 0); } text item { $$ = (add ($1[0], $2[0], $1[1], 0), $1[1]+$2[1]); } item : GLYPH sub0 sup0 { t = add ($1, $2[0], 1, -1) t = add (t, $3[0], 1, 1) $$ = (t, 1 + max($2[1], $3[1])) } sub0 : /*EMPTY*/ { $$ = (box(), 0) } _ { text } { $$ = $3 } sup0 : /*EMPTY*/ { $$ = (box(), 0) } ^ { text } { $$ = $3 } The terminal symbols are GLYPH, {, }, _, and ^. The object that you output is a box, a container of printable text, which is defined for you in the form of two functions: box() returns an empty box. add(b 1,B 2,x,y), where B 1 and B 2 are boxes and x and y are numbers, returns a new box containing B 1 s text, with B 2 s text positioned so that its lower-left corner is at coordinates (x,y) relative to B 1 s lower-left corner. Thus if B 1 contains the text hello and B 2 contains the text world, then add(b 1,B 2,6,1) would output as hello world. For each GLYPH The lexical analyzer returns a box containing text of width 1. Superscripts and subscripts are one unit above and below the text they are attached to, respectively. We will not bother with decreasing the font size of subscripts and superscripts for this problem. Fill in the blanks above to achieve the desired effect. Your semantic actions must not have side effects, except to assign to $$. You may, however, use any Python values and data structures you d like for semantic values (numbers, boxes, dictionaries, lists, etc.).