Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Compilation (Semester A, 2013/14) Lecture 6b: Context Analysis (aka Semantic Analysis) Noam Rinetzky 1 Slides credit: Mooly Sagiv and Eran Yahav.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
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.
Context-Free Grammars Lecture 7
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.
Programming Languages An Introduction to Grammars Oct 18th 2002.
CSE 6341 (755) Programming Languages
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Describing Syntax and Semantics
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
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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)
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 363 Comparative Programming Languages Semantics.
March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Chapter 3 Part II Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
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.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Definite Clause Grammars
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
CS416 Compiler Design lec00-outline September 19, 2018
CS 3304 Comparative Languages
Compiler Design 4. Language Grammars
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Syntax Questions 6. Define a left recursive grammar rule.
Lecture 11: Context Sensitive Analysis
Introduction CI612 Compiler Design CI612 Compiler Design.
Prabhaker Mateti ACK: Assembled from many sources
CSE 6341 Programming Languages
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
CS416 Compiler Design lec00-outline February 23, 2019
SYNTAX DIRECTED DEFINITION
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources

About Attribute Grammars Attribute grammars (AGs) add semantic info on parse tree nodes Used for semantic checking and other compile-time analyses, e.g., type checking in a compiler Used for translation, e.g., parse tree to assembly code A traversal of the parse tree and the computation of information. CS7842

Attribute Grammars: Definition An attribute grammar is a context-free grammar G = (S, N, T, P) with the following additions. For each terminal and non-terminal X, disjoint sets S(X) synthesized attributes and I(X) inherited attributes: – A(X) = S(X) ∪ I(X) X0 ::= X1... Xn S(X0) = f(I(X0), A(X1),..., A(Xn)) I(Xj) = g(A(X0),..., A(Xn)) – depends on the attribute value at parent and those of siblings. Each rule has a set of predicates/ conditions to check for attribute consistency: P( A(X0), A(X1), A(X2), …, A(Xn) ) CS7843

Example-1: Binary Numbers 1.N ::= 0 2.N ::= 1 3.N ::= N 0 4.N ::= N 1 CFGs do not provide semantics. CGGs provide only syntax, that too without context-sensitive details 1.N.val:= 0 2.N.val:= 1 3.N.val:= 2*N.rhs.val 4.N.val:= 2*N.rhs.val+1 N.val is an attribute associated with node N of the parse tree N.rhs Node corresponding to the rhs Synthesized Attributes CS7844

Example-2: Type Checking 1.E ::= n 2.E ::= x 3.E ::= E1 + E2 4.E ::= E1 * E2 Semantics we wish to add – n is an int, x is a real. – op + returns an int if both the operands are int, otherwise a real. 1.E.type := int 2.E.type := real 3.if E1.type = E2.type then E.type := E1.type else E.type := real fi Item 3 derived version of the semantics Static semantics CS7845

Example-3: Assignment Arithmetic stm ::= var := exp | stm; stm exp ::= var + var | var var ::= A | B | C synthesized actual-type for var and exp inherited expected-type for exp lookup (var.string) a helper function; gives the actual type of A, B, C exp ::= var1 + var2 – subscripts added exp.actual-type := var1.actual-type exp.expected-type – from parent in the parse tree Predicates: – var1.actual-type == var2.actual-type – exp.expected-type == exp.actual-type – var ::= A | B | C var.actual-type := lookup (var.string) CS7846

Inherited Attributes Example Declaration and Use { int i, j, k; i := i + j + j; } assign ::= var := exp – env: environment – var.env := assign.env – exp.env := assign.env CS7847

8 Information Flow inherited synthesized... computed available available

Attribute Value Computation If all attributes were inherited, the tree could be decorated in top-down order. Inherited Attributes pass information – down the parse tree, or – from left siblings to the right siblings If all attributes were synthesized, the tree could be decorated in bottom-up order. Synthesized Attributes pass information up the parse tree In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used. Initially, there are intrinsic attributes on the leaves If a condition in a tree evaluates to false, an error occurs. CS7849

Prolog for Example-2 1.type(n, int). 2.type(x, real). 3.type(+(E, F), T) :- type(E, T), type(F, T). 4.type(+(E, F), real) :- type(E, T1), type(F, T2), T1 \= T2. 5. Type Checking ?- type(+(n, x), real). 6. Type Inference ?- type(+(n, x), T). (Definite Clause Grammars) CS78410

Example-4: Fractions in Binary 1.F ::=. N 2.N ::= 0 3.N ::= 1 4.N ::= 0 N 5.N ::= 1 N Synthesized: val, value Inherited: pow, the number of bits between left of a non-terminal and the binary point Nr: N-right, Nl: N-left 1: F.val:= N.val; N.pow:= 1 2: N.val := 0 3: N.val := (1/2^N.pow) 4: Nl.val := Nr.val 4: Nr.pow := 1 + Nl.pow 5: Nl.Val := Nr.val+(1/2^N.pow) 5: Nr.pow := 1 + Nl.pow CS78411

