CS Programming Languages Fall Homework #1

Similar documents
Chapter 4: Basic C Operators

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

Principles of Programming Languages COMP251: Syntax and Grammars

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

CPS 506 Comparative Programming Languages. Syntax Specification

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

A Simple Syntax-Directed Translator

More Assigned Reading and Exercises on Syntax (for Exam 2)

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

Computational Expression

Introduction to Programming (Java) 2/12

Compilers for Modern Architectures Course Syllabus, Spring 2015

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

Introduction to Computer Science Midterm 3 Fall, Points

UNIVERSITY OF CALIFORNIA

1 Lexical Considerations

COP 3402 Systems Software Syntax Analysis (Parser)

Chapter 3: Operators, Expressions and Type Conversion

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

JAVA OPERATORS GENERAL

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Syntax-Directed Translation. Lecture 14

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

Operators and Expressions:

Operators And Expressions

Compiler Construction

Review of the C Programming Language

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

Review of the C Programming Language for Principles of Operating Systems

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Operators & Expressions

CS415 Compilers. Lexical Analysis

Operators in C. Staff Incharge: S.Sasirekha

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Data Types and Variables in C language

CS Programming Languages Fall Homework #2

SECTION II: LANGUAGE BASICS

Arithmetic Operators. Portability: Printing Numbers

CS202 Compiler Construction. Christian Skalka. Course prerequisites. Solid programming skills a must.

CS4120/4121/5120/5121 Spring /6 Programming Assignment 4

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

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Fundamentals of Programming Session 7

Operators and Expressions

The Substitution Model

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

ECE251 Midterm practice questions, Fall 2010

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

Lexical Considerations

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

Principles of Programming Languages COMP251: Syntax and Grammars

Language Reference Manual simplicity

CS 330 Homework Comma-Separated Expression

Homework Assignment #3

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Department of Computer Science

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

Compiler Construction

Operators. Java operators are classified into three categories:

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Declaration and Memory

More Programming Constructs -- Introduction

QUIZ: What value is stored in a after this

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

cmps104a 2002q4 Assignment 3 LALR(1) Parser page 1

Expressions and Precedence. Last updated 12/10/18

Lecture 3 Operators MIT AITI

UNIT- 3 Introduction to C++

CSCI312 Principles of Programming Languages!

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only

Chapter 3 Structure of a C Program

Lexical Considerations

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Syntax and Grammars 1 / 21

Part I Part 1 Expressions

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Formal Languages and Automata Theory, SS Project (due Week 14)

Engineering Computing I

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

Operators in java Operator operands.

DO NOT OPEN UNTIL INSTRUCTED

Syntax Intro and Overview. Syntax

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

Decaf PP2: Syntax Analysis

Class Information ANNOUCEMENTS

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

CSCI 3155: Lab Assignment 6

TDDD55 - Compilers and Interpreters Lesson 3

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Transcription:

CS 45 - Programming Languages Fall 200 Homework # Due: 2pm CDT (in class), September 2, 200 YOUR NAME: Collaboration policy No collaboration is permitted on this assignment. Any cheating (e.g., submitting another person s work as your own, or permitting your work to be copied) will automatically result in a failing grade. The Computer Science Department Code of Conduct can be found at http://www.cs.utexas.edu/academics/conduct/. Late submission policy This homework is due at the beginning of class on September 2. All late submissions will be subject to the following policy. You start the semester with a credit of late days. For the purpose of counting late days, a day is 24 hours starting at 2pm on the assignment s due date. Partial days are rounded up to the next full day. You are free to divide your late days among the take-home assignments any way you want: submit four assignments day late, submit one assignment days late, etc. After your days are used up, no late submissions will be accepted and you will automatically receive 0 points for each late assignment. You may submit late assignments to Vitaly Shmatikov (CSA.4 slide under the door if the office is locked). If you are submitting late, please indicate how many late days you are using. Write the number of late days you are using:

