Lecture 14 Sections Mon, Mar 2, 2009

Similar documents
Abstract Syntax Trees Synthetic and Inherited Attributes

Building the Abstract Syntax Trees

Introduction to Compiler Design

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COP5621 Exam 3 - Spring 2005

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

Recursive Descent Parsers

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Syntax-Directed Translation. Introduction

Semantic Analysis Attribute Grammars

Compilers. Compiler Construction Tutorial The Front-end

Syntax-Directed Translation Part II

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

Summary: Semantic Analysis

CS 314 Principles of Programming Languages

Syntax-Directed Translation

LECTURE 3. Compiler Phases

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

Syntax-Directed Translation. Lecture 14

5. Syntax-Directed Definitions & Type Analysis

Principles of Programming Languages

Syntax-Directed Translation Part I

CSE302: Compiler Design

LR Parsing - The Items

Lecture 20 Sections Wed, Apr 1, 2009

Error Handling Syntax-Directed Translation Recursive Descent Parsing

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Compiler Lab. Introduction to tools Lex and Yacc

Ambiguity and Errors Syntax-Directed Translation

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Syntax Analysis Check syntax and construct abstract syntax tree

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

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Programming Languages

Error Handling Syntax-Directed Translation Recursive Descent Parsing. Lecture 6

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

CSE302: Compiler Design

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

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

CPS 506 Comparative Programming Languages. Syntax Specification

[Syntax Directed Translation] Bikash Balami

More On Syntax Directed Translation

Static and Dynamic Semantics

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

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

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Function Definition Syntax Tree

Syntax-Directed Translation

Principles of Programming Languages

Syntax-Directed Translation

COP4020 Spring 2011 Midterm Exam

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

Semantic Analysis with Attribute Grammars Part 3

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

A Simple Syntax-Directed Translator

CS 314 Principles of Programming Languages. Lecture 9

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Semantic Analysis. Lecture 9. February 7, 2018

Principles of Programming Languages COMP251: Syntax and Grammars

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Building Compilers with Phoenix

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

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