Ex4: Synthesized Attributes Only Binary Fractions, same grammar as before: 1.F ::=. N 2.N ::= 0 3.N ::= 1 4.N ::= 0 N 5.N ::= 1 N Alternate computation based on synthesized attribute val only 1: F.val := N.val / 2 2: N.val := 0 3: N.val := 1 4: N.val := N.val / 2 5: N.val := N.val / CS78412

Example-5: Distinct Identifiers Compute the number of distinct identifiers in a straight-line program. Semantics specified in terms of sets of identifiers. Attributes – var  id – exp  ids – stm  ids  num CS78413

Example-5: Distinct Identifiers exp::= var – exp.ids = { var.id } exp::= exp1 + exp2 – exp.ids = exp1.ids U exp2.ids stm::= var:= exp – stm.ids = { var.id } U exp.ids – stm.num = | stm.ids | stm::= stm1;stm2 – stm.ids = stm1.ids U stm2.ids – stm.num = | stm.ids | CS78414

Example-5: Using Lists Attributes –  envi: list of vars in preceding context –  envo: list of vars for following context –  dnum: number of new variables exp ::= var exp.envo = if member(var.id, exp.envi) then exp.envi else cons(var.id, exp.envi) fi CS78415

Example-5: Using Lists exp ::= exp1 + exp2  envi  envi  envi  envo  envo  envo  dnum  dnum  dnum exp1.envi := exp.envi exp2.envi := exp1.envo exp.envo := exp2.envo exp.dnum := length(exp.envo) exp.envo = append-sans-duplicates(exp1.envo, exp2.envo ) CS78416

Complete Evaluation Rules Synthesized attribute associated with N: – Each alternative in “N ::= …” should contain a rule for evaluating the Synthesized attribute. Inherited attribute associated with N: – For every occurrence of N in “… ::= … N …” there must be a rule for evaluating the Inherited attribute. Whenever you create an attribute grammar (in home work/ exams), make sure it satisfies these requirements. CS78417

One Pass Attribute Computation To enable one-pass top-down left-to-right computation of the attributes: – each inherited attribute of the right-hand side symbol can depend on all the attributes associated with preceding right-hand side symbols and the inherited attribute of the left-hand side non-terminal. – Similarly, the synthesized attribute of the left-hand side non-terminal can depend on all the attributes associated with all the right-hand side symbols and the inherited attribute of the left-hand side non- terminal. CS78418

More than Context-Free Power LABC = { a^nb^nc^n | n > 0 } – Unlike LAB = { a^nb^n | n > 0 }, here we need explicit counting of a’s, b’s and c’s LWCW = { wcw | w ∈ {a, b}* } – The “flavor” of checking whether identifiers are declared before their uses LABC, LWCW cannot be defined with a context- free grammar Syntax analysis (i.e., parser based on CFGs) cannot handle semantic properties CS78419

LABC = { a^n b^n c^n | n > 0 } ls ::= as bs cs – ExpNb(bs) := Na(as); ExpNc(cs) := Na(as) as ::= a | a as1 – Na(as) := 1; Na(as) := Na(as1) + 1 bs ::= b | b bs1 – cond(ExpNb(bs) = 1); ExpNb(bs1) := ExpNb(bs) - 1 cs ::= c | c cs1 – Cond(ExpNc(cs) = 1); ExpNc(cs1) := ExpNc(cs) – 1 Na:synthesized by as ExpNb:inherited from bs ExpNc: inherited from cs CS78420

Uses of Attribute Grammars Compiler Generation – Top-down Parsers (LL(1)) FIRST sets, FOLLOW sets, etc – Code Generation Computations Type, Storage determination, etc. Databases – Optimizing Bottom-up Query Evaluation (Magic Sets) Programming and Definitions CS78421

Uses of Inherited Attributes ex: – need to generate code to coerce int 2 to real 2.0 Determination of un-initialized variables Determination of reachable non-terminals Evaluation of an expression containing variables CS78422

Use of Attribute Grammars Useful for expressing arbitrary cycle-free computational walks over CFG derivation trees – Synthesized and inherited attributes – Conditions to reject invalid parse trees – Evaluation order depends on attribute dependencies Realistic applications: – type checking – code generation “Global” data structures must be passed around as attributes Any container data structure (sets, etc.) can be used The evaluation rules can call auxiliary/helper functions but the functions cannot have side effects CS78423

References T. K. Prasad, Attribute Grammars and their Applications, In: Encyclopedia of Information Science and Technology, pp , Attribute-Grammars.pdf Attribute-Grammars.pdf PL Text Book Sections – Pagan: 2.1, 2.2, 2.3, 3.2 – Stansifer: 2.2, 2.3 – Slonneger and Kurtz: 3.1, 3.2 CS78424