ECE251 Midterm practice questions, Fall 2010

Similar documents
CPS 506 Comparative Programming Languages. Syntax Specification

UNIVERSITY OF CALIFORNIA

CSE 401 Midterm Exam Sample Solution 2/11/15

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

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

CSE 3302 Programming Languages Lecture 2: Syntax

Introduction to Lexing and Parsing

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

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

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

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

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

BSCS Fall Mid Term Examination December 2012

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CSE 401 Midterm Exam Sample Solution 11/4/11

2068 (I) Attempt all questions.

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

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Chapter 2 :: Programming Language Syntax

Question Points Score

A simple syntax-directed

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

CSE P 501 Exam 11/17/05 Sample Solution

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

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

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

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

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS 314 Principles of Programming Languages

Part 5 Program Analysis Principles and Techniques

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

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

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

QUESTIONS RELATED TO UNIT I, II And III

Programming Languages and Compilers (CS 421)

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

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

Syntax. A. Bellaachia Page: 1

Syntax Analysis Check syntax and construct abstract syntax tree

EECS 6083 Intro to Parsing Context Free Grammars

CS143 Midterm Fall 2008

Principles of Programming Languages COMP251: Syntax and Grammars

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Context-Free Grammars

Syntax. In Text: Chapter 3

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

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

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

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

CMSC 330: Organization of Programming Languages

Dr. D.M. Akbar Hussain

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Principles of Programming Languages COMP251: Syntax and Grammars

Week 2: Syntax Specification, Grammars

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

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

3. Context-free grammars & parsing

Midterm I (Solutions) CS164, Spring 2002

Compilers and computer architecture From strings to ASTs (2): context free grammars

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

Syntax and Parsing COMS W4115. Prof. Stephen A. Edwards Fall 2003 Columbia University Department of Computer Science

Lecture 4: Syntax Specification

CMSC 330: Organization of Programming Languages

CS 411 Midterm Feb 2008

Optimizing Finite Automata

Compiler Design Overview. Compiler Design 1

CMSC 330: Organization of Programming Languages

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

Related Course Objec6ves

LECTURE 3. Compiler Phases

CMSC 330: Organization of Programming Languages. Context Free Grammars

CSCI312 Principles of Programming Languages!

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

Topic 3: Syntax Analysis I

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

A Simple Syntax-Directed Translator

Semantic Analysis. Lecture 9. February 7, 2018

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Appendix Set Notation and Concepts

Chapter 3: Lexing and Parsing

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Syntax. 2.1 Terminology

Scanners. Xiaokang Qiu Purdue University. August 24, ECE 468 Adapted from Kulkarni 2012

CMSC 330: Organization of Programming Languages. Context Free Grammars

Context free grammars and predictive parsing

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

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Monday, August 26, 13. Scanners

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

Revisit the example. Transformed DFA 10/1/16 A B C D E. Start

Transcription:

ECE251 Midterm practice questions, Fall 2010 Patrick Lam October 20, 2010 Bootstrapping In particular, say you have a compiler from C to Pascal which runs on x86, and you want to write a self-hosting Java compiler, and you have an x86 processor. What s the best way to proceed? Regular Expressions Creating regular expressions and DFAs. To practice, you can create both regular expressions and DFAs for these. I recommend that you start with DFAs. a DFA which recognizes strings of a s, b s, x s and y s, where every x is immediately followed by a y. a regular expression which accepts strings over a, x, b where at least one a follows every b. Regexps. Recall that regexps are not the same as regular expressions. In particular, they can match groups (subexpressions of the regular expression). Write a regexp for perl interpolation. It should accept print statements with this syntax: print "$foo". $bar. baz ; So we have print statements with, as arguments, a sequence of strings, joined by the. operator. You should match variables contained in double-quotes only. Regular expressions to NFAs. Create any valid NFA for the following regexps: (ab? c(dd) ) x (xy ab) zw? 1

NFA execution. Show an accepting trace or explain why the following NFA does not accept the word xxxx. x x 1 2 3 x x x 4 5 6 7 What about the word bxyxy on this automaton? 1 2 a 3 x b x y 4 5 6 7 (You might find harder questions of this flavour on the midterm.) Lexing. Say you have these four token definitions: PRINT: print INT LITERAL: [+ ]? [0 9] + FLOAT LITERAL: [+ ]? [0 9] + (. [0 9] )? PLUS: + What is the proper lexing for these strings: PRINT 5.4 PRINT +2874. PRINT 27.928 PRINT 28+99 Why? 2

Declarative versus operational regular expression parsing. Recall that declarative regular expression parsing matches the longest possible substring match for a group, while operational parsing specifies that should match as many repetitions as possible while allowing the remainder to match and that should match the leftmost alternative while allowing the remainder to match. I can think of two possible questions: 1. Give me an example of which groups the declarative and operational semantics would match on a given string (or, given matches, tell me which is which); 2. Construct an example where the declarative and operational semantics give different answers. Interpreters You might find short answer question on interpreters, like: Describe the general structure of an interpreter. What is the difference between interpreters and compilers? General parsing questions Parse trees and ASTs Construct and compare a reasonable parse tree and an AST for the following code: if (x > 4) x = x * y + (z * 2); Briefly say why these are different. Language design and CFGs Propose a language and a context-free grammar for a language to describe graphs. Graphs consist of a list of vertices V and a set of edges E; each edge is a pair of vertices. Derivations Consider the following grammar: S := ABC A := [x D q B ] A B := y B C := ] D := qq 1) Provide two words that can be derived from this grammar. 2) Can the following words be derived from this grammar: [xqqqy]qq; [xq][qqqq] 3

Regexps versus CFGs. Short-answer questions about specific languages and if you would parse them using regular expressions or context-free grammars. Examples: You ve saved the HTML for a bunch of Facebook profiles and want to automatically extract peoples relationship statuses from them. How would you do this? What would change your answer? You have to parse some log files. Each log entry is a bunch of comma-delimited fields. What do you do? You need to validate user input on forms according to some simple syntax-based rules. What is the best way to do that? Parsing: Grammar manipulations A couple questions on parsing and grammars. EBNF. Convert EBNF to BNF; here is a EBNF grammar: What is a corresponding BNF grammar? S := (AB)?C A := t w B := x y C := z FIRST sets. Compute FIRST sets and decide when a production is nullable for the grammars you ve seen in this set of questions. Recursive-descent parsing. Generate pseudocode for a recursive-descent parser on an expression grammar. Or: can you produce a recursive-descent parser for a given grammar, say the one immediately above? Right-recursion and left-recursion. Here is a right-recursive statement list. stmt list := stmt ; stmt list What is the left-recursive equivalent? Dangling else. Briefly describe the dangling-else problem. Draw the two parse trees for dangling else and name 2 ways for dealing with the problem. 4

Stratified grammars. Consider a programming language with the and binary operators: E := E E E E Illustrate the ambiguity with two parse trees for the same expression. No w, assume that has higher precedence than. Give a stratified grammar which gets rid of the ambiguity. Shift-reduce conflicts. Give me an example of a grammar which contains a shift-reduce conflict. Write down a sentence in the language of the grammar where the parser should shift, and a sentence where it should reduce. Fix the grammar by delaying decisions. 5