Chapter 4. Action Routines

Similar documents
COP4020 Programming Languages. Semantics Prof. Robert van Engelen

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Static and Dynamic Semantics

Programming Languages

Chapter 4 :: Semantic Analysis

Chapter 4 - Semantic Analysis. June 2, 2015

Semantic Analysis. Role of Semantic Analysis

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

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

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation

CSE302: Compiler Design

Syntax-Directed Translation

CS A331 Programming Language Concepts

Syntax-Directed Translation. Introduction

Syntax Directed Translation

Abstract Syntax Tree

Abstract Syntax Trees Synthetic and Inherited Attributes

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

Syntax-Directed Translation Part I

Summary: Semantic Analysis

Semantic Analysis Attribute Grammars

Chapter 3 (part 3) Describing Syntax and Semantics

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Syntax-Directed Translation Part II

Syntax-Directed Translation

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

Syntax-Directed Translation

COP4020 Spring 2011 Midterm Exam

Syntactic Directed Translation

2068 (I) Attempt all questions.

MIDTERM EXAM (Solutions)

LECTURE 3. Compiler Phases

COS 320. Compiling Techniques

UNIT IV INTERMEDIATE CODE GENERATION

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COP5621 Exam 3 - Spring 2005

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Abstract Syntax Trees & Top-Down Parsing

Principles of Programming Languages

COMPILER DESIGN. Syntax-Directed Translation. COMP 442/6421 Compiler Design. Department of Computer Science and Software Engineering

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Syntax Directed Translation

Lecture 14 Sections Mon, Mar 2, 2009

Yacc: A Syntactic Analysers Generator

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 2: Syntax Directed Translation and YACC

5. Syntax-Directed Definitions & Type Analysis

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

4. Semantic Processing and Attributed Grammars

Semantic actions for declarations and expressions

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Single-pass Static Semantic Check for Efficient Translation in YAPL

G Programming Languages - Fall 2012

Semantic actions for declarations and expressions. Monday, September 28, 15

Syntax-Directed Translation

How do LL(1) Parsers Build Syntax Trees?

Syntax Directed Translation

CS 406: Syntax Directed Translation

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

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

Defining Languages GMU

Recursive Descent Parsers

Chapter 3. Describing Syntax and Semantics

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

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

LR Parsing LALR Parser Generators

List of Figures. About the Authors. Acknowledgments

Chapter 3. Describing Syntax and Semantics

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Semantic actions for declarations and expressions

Principles of Programming Languages

Semantic Analysis. Lecture 9. February 7, 2018

Building Compilers with Phoenix

CS 536 Midterm Exam Spring 2013

CSE P 501 Exam 8/5/04

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

More On Syntax Directed Translation

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

CSCI Compiler Design

CST-402(T): Language Processors

Programming Languages Third Edition. Chapter 7 Basic Semantics

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Project Compiler. CS031 TA Help Session November 28, 2011

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

CSE3322 Programming Languages and Implementation

Compiler Design Aug 1996

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Table-Driven Top-Down Parsers

Semantic Analysis with Attribute Grammars Part 3

Transcription:

Chapter 4 Action Routines

Syntax and Semantics In general: Syntax form Semantics meaning In programming languages: Syntax part of the language definition that can be described via a context-free grammar Semantics part of language definition that can t.

Non-Syntax Items Can t be enforced by a context-free grammar: Variables must be declared before use Variables must be assigned before use Type compatibility in expressions Number and type of arguments must match the number and type of formal parameters Procedure must contain a return statement for each execution path

Semantics Recall, semantics can be broken into: Static semantics can be enforced by the compiler at compile time Dynamic semantics enforced at run-time, probably via code generated at compile time by the compiler.

Evaluating Attribute Grammars Attribute grammar variables can be characterized as: Intrinsic look-up value Synthesized Variables on the left side of a production get attribute values calculated from values on the right side of the production. In other words, attribute values come from values lower in the parse tree. Inherited Variables on the right side of a production get attribute values from items on the left side of the production or items to the left of the variable. In other words, values come from parents or siblings on the right in the parse tree.

Synthesized Attributes Simple attribute grammar for constant expressions using standard arithmetic operations Figure 4.1, page 181