Homework # (5 points) Problem (4 points) Describe some design tradeoffs between efficiency and safety or reliability in any programming language you know. Problem 2 (4 points) By hand, using a top-down recursive descent approach, derive the complete parse tree for the simple C program below, showing each non-terminal interior node and each terminal leaf note. int main() { int x; if (x == 0) return ; else return 2*x; } 2

Problem The purpose of this problem is to introduce you to lexical and syntactic analysis, and to help you gain some experience with Flex, Bison, and C. You will start with a simple integer calculator. Its Flex and Bison specifications can be found here, in the files called, respectively, icalc.l and icalc.y: http://www.cs.utexas.edu/~shmat/courses/cs45/icalc.tar.gz Your goal is to extend the calculator by adding new lexical specifications and BNF grammar rules for correctly evaluating new types of expressions. The operators and their associativity rules are given below, listed from lowest to highest precedence. Parentheses ( ) can be used to override the default precedence rules. Operators Category Associativity =, +=, =, =, /=, %= binary assignment right-to-left? : ternary conditional right-to-left binary logical OR left-to-right && binary logical AND left-to-right <, >, <=, >= binary relational left-to-right ==,! = binary equality/inequality left-to-right +, binary additive left-to-right, /, % binary multiplicative left-to-right ˆ binary exponentiation right-to-left, + unary minus/plus right-to-left ++, postfix increment/decrement left-to-right ++, prefix increment/decrement right-to-left Submission instructions. Submit a paper printout of your Flex and Bison code, stapled to the first page of this homework (the one showing your name and the number of late days you are using, if any). The printout must be processed using the following command: enscript -C -2Gr -Ec <yourfile> -o <outputfile.ps> 2. Submit your source code electronically using the following command: turnin --submit austin hw2 <filename> <filename2>... Problem a (6 points) Add twenty-six 2-bit registers, labeled a through z. Users should be able to store values in registers and use them in subsequent expressions in the calculator. A register variable by itself evaluates to the value currently stored in the register. Registers should all be initialized to 0 when the calculator starts.

You should also implement assignment expressions of the form reg = expr, reg += expr, reg -= expr, reg *= expr, reg /= expr, and reg %= expr. Each assignment expression evaluates to the value of the expression on the right-hand side, updates the register on the left-hand side, and prints the result. Note the associativity of the assignment operators (see the table above). icalc: b 0 icalc: b= icalc: b *= 5 + a 5 icalc: a = b = c = 7 7 icalc: z += a*b + c 56 Problem b (4 points) Add C-style unary increment and decrement operators ++ and --, ensuring that they can only be applied to registers (i.e., ++2 is a syntax error). Pay attention to the precedence of these operators vs. binary operators. These operators should have the same semantics as in C. icalc: x= icalc: x++ icalc: x 2 icalc: ++x icalc: ---x syntax error icalc: -(--x) -2 Problem c ( points) Implement built-in constants MAXINT and MININT, assuming signed two s-complement 2-bit integers. icalc: MAXINT 24748647 4

icalc: MININT -24748648 Problem d (4 points) Implement a binary exponentiation operator ˆ that raises the first argument to the power of the second argument (note the associativity of this operator in the table above). Feel free to look at the standard C function pow(x,y), as defined in math.h. It computes x^y up to MAXINT and -x^y up to MININT, and pow(x,0)= for all x. icalc: 2^5 2 icalc: 2^0 icalc: -2^ -24748648 icalc: 2^ 24748647 icalc: 2^00 24748647 Problem e (4 points) Implement built-in functions abs, min and max as prefix operators which take a commaseparated, parenthesized list of arguments that can be constants, registers, or expressions. icalc: abs(-5) 5 icalc: a 0 icalc: x=abs(a-2*) 6 icalc: min(abs(-2),min(5,)) icalc: max(x,2+2) 6 Problem f (6 points) Implement relational operators ==,!=, <, <=, >, >=, logical operators and &&, and the ternary conditional expression operator?: Their semantics should be the same as in C. 5