Program Assignment 2 Due date: 10/20 12:30pm

Similar documents
10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

10/30/18. Dynamic Semantics DYNAMIC SEMANTICS. Operational Semantics. An Example. Operational Semantics Definition Process

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

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

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Syntax and Semantics

Programming Languages

3. DESCRIBING SYNTAX AND SEMANTICS

Chapter 3. Describing Syntax and Semantics

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

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019

Chapter 3. Describing Syntax and Semantics. Introduction. Why and How. Syntax Overview

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

A Simple Syntax-Directed Translator

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

COP5621 Exam 3 - Spring 2005

Programming Languages

CS 314 Principles of Programming Languages

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Semantic Analysis. Role of Semantic Analysis

Lexical and Syntax Analysis

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

CMSC 330: Organization of Programming Languages

Semantic Analysis Attribute Grammars

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Lecture 14 Sections Mon, Mar 2, 2009

Question Bank. 10CS63:Compiler Design

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

[Syntax Directed Translation] Bikash Balami

Lexical Analysis. Introduction

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

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

Chapter 4. Lexical and Syntax Analysis

Lexical Considerations

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

Syntax. In Text: Chapter 3

COP4020 Spring 2011 Midterm Exam

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

Chapter 4 - Semantic Analysis. June 2, 2015

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

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

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

Left to right design 1

Abstract Syntax Trees Synthetic and Inherited Attributes

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

CSCE 314 Programming Languages

Defining Languages GMU

Syntax-Directed Translation. Lecture 14

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

1 Lexical Considerations

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Syntax-Directed Translation Part II

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Summary: Semantic Analysis

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

4. Lexical and Syntax Analysis

CSE 3302 Programming Languages Lecture 2: Syntax

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

CS 314 Principles of Programming Languages

Syntax-Directed Translation

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

4. Lexical and Syntax Analysis

COP4020 Programming Assignment 2 - Fall 2016

Compiler Construction

Programming Language Syntax and Analysis

Parsing II Top-down parsing. Comp 412

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

Lexical and Syntax Analysis. Top-Down Parsing

Decaf Language Reference

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Syntax Analysis Part I

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

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

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

Introduction to Parsing. Lecture 5

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

Compilers. Compiler Construction Tutorial The Front-end

Abstract Syntax Trees & Top-Down Parsing

More On Syntax Directed Translation

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Syntax and Grammars 1 / 21

Lexical and Syntax Analysis

COP4020 Programming Assignment 2 Spring 2011

Wednesday, September 9, 15. Parsers

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

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

Semantic Analysis. Lecture 9. February 7, 2018

Chapter 4 :: Semantic Analysis

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

Principles of Programming Languages COMP251: Syntax and Grammars

CMSC 330: Organization of Programming Languages

Languages and Compilers

Transcription:

Decoration of parse tree for (1 + 3) * 2 N. Meng, S. Arthur 1 Program Assignment 2 Due date: 10/20 12:30pm Bitwise Manipulation of Hexidecimal Numbers CFG E E A bitwise OR E A A A ^ B bitwise XOR A B B B & C bitwise AND B C C < C bitwise shift left 1 C > C bitwise shift right 1 C ~ C bitwise NOT C ( E ) C hex N. Meng, S. Arthur 2 1

LL(1) Attribute Grammar E A EE EE.st = A.val E.val = EE.val EE 1 A EE 2 EE 2.st = EE 1.st A.val ( bitwise OR ) EE 1.val = EE 2.val EE ε EE.val = EE.st A B AA AA.st = B.val A.val = AA.val AA 1 ^ B AA 2 AA 2.st = AA 1.st ^ B.val ( ^ bitwise XOR ) AA 1.val = AA 2.val AA ε AA.val = AA.st B C BB BB.st = C.val B.val = BB.val BB 1 & C BB 2 BB 2.st = BB 1.st & C.val ( & bitwise AND ) BB 1.val = BB 2.val BB ε BB.val = BB.st C 1 <C 2 C 1.val = C 2.val << 1 ( << bitwise shift left one ) C 1 >C 2 C 1.val = C 2.val >> 1 ( >> bitwise shift right one ) C 1 ~C 2 C 1.val = ~C 2.val ( ~ bitwise NOT ) C ( E ) C.val = E.val C hex C.val = hex.val 3 Program Requirement Write a C program using recursive descent parser w/ lexical analyzer to implement the designated inherited and synthesized attributes. The program evaluates the expressions in a file input.txt, and outputs the results to console E.g., input: f&a output: f&a = a N. Meng, S. Arthur 4 2

