Compiler Construction Sohail Aslam Lecture 32. 2 Number Sign List Bit – 1 List Bit 1 0 Parse tree for – 101.

Slides:



Advertisements
Similar presentations
Compiler Construction Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
Advertisements

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)
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.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix.
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.
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)
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],
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Syntax-Directed Translation
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
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.
1 Lecture 11: Semantic Analysis (Section ) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos.
Context-sensitive Analysis or Semantic Elaboration Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
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.
Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17.
Context-sensitive Analysis, III Ad-hoc syntax-directed translation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Context-sensitive Analysis, Part II From AGs to ad-hoc methods Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
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.
COMPILER CONSTRUCTION
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
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.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
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 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
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.
Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
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.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
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.
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Chapter 5 Syntax Directed Translation
Syntax Directed Translation
Syntax-Directed Translation Part I
Chapter 5. Syntax-Directed Translation
Lecture 11: Context Sensitive Analysis
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Context-sensitive Analysis
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Context-sensitive Analysis, III Ad-hoc syntax-directed translation
Compiler Construction
Compiler Construction
Syntax-Directed Translation Part I
Compiler Construction
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 32

2 Number Sign List Bit – 1 List Bit 1 0 Parse tree for – 101

3 Number Sign List Bit – 1 val: -5 List Bit 1 0 attributed tree pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true

4 Number Sign List Bit – 1 val: -5 List Bit 1 0 Inherited Attributes pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true

5 Number Sign List Bit – 1 val: -5 List Bit 1 0 Synthesized Attributes pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true

6 Number Sign List Bit – 1 val: -5 List Bit 1 0 Together pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true

7 Number Sign List Bit – 1 val: -5 List Bit 1 0 peel away parse tree pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true

8 – 1 val: dependence graph pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true

9 – 1 val: dependence graph pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true must be acyclic!

10 Evaluation Methods Dynamic methods  Build the parse tree  Build the dependence graph  Topological sort the graph  Define attributes in topological order

11 Evaluation Methods Dynamic methods  Build the parse tree  Build the dependence graph  Topological sort the graph  Define attributes in topological order

12 Evaluation Methods Dynamic methods  Build the parse tree  Build the dependence graph  Topological sort the graph  Define attributes in topological order

13 Evaluation Methods Dynamic methods  Build the parse tree  Build the dependence graph  Topological sort the graph  Define attributes in topological order

14 Evaluation Methods Dynamic methods  Build the parse tree  Build the dependence graph  Topological sort the graph  Define attributes in topological order

15 Evaluation Methods Rule-based (treewalk)  Analyze attribute rules at compiler-generation time  Determine a fixed (static) ordering  Evaluate nodes in that order

16 Evaluation Methods Oblivious (passes, dataflow)  Ignore rules and parse tree  Pick a convenient order (at design time) and use it

17 ProblemsProblems Attribute grammars have not achieved widespread use due to a myraid of problems

18 ProblemsProblems  non-local computation  traversing parse tree  storage management for short-lived attributes  lack of high-quality inexpensive tools

19 ProblemsProblems  non-local computation  traversing parse tree  storage management for short-lived attributes  lack of high-quality inexpensive tools

20 ProblemsProblems  non-local computation  traversing parse tree  storage management for short-lived attributes  lack of high-quality inexpensive tools

21 ProblemsProblems  non-local computation  traversing parse tree  storage management for short-lived attributes  lack of high-quality inexpensive tools

22 Ad-Hoc Analysis  In rule-based evaluators, a sequence of actions are associated with grammar productions

23 Ad-Hoc Analysis  Organizing actions required for context-sensitive analysis around structure of the grammar leads to powerful, albeit ad-hoc, approach which is used on most parsers

24 Ad-Hoc Analysis  A snippet of code (action) is associated with each production that executes at parse time  In top-down parsers, the snippet is added to the appropriate parsing routine

25 Ad-Hoc Analysis  A snippet of code (action) is associated with each production that executes at parse time  In top-down parsers, the snippet is added to the appropriate parsing routine

26 Ad-Hoc Analysis  In a bottom-up shift-reduce parsers, the actions are performed each time the parser performs a reduction.

27 LR(1) Skeleton Parser stack.push(dummy); stack.push(0); done = false; token = scanner.next(); while (!done) { s = stack.top(); if( Action[s,token] == “reduce A →  ”) { stack.pop(2  |  |); s = stack.top(); stack.push( A ); stack.push(Goto[s, A ]); } else if( Action[s,token] == “shift i”){ stack.push(token); stack.push(i); token = scanner.next(); }

28 LR(1) Skeleton Parser stack.push(dummy); stack.push(0); done = false; token = scanner.next(); while (!done) { s = stack.top(); if( Action[s,token] == “reduce A →  ”) { stack.pop(2  |  |); s = stack.top(); stack.push( A ); stack.push(Goto[s, A ]); } else if( Action[s,token] == “shift i”){ stack.push(token); stack.push(i); token = scanner.next(); }

29 LR(1) Skeleton Parser if( Action[s,token] == “reduce A →  ”) { stack.pop(2  |  |); s = stack.top(); stack.push( A ); stack.push(Goto[s, A ]); } invoke the code snippet

30 Productions Code snippet Number → Sign List Number.val ← – Sign.val  List.val Sign → + Sign.val ← 1 Sign → –Sign.val ← –1 List → BitList.val ← Bit.val List 0 → List 1 Bit List 0.val ← 2  List 1.val + Bit.val Bit → 0 Bit.val ← 0 Bit → 1 Bit.val ← 1