Evaluating Attributes

Synthesized & Inherited Attributes Attribute grammar for constant expressions based on LL(1) context-free grammar. Several productions have two semantic rules Figure 4.3, page 186

Evaluating Attributes - Example

Attribute Grammars Semantic analysis and code generation can be described in terms of annotation, or decoration of a parse or syntax tree Attribute grammars provide a formal framework for decorating such a tree

Example Context-Free Grammar E E + T E E T E T T T * F T T / F T F F - F Says nothing about what the program means

Example Attribute Grammar E E + T E1.val = E2.val + T.val E E T E1.val = E2.val - T.val E T E.val = T.val T T * F T1.val = T2.val * F.val T T / F T1.val = T2.val / F.val T F T.val = F.val F - F F1.val = - F2.val

Assertions Some languages allow programmers to write logical assertions about values of program data Example: assert denominator!= 0 C allows via library routine assert Ada and Java have built-in language support for assertions Euclid and Eiffel include explicit support for invariants, preconditions and post-conditions

Action Routines Action routine a semantic function that a grammar writer can associate with a grammar for the compiler to execute at particular points during parsing Action routines are more flexible than attribute grammars.

Action Routines Action routines routines attached to a grammar which is executed by the compiler as it parses code Often used in production compilers Can be used to build parse trees

Example for LL Parsing LL parsing action routines can appear anywhere within a right-had side. Pointer to the action routine is pushed onto the stack and is executed when it is at the top of the stack. Thus: Action routine at the beginning is executed when the parser predicts the production Routine embedded in the middle of the righthand side is executed when the parser matches the symbol to its left

Action Routine Example Consider the grammar: E T TT TT + T TT - T TT ε T F FT FT * F FT / F FT ε F - F F ( E ) F const Figure 4.9 in text, page 195

Action Routine Example Action routines added: E T {TT.st:=T.ptr} TT {E.ptr:=TT.ptr} TT 1 T {TT 2.st:=make_bin_op( +,TT 1.st,T.ptr)} TT 2 {TT 1.ptr:=TT 2.ptr} TT 1 T {TT 2.st:=make_bin_op( -,TT 1.st,T.ptr)} TT 2 {TT 1.ptr:=TT 2.ptr} TT ε {TT.ptr:=TT.st} T F {FT.st:=F.ptr} FT {T.ptr:=FT.ptr}

Action Routine Example - continued FT 1 F {FT 2.st:=make_bin_op( x,ft 1.st,F.ptr)} FT 2 {FT 1.ptr:=FT 2.ptr} FT 1 F {FT 2.st:=make_bin_op( /,FT 1.st,F.ptr)} FT 2 {FT 1.ptr:=FT 2.ptr} FT ε {FT.ptr:=FT.st} F 1 F F 2 {F 1.ptr:=make_un_op( +/-,FT 2.ptr)} (E) {F.ptr:=E.ptr} F const {F.ptr:=make_leaf(const.ptr)}

Build Tree Use the previous grammar with action routines to build a tree for the expression: 2 * 3 + 5

Build Tree Stack: F F const {F.ptr:=make_leaf(const.ptr)} FT T TT E

Build Tree Stack: FT 1 * F {FT 2.st:= make_bin_op( x,ft 1.st, F.ptr)} FT 2 {FT 1.ptr:= FT 2.ptr} * F FT TT

Simplified Action Routine Example Demonstrating action routines using a more simple (LR) grammar. E E + T E E T E T T T * F T T / F T F F - F F (E) F const

Simplified Action Routine Example More simplified example: E E + T E 1.ptr:= make_bin_op( +, E 2.ptr, T.ptr) E E - T E 1.ptr:= make_bin_op( -, E 2.ptr, T.ptr) E T E.ptr:= T.ptr T T * F T 1.ptr:= make_bin_op( x, T 2.ptr, F.ptr) T T / F T 1.ptr:= make_bin_op( /, T 2.ptr, F.ptr) T F T.ptr:= F.ptr F - F F 1.ptr:= make_un_op( +/-, F 2.ptr) (+/- - indicates a change of sign, similar to on calculators) F (E) F.ptr:=E.ptr F const F.ptr:=make_leaf(const.val)