WARNING for Autumn 2004:

Similar documents
Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

CSCI312 Principles of Programming Languages!

Lexical Analysis. Introduction

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

CSE 582 Autumn 2002 Exam 11/26/02

Outline. 1 Scanning Tokens. 2 Regular Expresssions. 3 Finite State Automata

Announcements! P1 part 1 due next Tuesday P1 part 2 due next Friday

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

Lexical Analysis 1 / 52

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

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

Postscript Control Flow

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CSc 453 Lexical Analysis (Scanning)

CSc 453 Compilers and Systems Software

Building lexical and syntactic analyzers. Chapter 3. Syntactic sugar causes cancer of the semicolon. A. Perlis. Chomsky Hierarchy

Formal Languages and Compilers Lecture VI: Lexical Analysis

Assignment 1 (Lexical Analyzer)

Compiler phases. Non-tokens

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Part 5 Program Analysis Principles and Techniques

CSE 401 Midterm Exam Sample Solution 2/11/15

Assignment 1 (Lexical Analyzer)

A Scanner should create a token stream from the source code. Here are 4 ways to do this:

CSE 401/M501 Compilers

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Compiler Construction LECTURE # 3

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Lexical Analysis. Chapter 2

CS 536 Midterm Exam Spring 2013

CS 403: Scanning and Parsing

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

UNIT -2 LEXICAL ANALYSIS

Lexical Analysis. Chapter 1, Section Chapter 3, Section 3.1, 3.3, 3.4, 3.5 JFlex Manual

CSE Lecture 4: Scanning and parsing 28 Jan Nate Nystrom University of Texas at Arlington

Languages, Automata, Regular Expressions & Scanners. Winter /8/ Hal Perkins & UW CSE B-1

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

Chapter 3 Lexical Analysis

Postscript Intro. References. What is Postscript? Hello World! in Ghostscript. CSE 413, Autumn 2005 Programming Languages. Page description language

CSE302: Compiler Design

Compiler course. Chapter 3 Lexical Analysis

Figure 2.1: Role of Lexical Analyzer

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Computational Expression

Compiling Regular Expressions COMP360

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

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

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

CMSC 350: COMPILER DESIGN

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Lexical Analysis. Lecture 2-4

CSE 3302 Programming Languages Lecture 2: Syntax

CS 415 Midterm Exam Spring SOLUTION

Introduction to Lexing and Parsing

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

Following is the general form of a typical decision making structure found in most of the programming languages:

Lexical Analysis. Lecture 3. January 10, 2018

Introduction to Programming Using Java (98-388)

Computer Science Spring 98 Midterm Examination Professor L. Rowe

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Lexical and Syntax Analysis

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

Projects for Compilers

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

Lexical Scanning COMP360

LECTURE 3. Compiler Phases

Lecture 4: Syntax Specification

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

Optimizing Finite Automata

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

Lexical Analysis. Implementation: Finite Automata

A simple syntax-directed

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

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

CSE P 501 Exam 8/5/04

BSCS Fall Mid Term Examination December 2012

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

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

CPS 506 Comparative Programming Languages. Syntax Specification

Introduction to Lexical Analysis

CS 314 Principles of Programming Languages

PROGRAMMING FUNDAMENTALS

CSCE 531 Spring 2009 Final Exam

INFOB3TC Exam 2. Sean Leather. Monday, 30 January 2012, 17:00 20:00

Project 1 Using Lex and Yacc for Higher Level Abstraction CISC 471 Compiler Design

CS 314 Principles of Programming Languages. Lecture 3

Week 2: Syntax Specification, Grammars

2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

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

UNIT II LEXICAL ANALYSIS

CS 44 Exam #2 February 14, 2001

Chapter 3. Parsing #1

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

Midterm I - Solution CS164, Spring 2014

Transcription:

CSE 413 Programming Languages Autumn 2003 Max Points 50 Closed book, closed notes, no electronics. Do your own work! WARNING for Autumn 2004 Last year s exam did not cover Scheme and Java, but this year s final will cover all course material (except Smalltalk). In particular, the following material will be covered but was not tested or not covered last year -Scheme -Java -Logic programming languages December 16, 2003 Page 1 of 6