Compilers. Lecture 2 Overview. (original slides by Sam

Syntax and Grammars 1 / 21

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

CSE302: Compiler Design

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

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

COSE312: Compilers. Lecture 1 Overview of Compilers

Table-Driven Top-Down Parsers

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

The x86 Instruction Set

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

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

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Chapter 4 :: Semantic Analysis

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntax. In Text: Chapter 3

Lexical and Syntax Analysis

EECS 6083 Intro to Parsing Context Free Grammars

CS 406: Syntax Directed Translation

Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

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

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

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Chapter 6 Intermediate Code Generation

Some Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Program Representations

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Transcription:

Lecture 14 Sections 5.1-5.4 Hampden-Sydney College Mon, Mar 2, 2009

Outline 1 2 3 4 5

Parse A parse tree shows the grammatical structure of a statement. It includes all of the grammar symbols (terminals and nonterminals) that were encountered during parsing.

An abstract syntax tree (AST) shows the logical structure of the statement. Each node represents an action to be taken by the program or an object to be acted upon. The syntax tree may introduce operations that were not in the source code or the grammar. Dereferencing operations. Type-casting operations. Jump statements.

vs. Parse Example ( and Parse ) Consider the statement a = 2*b + c; stmt = lval = expr ; a + id expr + expr * deref expr * expr id 2 deref c num id b Parse Tree Syntax Tree

vs. Parse Our TreeBuilder program will convert the parse tree into the syntax tree. The parse tree never really exists, except insofar as the parser follows its logical order. The TreeBuilder will simply build the syntax tree from the information obtained by the parser. Then the code generator will write the assembly code from the syntax tree.

Recursive descent parsers generally create a single AST for the entire program. Our parser will generate a separate AST for each statement. It will create a list of ASTs. This will allow us to generate assembly code as the ASTs are created. The trees will be connected both sequentially and through jump statements.

Syntax-Directed Definitions Definition A syntax-directed definition is a context-free grammar with attributes added to the grammar symbols. These attributes are stored in the nodes of the syntax tree. Each node has A set of synthesized attributes, and A set of inherited attributes.

Definition A synthesized attribute of a grammar symbol is a property that is determined by the attributes of the symbols below it in the parse tree. In other words, if A α is a production, then A s synthesized attributes are determined by the attributes of the symbols in α.

Definition An inherited attribute is a property of a symbol (node) that is determined by its parent node and its siblings in the parse tree. In other words, if β is symbol on the right side of the production A αβγ, then β s inherited attributes are determined by the attributes of A and the other symbols in α and γ.

If the AST represents a numerical expression, then the value of the root node is determined by the values of the nodes below it in the tree. Thus, the value of the root node is a synthesized attribute.

Example ( ) Let the grammar be E E + E num Then E derives its value from the num tokens in the expression. This is expressed formally by the rules E.val = E 1.val + E 2.val E.val = num.lexval

Example ( ) The terminals get their values directly from the lexical analyzer. For example, a num token s value attribute would be the numerical value of the string of digits in the token.

Example ( ) E.val E 1.val + E 2.val num.lexval num.lexval 100 250

Example ( ) E.val E 1.val + E 2.val num.lexval num.lexval from lexer from lexer 100 250

Example ( ) E.val E 1.val + E 2.val synthesized synthesized num.lexval num.lexval from lexer from lexer 100 250

Example ( ) synthesized E.val synthesized E 1.val + E 2.val synthesized synthesized num.lexval num.lexval from lexer from lexer 100 250

Example ( ) Consider the grammar for a declaration containing one or more identifiers. D T L L L, id id T int float For example, the declaration might be float a, b;

Example ( ) The attribute float first appears as the type of the float token. From there it is passed to the identifiers a and b.

Example ( ) D T.type L.type float L.type, id 2.type float id 1.type

Example ( ) D T.type L.type float L.type, id 2.type from lexer float id 1.type

Example ( ) D T.type L.type synthesized float L.type, id 2.type from lexer float id 1.type

Example ( ) D T.type inherited L.type synthesized float L.type, id 2.type from lexer float id 1.type

Example ( ) D T.type inherited L.type synthesized inherited inherited float L.type, id 2.type from lexer float id 1.type

Example ( ) D T.type inherited L.type synthesized inherited inherited float L.type, id 2.type from lexer inherited float id 1.type

Some Questions Questions In an expression tree, is the type of the expression at the root inherited or is it synthesized? Is the type used in an arithmetic operation an inherited attribute or an synthesized attribute of the operator? In an assignment statement, is the type assigned by the operator an inherited attribute or a synthesized attribute of the operator?

We will describe how to build an AST for an expression. We will use TreeNode constructors similar to the following. TreeNode(op, left, right) Join two existing trees, placing op at the root node. TreeNode(id, entry) Create a single-node tree with id at the root node. TreeNode(num, value) Create a single-node tree with num at the root node.

Example ( Tree) To construct a tree for the expression a - 4 + c we do the following: tree 1 = new TreeNode(id, identry a ) tree 2 = new TreeNode(num, 4) tree 3 = new TreeNode(minus, tree 1, tree 2 ) tree 4 = new TreeNode(id, identry c ) tree 5 = new TreeNode(plus, tree 3, tree 4 )

Example ( Tree) Production Semantic Rule E E 1 + E 2 E.tree = new TreeNode(plus, E 1.tree, E 2.tree); E E 1 E 2 E.tree = new TreeNode(minus, E 1.tree, E 2.tree); E (E 1 ) E.tree = new TreeNode(E 1.tree); E id E.tree = new TreeNode(id, id.entry); E num E.tree = new TreeNode(num, num.val);

Homework Read Sections 5.1-5.4, pages 279-301.