Compiler Construction Sohail Aslam Lecture 31. 2 Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.

Slides:



Advertisements
Similar presentations
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
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.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
Compilation (Semester A, 2013/14) Lecture 6b: Context Analysis (aka Semantic Analysis) Noam Rinetzky 1 Slides credit: Mooly Sagiv and Eran Yahav.
Compiler Construction Sohail Aslam Lecture Number Sign List Bit – 1 List Bit 1 0 Parse tree for – 101.
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 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.
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.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
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.
Abstract Syntax Tree (AST)
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Attribute Grammars Recall the yacc program Parse a given expression
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],
Compiler Construction CS 606 Sohail Aslam Lecture 2.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 Compiler Construction 강의 8. 2 Context-Free Languages The inclusion hierarchy for context-free languages Context-free languages Deterministic languages.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Context-sensitive Analysis or Semantic Elaboration Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17.
Parsing VII The Last Parsing Lecture. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { … } fee()
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
Syntax Specification and BNF © Allan C. Milne Abertay University v
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)
Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
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.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
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.
Introduction to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Syntax Directed Definition and Syntax directed Translation
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Lecture 8 Semantic Analysis.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
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.
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.
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.
Programming Language Concepts
Context-Sensitive Analysis
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax Directed Translation
Lecture 11: Context Sensitive Analysis
Context-sensitive Analysis
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
SYNTAX DIRECTED DEFINITION
Deletion in AVL Tree There are 5 cases to consider.
Compiler Construction
Lecture 16 Boolean Expressions and Control Flow
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 31

2 Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not parts of speech  Answers may involve computation

3 Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not parts of speech  Answers may involve computation

4 Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not parts of speech  Answers may involve computation

5 Beyond Syntax How can we answer these questions?  Use formal methods Context-sensitive grammars Attribute grammars

6 Beyond Syntax How can we answer these questions?  Use formal methods Context-sensitive grammars Attribute grammars

7 Beyond Syntax How can we answer these questions?  Use formal methods Context-sensitive grammars Attribute grammars

8 Beyond Syntax  Use ad-hoc techniques Symbol tables ad-hoc code

9 Attribute Grammars  A CFG is augmented with a set of rules  Each symbol in the derivation has a set of values or attributes  Rules specify how to compute a value for each attribute

10 Attribute Grammars  A CFG is augmented with a set of rules  Each symbol in the derivation has a set of values or attributes  Rules specify how to compute a value for each attribute

11 Attribute Grammars  A CFG is augmented with a set of rules  Each symbol in the derivation has a set of values or attributes  Rules specify how to compute a value for each attribute

12 Attribute Grammars  grammar for signed binary numbers (SBN) Number → Sign List Sign → +  – List → List Bit | Bit Bit → 0  1

13 Attribute Grammars derivation for “ –1 ” Number → Sign List → – List → – Bit → – 1

14 Attribute Grammars For “-101” Number → Sign List → Sign List Bit → Sign List 1 → Sign List Bit 1 → Sign List 0 1 → Sign Bit 0 1 → Sign → – 1 0 1

15 Attribute Grammars For an attributed version of SBN, the following attributes are needed SymbolAttributes Number val Sign neg List pos, val Bit pos, val

16 Attribute Grammars  We will add rules to compute decimal value of a signed binary number

17 Attribute Grammars ProductionsAttribution Rules Number → Sign List List.pos  0 if Sign.neg then Number.val  – List.val else Number.val  List.val Sign → + Sign.neg  false Sign → – Sign.neg  true

18 Attribute Grammars ProductionsAttribution Rules Number → Sign List List.pos  0 if Sign.neg then Number.val  – List.val else Number.val  List.val Sign → + Sign.neg  false Sign → – Sign.neg  true

19 Attribute Grammars ProductionsAttribution Rules List 0 → List 1 Bit List 1.pos  List 0.pos + 1 Bit.pos  List 0.pos List 0.val  List 1.val + Bit.val List → Bit Bit.pos  List.pos List.val  Bit.val Bit → 0 Bit.val  0 Bit → 1 Bit.val  2 Bit.pos

20 Attribute Grammars  Attributes are associated with nodes in parse tree  Rules are value assignments associated with productions

21 Attribute Grammars  Attributes are associated with nodes in parse tree  Rules are value assignments associated with productions

22 Attribute Grammars  Rules and parse tree define an attribute dependence graph Graph must be acyclic

23 Attribute Grammars Number Sign List Bit – 1 List.pos  0 List.val  Bit.val = 1 Number.val  – List.val = –1 Bit.pos  0 Bit.val  2 Bit.pos = 1 Sign.neg  true

24 AttributesAttributes Attributes are distinguished based on the direction of value flow 1.Synthesized attributes 2.Inherited attributes

25 Synthesized Attributes  Attributes of a node whose values are defined wholly in terms of attributes of node’s children and from constants are called synthesized attributes

26 Synthesized Attributes  Values used to compute synthesized attributes flow bottom-up in the parse tree  Good match to LR parsing

27 Inherited Attributes  Attributes whose values are defined in terms of node’s own attributes, node’s siblings node’s parent  Values flow top-down and laterally in the parse tree

28 Inherited Attributes  Attributes whose values are defined in terms of node’s own attributes, node’s siblings node’s parent  Values flow top-down and laterally in the parse tree