Program Requirements You cannot use more than 2 global/nonlocal variables, and they should be to hold the Operator and HexNumber as detected by the lexical analyzer N. Meng, S. Arthur 5 Hints To solve the problems, you should take the following steps: Write a lexical analyzer Write a recursive-descent parser Attributes are processed as either pass-in parameters or return value of functions N. Meng, S. Arthur 6 3

Hints Write a lexical analyzer You may need to define an enum type for all possible tokens your scanner can generate E.g., when reading hexadecimial numbers 0-9 or a-f, the recognized token is HEX, and the value is saved in HexNumber N. Meng, S. Arthur 7 Hints Write a recursive-descent parser Parse the program by defining and invoking functions E.g., E A EE EE.st = A.val E.val = EE.val int E() { int val = A(); return EE(val); } N. Meng, S. Arthur 8 4

Hints There are parameters passed in or returned when invoking functions. When invoking a function, the synthesized attribute is the return value, while the inherited attribute is the passing-in parameter N. Meng, S. Arthur 9 Hints Sample code of main() int main() { int val; symbol = getnexttoken(); while (symbol!= EOF_) { if (symbol!= NEW_LINE) { val = E(); printf(" = %x\n", val & 0xf); } if (symbol == EOF_) break; symbol = getnexttoken(); } return 1; } N. Meng, S. Arthur 10 5

Submission Requirements Pack the following files into a.tar file: Source file: parser.c Executable file: parser Input file: input.txt Output file: output.txt (copy all your console outputs to this file) README file (optional, used if you have any additional comments/explanations about the files) N. Meng, S. Arthur 11 DYNAMIC SEMANTICS N. Meng, S. Arthur 12 6

Dynamic Semantics Describe the meaning of expressions, statements, and program units No single widely acceptable notation or formalism for describing semantics Two common approaches: Operational Denotational N. Meng, S. Arthur 13 Operational Semantics Gives a program's meaning in terms of its implementation on a real or virtual machine Change in the state of the machine (memory, registers, etc.) defines the meaning of the statement N. Meng, S. Arthur 14 7

Operational Semantics There are different levels of operational semantics Big-Step Semantics: At the highest level, natural operational semantics are used to describe the final execution result of a complete program Small-Step Semantics: At the lowest level, structural operational semantics are used to determine the precise meaning of a single statement How does the statement change the state of a real/ virtual machine, such as memory and registers? N. Meng, S. Arthur 15 Operational Semantics Definition Process 1. Design an appropriate intermediate language. Each construct of the intermediate language must have an obvious and unambiguous meaning 2. Construct a virtual machine (an interpreter) for the intermediate language. The virtual machine can be used to execute either single statements, code segments, or whole programs N. Meng, S. Arthur 16 8

An Example C for (expr1; expr2; expr3) {... } Operational Semantics expr1; loop: if expr2 == 0 goto out... expr3; goto loop out:... The virtual computer is supposed to be able to correctly execute the instructions and recognize the effects of the execution N. Meng, S. Arthur 17 A Simple Language of Arithmetic Expressions [2] CFG e ::= n e1 + e2 e1 * e2 We are curious about: What is the meaning of a given ARITH expression? How do we evaluate expression? N. Meng, S. Arthur 18 9

Operational Semantics of Arithmetic Expressions [2] Specify how expressions should be evaluated Defined by cases on the form of expressions n evaluates to n n is a normal form, an expression that cannot be reduced further e 1 + e 2 evaluates to n if e 1 evaluates to n 1, e 2 evaluates to n 2, and n is the sum of n 1 and n 2 N. Meng, S. Arthur 19 Operational Semantics of Arithmetic Expressions [2] e 1 * e 2 evaluates to n if e 1 evaluates to n 1, e 2 evaluates to n 2, and n is the product of n 1 and n 2 N. Meng, S. Arthur 20 10

Big-Step Operational Semantics [2] n n e 1 n 1 e 2 n 2 n is the sum of n 1 and n 2 e 1 + e 2 n e 1 n 1 e 2 n 2 n is the product of n 1 and n 2 e 1 * e 2 n N. Meng, S. Arthur 21 Small-Step Operational Semantics [3] Describe a single step in the evaluation Show intermediate results and how to calculate each result Many steps may be needed to get a result N. Meng, S. Arthur 22 11

