CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Department.
PZ02A - Language translation
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
FE.1 CSE4100 Final Exam Advice and Hints Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
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],
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
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.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
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)
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.
CPS 506 Comparative Programming Languages Syntax Specification.
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.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Introduction to Compiling
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
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.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Semantic analysis Jakub Yaghob
Context-Sensitive Analysis
Constructing Precedence Table
Definite Clause Grammars
Syntax-Directed Translation Part I
Lecture 11: Context Sensitive Analysis
Prabhaker Mateti ACK: Assembled from many sources
Syntax-Directed Translation Part I
Presentation transcript:

CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.

CS784 (Prasad)L167AG2 “Correct” Programs (no run-time errors)  Regular (lexer) Context-free (parser) Context-sensitive (type-checker)

CS784 (Prasad)L167AG3 Semantics of Bit Pattern : Number –46 (if base 2) – (if base 10) Fraction –23/64 Unary encoding “13” ASCII Character “. ” Switch positions –ON-OFF-ON-… Binary string “ ”

CS784 (Prasad)L167AG4 Motivation for Precise Specification Capture subtly different semantics for the same syntax in various languages.  Arrays and strings.  Parameter passing mechanisms.  Scoping rules.  Primitive types vs Composite types.  Type equivalence.

CS784 (Prasad)L167AG5 Attribute Grammars Formalism for specifying semantics based on context-free grammars (BNF) Static semantics (context-sensitive aspects) Type checking and type inference Compatibility between procedure definition and call Scope rules Dynamic semantics Associate attributes with terminals and non-terminals Associate attribute computation rules with productions

CS784 (Prasad)L167AG6 Synthesized Attributes    N   N   val   val   val  N  val  val  N  val 

CS784 (Prasad)L167AG7 Derivation Tree N N N ~> 6

CS784 (Prasad)L167AG8 Synthesized Attributes    N  N  val   len   val   len   val  N  val  len  N  len   val  N  val  ^ N  len  len  N  len + 1

CS784 (Prasad)L167AG9 Inherited Attributes Declaration and Use { int i, j, k; i := i + j + j; }    :=. env :=. env

CS784 (Prasad)L167AG10 Inherited Attributes Coercion Code Generation  coerce_int_to_real Determination of un-initialized variables Determination of reachable non-terminals Evaluation of an expression containing variables

CS784 (Prasad)L167AG11 AttributesAttributes A( X ) –Synthesized S( X ) –Inherited I( X ) Attribute computation rulesAttribute computation rules (Semantic functions) X0 -> X1 X2 … Xn S( X0 ) = f ( I( X0 ), A( X1 ), A( X2 ), …, A( Xn ) ) I( Xj ) = G j ( I( X0 ), A( X1 ), A( X2 ), …, A( Xj-1 )) for all j in 1..n P ( A( X0 ), A( X1 ), A( X2 ), …, A( Xn ) )

CS784 (Prasad)L167AG12 Information Flow inherited synthesized... computed available available

CS784 (Prasad)L167AG13 Synthesized AttributesSynthesized Attributes Pass information up the parse tree Inherited AttributesInherited Attributes Pass information down the parse tree or from left siblings to the right siblings Attribute values assumed to be available from the context. Attribute values computed using the semantic rules provided. The constraints on the attribute evaluation rules permit top-down left-to-right (one-pass) traversal of the parse tree to compute the meaning.

CS784 (Prasad)L167AG14 Static Semantics E -> n | m E -> x | y E -> E1 + E2 E -> E1 * E2  E.type  int E.type  real if E1.type  E2.type then E.type  E1.type else E.type  real

CS784 (Prasad)L167AG15 Executable Specification in Prolog type(i, int ). type(x, real ). type(+(E,F),T) :- type(E,T), type(F,T). type(+(E,F), real ) :- type(E,T1), type(F,T2), T1 \= T2. Type Checking ?- type(+(i,x),real). Type Inference ?- type(+(x,x),T).

CS784 (Prasad)L167AG16 Static Semantics E -> n | m E -> p | q E -> if E0 then E1 else E2 E.type  int E.type  bool if ( E0.type  bool )  ( E1.type = E2.type ) then E.type  E1.type else type error

CS784 (Prasad)L167AG17 Fractions F       N  N F.val  N.val N.pow  1 N.val  N.val  1/2^N.pow)  N.pow  1 + N.pow N.val  N  val N.pow  1 + N.pow N.val  N  val + (1/2^N.pow)

CS784 (Prasad)L167AG18 Fractions (Alternate solution) F       N  N F.val  N.val  2 N.val  N.val  N.val  N  val  2 + 1

CS784 (Prasad)L167AG19 Applications 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

CS784 (Prasad)L167AG20 An Extended Example Distinct identifiers in a straight-line program. BNF ::= | + ::= := | ; Attributes  id  ids  ids  num Semantics specified in terms of sets (of identifiers).

CS784 (Prasad)L167AG21 ::=.ids = {.id } ::= +.ids =.ids U.ids ::= :=.ids ={.id } U.ids.num = |.ids | ::= ;.ids =.ids U.ids.num = |.ids |

CS784 (Prasad)L167AG22 Alternate approach : Using lists Attributes  envi : list of vars in preceding context  envo : list of vars for following context  dnum : number of new variables ::=.envo = if member(.id,.envi) then.envi else cons(.id,.envi)

CS784 (Prasad)L167AG23 ::= +  envi  envi  envi  envo  envo  envo  dnum  dnum  dnum.envi =.envi.envi =.envo.envo =.envo.dnum = length(.envo) Attribute Computation Rules