Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

CPSC 388 – Compiler Design and Construction
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Abstract Syntax Trees Compiler Baojian Hua
Context-Free Grammars Lecture 7
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Syntax-Directed Translation
Chapter 2 A Simple Compiler
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Definitions Synthesized Attributes
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Syntax-Directed Translation
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
LESSON 04.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax Directed Definition and Syntax directed Translation
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Chapter 2: A Simple One Pass Compiler
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Presentation transcript:

Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004

Parse Trees 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.

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

Syntax Trees vs. Parse Trees Consider the statement a = 2*b + c; stmt l-val=E ; +E E*E numid E Parse TreeSyntax Tree = a+ * 2deref b c

Syntax Trees vs. Parse Trees Our TreeBuilder program will “convert” the syntax tree into the parse 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 compiler will write the assembly code from the syntax tree.

Abstract Syntax Trees 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 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.

Synthesized Attributes 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 .

Example: Synthesized Attributes 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.

Example: Synthesized Attributes 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.

Synthesized Attributes 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: Synthesized Attributes E.val E 1.val num.lexval E 2.val

Example: Synthesized Attributes E.val E 1.val num.lexval E 2.val from lexer

Example: Synthesized Attributes E.val E 1.val num.lexval E 2.val+ synthesized

Example: Synthesized Attributes E.val E 1.val num.lexval E 2.val synthesized

Inherited Attributes 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  , (  is part of  ) then  ’s inherited attributes are determined by the attributes of A and the other symbols in .

Example: Inherited Attributes 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: Inherited Attributes The attribute ( float ) first appears as the value of the float token. From there it is passed to the identifiers a and b.

Example: Inherited Attributes D T.type id 1.type float L.type id 2.typeL.type,

Example: Inherited Attributes D T.type id 1.type float L.type id 2.typeL.type, from lexer

Example: Inherited Attributes D T.type id 1.type float L.type id 2.typeL.type, inherited

Example: Inherited Attributes D T.type id 1.type float L.type id 2.typeL.type, inherited

Example: Inherited Attributes D T.type id 1.type float L.type id 2.typeL.type, inherited

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?

Example: Expression Tree 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: Expression Tree To construct a tree for the expression a 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: Expression Tree The semantic rules would be ProductionSemantic 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 = E 1.tree; E  id E.tree = new TreeNode(id, id.entry); E  num E.tree = new TreeNode(num, num.val);