Small-Step Operational Semantics [3] n is the sum of n 1 and n 2 n 1 + n 2 -> n e 1 -> e 1 e 1 + e 2 -> e 1 + e 2 e 1 -> e 1 e 1 * e 2 -> e 1 * e 2 n is the product of n 1 and n 2 n 1 * n -> n 2 e 2 -> e 2 n 1 + e 2 -> n 1 + e 2 e 2 -> e 2 n 1 * e 2 -> n 1 * e 2 The semantic rules tell not only the operator meanings, but also evaluation orders E.g., (1+2) + (3+4) = N. Meng, 3 + S. Arthur (3+4) 23 Key Points of Operational Semantics Advantages May be simple and intuitive for small examples Good if used informally Useful for implementation Disadvantages Very complex for large programs Lacks mathematical rigor N. Meng, S. Arthur 24 12

Typical Usage of Operational Semantics Vienna Definition Language (VDL) used to define PL/I (Wegner 1972) Unfortunately, VDL is so complex that it serves no practical purpose N. Meng, S. Arthur 25 Denotational Semantics The most rigorous, widely known method for describing the meaning of programs Solely based on recursive function theory Originally developed by Scott and Strachey (1970) N. Meng, S. Arthur 26 13

Denotational Semantics Key Idea Define for each language entity both a mathematical object, and a function that maps instances of that entity onto instances of the mathematical object The basic idea There are rigorous ways of manipulating mathematical objects but not programming language constructs N. Meng, S. Arthur 27 Denotational Semantics Difficulty How to create the objects and the mapping functions? The method is named denotational, because the mathematical objects denote the meaning of their corresponding syntactic entities N. Meng, S. Arthur 28 14

Denotational vs. Operational Both denotational semantics and operational semantics are defined in terms of state changes in a virtual machine In operational semantics, the state changes are defined by coded algorithms in the machine In denotational semantics, the state change is defined by rigorous mathematical functions N. Meng, S. Arthur 29 Program State Let the state s of a program be a set of pairs as follows: {<i 1, v 1 >, <i 2, v 2 >,, <i n, v n >} Each i is the name of a variable The associated v is the current value of the variable Any v can have the special value undef, indicating that the associated variable is undefined Let VARMAP be a function as follows: VARMAP(i j, s) N. = Meng, v S. Arthur 30 j 15

Program State Most semantics mapping functions for programs and program constructs map from states to states These state changes are used to define the meanings of programs and program constructs Some language constructs, such as expressions, are mapped to values, not state changes N. Meng, S. Arthur 31 An Example CFG for binary numbers <bin_num> -> 0 <bin_num> -> 1 <bin_num> -> <bin_num> 0 <bin_num> -> <bin_num> 1 Parse tree of the binary number 110 <bin_num> <bin_num> 0 <bin_num> 1 1 N. Meng, S. Arthur 32 16

Example Semantic Rule Design Mathematical objects Decimal number equivalence for each binary number Functions Map binary numbers to decimal numbers Rules with terminals as RHS are translated as direct mappings from terminals to mathematical objects Rules with nonterminals as RHS are translated as manipulations on mathematical objects N. Meng, S. Arthur 33 Example Semantic Rules Syntax Rules <bin_num>-> 0 <bin_num>-> 1 <bin_num>-><bin_num> 0 <bin_num>-><bin_num> 1 Semantic Rules M bin ( 0 )=0 M bin ( 1 )=1 M bin (<bin_num> 0 )= 2*M bin (<bin_num>) M bin (<bin_num> 1 )= 2*M bin (<bin_num>)+1 N. Meng, S. Arthur 34 17

CFG for expressions Expressions <expr> -> <dec_num> <var> <binary_expr> <binary_expr> -> <l_expr> <op> <r_expr> <l_expr> -> <dec_num> <var> <r_expr> -> <dec_num> <var> <op> -> + * N. Meng, S. Arthur 35 Expressions M e (<expr>, s) Δ= case <expr> of <dec_num> M dec (<dec_num>) <var> VARMAP(<var>, s) <binary_expr> if (<binary_expr>.<op> = + ) then M e (<binary_expr>.<l_expr>, s) + M e (<binary_expr>.<r_expr>, s) else M e (<binary_expr>.<l_expr>, s) M e (<binary_expr>.<r_expr>, s) N. Meng, S. Arthur 36 18