1. Consider the regular expression s = [A-Z][a-z]*([a-z]+)* [3 Points] Describe in English the language generated by s, ie L(s). 2. Consider the regular expression t = 0 [12](0 1 2)* [6 Points = 2 * 3] a. What is the alphabet over which t is defined? b. Give two examples of strings that are members of L(t). c. Is L(t) a finite set or an infinite set? 3. Consider the following deterministic finite automaton. [4 points = 2 * 2] start 1 [A-Z] [A-Z] 2 3 [A-Z] 5 0 4 [0-9] [1-9] 6 7 [0-9] 8 9 a. Write down a regular expression that generates the language recognized by this DFA. b. Write down the series of states that this DFA goes through for the following inputs and indicate whether or not the input is accepted. Input string DUA0 MUC2 List of states by number Accepted? Yes or No December 16, 2003 Page 2 of 6

4. Consider the regular expression r = [ch][ao][dt] [9 points = 3 * 3] a. Describe in English the language generated by r. b. Draw a DFA that accepts the language generated by r. Use the same symbology as I did in the previous problem to indicate states, transitions, and accept states. c. Write a Java method that takes a String object as a parameter and returns a boolean value indicating either true, the String is accepted by the DFA, or false, the String is not accepted by the DFA. String instance methods that may be useful to you are int length() returns the number of characters in this string. char charat(int idx) returns the character at the given index in this string. boolean equals(object o) returns true if o and this string are the same. December 16, 2003 Page 3 of 6

5. Consider the following language grammar G. There are two non-terminals defined in this grammar type and simple. The start symbol is type. id is a terminal whose attribute is the string value of the associated identifier. num is a terminal whose attribute is the numeric value of the associated number. dotdot is the 2-character string "..". type = simple id array [ simple ] of type simple = integer char num dotdot num a. What is the set of all terminals in grammar G? [2 Points] b. Give an example of a string in L(G). [2 Points] c. Show the parse tree generated by a top-down leftmost derivation of the following string. array [ integer ] of rocket [4 Points] December 16, 2003 Page 4 of 6

6. Consider the following grammar J. The start symbol is program. Only one main method is allowed, the one in mainclass. Lexical issues Grammar J Integer literals are identified by the scanner and assigned a token type of integervalue with an associated numeric value attribute. Identifiers are identified by the scanner and assigned a token type of id with an associated string value attribute. The keywords are reserved and cannot be used as identifiers. End-of-line is not significant and is treated as whitespace. program mainclass mainclass otherclass mainclass class id { public static void main ( String id ) { statementlist otherclass class id { methoddecl methoddecl public type id (type id) {statementlist return exp; type boolean int id statementlist statement statementlist ε statement { statementlist id = exp ; if ( exp ) statement else statement exp true false id integervalue a. Can we define class A, class B, and class C all in one program? Yes No [1 Point] b. Can the main method have more than one argument? Yes No [1 Point] c. Can methods other than main have a boolean argument? Yes No [1 Point] d. What is the shortest valid program in this language L(J)? [2 Points] e. Using grammar J, is there a syntax error in this program? Yes or No. If yes, identify it. [2 Points] class A { public static void main ( String s ) { if (s) { a = 1; else a = 0; b = true; class B { public boolean assert(a d) { return true; December 16, 2003 Page 5 of 6

7. Consider the following Postscript code. Assume that the stack is empty before each snippet of code executes. Show what is on the stack after the code executes. [3 Points =1 * 3] a. 3 2 3 add mul b. /a 2 def c. 1 2 3 4 exch pop 8. What do the following snippets of code draw on the page? [4 Points = 2 * 2] a. newpath 100 100 moveto /w 50 def w w rlineto stroke b. /inch {72 mul def newpath 100 100 moveto true {0 1 inch {5 inch 0 ifelse rlineto stroke 9. Convert the following Java expressions to Postscript. You can assume that the variable names, procedure names and argument orders are the same in both languages. [6 Points = 2* 3] a. Java (2*PI)+1 Postscript b. Java sin(idx*pi/10) Postscript c. Java moveto(50,idx*5) Postscript December 16, 2003 Page 6 of 6