Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Semantics Static semantics Dynamic semantics attribute grammars
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Jan. 2013Dr. Yangjun Chen ACS Outline Signature Files - Signature for attribute values - Signature for records - Searching a signature file Signature.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Parsing III (Eliminating left recursion, recursive descent parsing)
Context-sensitive Analysis. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { … } fee() { int f[3],g[0],
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
CS 363 Comparative Programming Languages Semantics.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
Standardizing RPAL AST’s Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 10.
Writing RPAL Programs Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 13.
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.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Syntax Directed Definition and Syntax directed Translation
An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
SDTs used to implement SDDs A non-cyclic SDD (having definitions of attributes) can always be implemented by a SDT (having actions that assign values to.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Programming Language Concepts
Context-free grammars
COMP 53 – Week Fourteen Trees.
Building AST's for RPAL Programs
A Simple Syntax-Directed Translator
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 5 Syntax Directed Translation
An Attribute Grammar for Tiny
Programming Language Principles
Graph Algorithms Using Depth First Search
CS 3304 Comparative Languages
Syntax-Directed Translation Part I
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Language Principles
Building AST's for RPAL Programs
SYNTAX DIRECTED DEFINITION
Building Java Programs
Operator precedence and AST’s
Operator Precedence and Associativity
Programming Language Concepts
Presentation transcript:

Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17

Functional Graphs Definition: A functional graph is a directed graph in which: Nodes represent functions. Incoming edges are parameters. Outgoing edges represent functional values. Edges represent data transmission among functions.

Example Here we have three functions: Constant function 2. Constant function 3. Binary function + (addition). After some delay, the output value is

Cycles in Functional Graphs Can make sense only if the graph can achieve a steady state. Example: No steady state is achieved, because the output value keeps incrementing. 1+ ?

Example A steady state is achieved. If the "AND" were changed to a "NAND," no steady state. Undecidable (halting problem) whether a steady state will ever occur. We will assume that all functional graphs are acyclic. trueand ?

Evaluation of Functional Graphs First insert registers. Then propagate values along the edges. Register insertion: Given edges E 1... E n from node A to nodes B 1... B n, insert a register R : one edge from A to R, and n edges from R to B 1... B n.

Example All registers initialized to some "undefined" value. Top-most registers have no outgoing edges. B1B1 A B2B2 BnBn … gets converted to B1B1 AB2B2 BnBn …

Functional Value Propagation Two algorithms: Data Flow Analysis: repeated passes on the graph, evaluating functions whose inputs (registers) are defined. Lazy Evaluation", performs a depth-first search, backwards on the edges.

Data Flow Algorithm While any top-most register is undefined { for each node N in the graph { if all inputs of N come from defined register then evaluate N ; update N's output register }

Lazy Evaluation for each top-most register R, { push (stack, R) } while stack not empty { current := top (stack); if current = undefined { computable := true; for each R in dependency (current), while computable { if R = undefined { computable := false; push (stack,R); } } if computable { compute_value(current); pop(stack); } } else pop (stack) }

Data Flow and Lazy Evaluation Data Flow Analysis: Starts at constants and propagates values forward. No stack. Algorithm computes ALL values, needed or not.

Data Flow and Lazy Evaluation (cont’d) Lazy evaluation: Starts at the target nodes. Chases dependencies backwards. Evaluates functions ONLY if they are needed. More storage expensive (stack), but faster.

Attribute Grammars Associate constructs in an AST with segments of a functional graph. It's a context-free grammar: Each rule augmented with a set of axioms. Axioms specify graph segments. As AST is built (bottom-up); segments of the functional graph are "pasted" together.

Attribute Grammars (cont’d) After completing AST (and graph), evaluate graph (data flow or lazy evaluation). After graph evaluation, top-most graph register(s) (presumably) contain the output of the translation.

Definition An attribute grammar consists of: 1.A context-free grammar (structure of the parse tree) 2.A set of attributes ATT. Each attribute "decorates" some node in the AST, later becomes a register in the functional graph. 3.A set of axioms defining relationships among attributes (nodes in the graph).

Example: Binary Numbers String-to-tree transduction grammar: S → N =>. → N. N =>. N → N D => cat → D D → 0 => 0 D →1 => 1 This grammar specifies "concrete" syntax. Notice left recursion.

Abstract Syntax Tree Grammar Use tree notation. S → → N → → D D → 0 → 1 This grammar (our choice for AG's) specifies "abstract" syntax.

Attributes Two types: Synthesized: pass information UP the tree. Inherited: pass information DOWN the tree. If tree is traversed recursively with (s 1, … s n ) ProcessNode(tree T, (i 1,…,i m )), m inherited attributes are parameters to ProcessNode. n synthesized attributes are return values of ProcessNode.

Attributes (cont’d) For binary numbers, ATT = {value, length, exp}, where value: decimal value of the binary number. length: number of binary digits to the left of the right-most digit in the sub- tree. Used to generate negative exponents (fractional part). exp: exponent (of 2), to be multiplied by the right-most binary digit in the subtree.

Attributes (cont’d) Synthesized and inherited attributes specified by two subsets of ATT : SATT = {value, length} IATT = {exp} Note: SATT and IATT are disjoint in this case. Not always.

Attributes Associated with Tree Nodes S:  → PowerSet (SATT) I:  → PowerSet (IATT)  = { 0, 1, cat,. } (tree grammar vocabulary). S(0) = { value, length } S(1) = { value, length } S(cat) = { value, length } S(.) = { value } I(0) = { exp } I(1) = { exp } I(cat) = { exp } I(.) = { }

Example Input: Convention: Inherited attributes depicted on the LEFT. Synthesized attributes depicted on the RIGHT. Flow of information: top-down on left, bottom-up on right.

Tree Addressing Scheme Given a tree node T, with kids T 1,... T n, a(  ) denotes attribute " a " at node T, and a(i) denotes attribute " a " at the i’th child of node T (node T i ). So, v(  ) is the "value" attribute at T v(1) is the "value" attribute at T 1. v(2) is the "value" attribute at T 2.

Rules for axiom specification. Consider a production rule A →. Let r 1,..., r n be the roots of subtrees K 1,..., K n. 1.Need one axiom per synthesized attribute at r (specify what goes up at root). 2.Need one axiom per inherited attribute at each kid r i (specify what goes to the kids). 3.Axioms are of the form a=f(w 1,..., w m ), where each w i is either 3.1.inherited at r, 3.2.synthesized from r i, for some i. Note: this doesn’t prevent cycles.

Attribute Grammar, Binary Numbers Production rule S →. Need three axioms: one for v at ". ", one for e at T 1, one for e at T 2. Axioms: value(  ) = value(1) + value(2) exp(1) = 0 exp(2) = - length(2)

Notes length attribute from kid 1 ignored. length attribute from kid 2 is negated, sent down to e(2). Only use length to calculate negative exponents (fractional part, second kid). length will start at 1, at the bottom of the tree. Will be incremented on the way up.

The Complete Attribute Grammar for Binary Numbers S -> value(  ) = value(1) + value(2) exp(1) = 0 exp(2) = - length(2) S -> value(  ) = value(1) # no fraction; # copy value up. exp(1) = 0

The Complete Attribute Grammar for Binary Numbers (cont’d) N → value(  ) = value(1) + value (2) # add two # values. length(  ) = length(1) + 1 # increment # length up left. exp(1) = exp (  ) + 1 # increment # exp down left. exp(2) = exp (  ) # copy exp # down right. N → D # No axioms !

The Complete Attribute Grammar for Binary Numbers (cont’d) D → 0 value(  ) = 0 # zero * 2 exp. length(  ) = 1 # initial length. D → 1 value(  ) = 2 ** exp(  ) # compute value. length(  ) = 1 # initial length.

Example Example, for input 10.1

Generation of Functional Graphs Optional, see notes

Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17