Syntax Directed Translation
|
|
- Brice Stone
- 6 years ago
- Views:
Transcription
1 Syntax Directed Translation
2 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 type of arguments does it take? Is x declared before being used? Where can x be stored? Is the expression x+y type-consistent? Semantic analysis is the phase where we collect information about the types of expressions and check for type related errors. The more information we can collect at compile time, the less overhead we have at run time.
3 Semantic analysis Collecting type information may involve "computations" What is the type of x+y given the types of x and y? Tool: attribute grammars Each grammar symbol has a number of associated attributes: The type of a variable or expression The value of a variable or expression The code for a statement The grammar is augmented with special equations (called semantic actions) that specify how the values of attributes are computed from other attributes. The process of using semantic actions to evaluate attributes is called syntax-directed translation.
4 Syntax Directed Translation Syntax = form, Semantics = meaning Use the syntax to derive semantic information. Attribute grammar: Context free grammar augmented by a set of rules Each symbol in the derivation has a set of values, or attributes The rules specify how to compute a value for each attribute
5 Attribute Grammars Associate attributes with tree nodes (internal and leaf). Rules describe how to compute value of attributes in tree (possibly using other attributes in the tree) Two types of attributes based on how value is calculated (Synthesized & Inherited)
6 Attribute Grammar Production E E 1 + T E T T T 1 * T num ( E ) Semantic Actions E.val = E 1.val + T.val E.val = T.val T.val = T 1.val *.val T.val =.val.val = value(num).val = E.val Each node has single integer attribute val
7 Synthesized Attributes Synthesized attributes the value of a synthesized attribute for a node is computed using only information associated with the node and the node s children (or the lexical analyzer for leaf nodes). Example: Production Semantic Rules A B C D A.a := B.b + C.e
8 Example Problems for Synthesized Expression grammar given a valid expression (ex: 1 * 2 + 3), determine the associated value while parsing. Grid Given a starting location of 0,0 and a sequence of north, south, east, west moves (ex: NESNNE), find the final position on a unit grid.
9 Synthesized Attributes Expression Grammar Production E E 1 + T E T T T 1 * T num ( E ) Semantic Actions E.val = E 1.val + T.val E.val = T.val T.val = T 1.val *.val T.val =.val.val = value(num).val = E.val
10 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val = Val = = 2 Val = Val = Val = = 3 Val = Val = Val = = 4
11 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val = 2 = 2 = 3 = 4
12 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val =2 Val = 2 = 2 = 3 = 4
13 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val =2 Val = 2 = 2 Val = 3 = 3 = 4
14 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val =2 Val = 2 = 2 Val =2*3=6 Val = 3 = 3 = 4
15 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val =2 Val = 2 = 2 Val =6 Val =6 Val = 3 = 3 = 4
16 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val =2 Val = 2 = 2 Val =6 Val =6 Val = 3 = 3 Val =4 = 4
17 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val =2 Val = 2 = 2 Val =6 Val =6 Val = 3 = 3 Val =4 Val =4 = 4
18 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 * E E + T T T * Val =2 Val = 2 = 2 Val =6 Val =6 Val = 3 = 3 Val =6 + 4 = 10 Val =4 Val =4 = 4
19 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: 2 + 3* 4 E E + T T Val = Val = Val = = 2 Val = T * Val = Val = = 3 Val = Val = = 4
20 Synthesized Attributes Annotating the parse tree Production Semantic Actions E E 1 + T E.val = E 1.val + T.val E T E.val = T.val T T 1 * T.val = T 1.val *.val T T.val =.val num.val = value(num) ( E ).val = E.val Input: * 4 E E + T T Val =2 = 2 Val =2 Val =2 Val =14 T * Val =3 Val =3 = 3 Val =12 Val =4 = 4
21 Grid Example Given a starting location of 0,0 and a sequence of north, south, east, west moves (ex: NEENNW), find the final position on a unit grid. start final
22 Synthesized Attributes Grid Positions Production Semantic Actions seq seq 1 instr seq.x = seq 1.x + instr.dx seq.y = seq 1.y + instr.dy seq BEGIN seq.x = 0, seq.y = 0 instr NORTH instr.dx = 0, instr.dy = 1 instr SOUTH instr.dx = 0, instr.dy = -1 instr EAST instr.dx = 1, instr.dy = 0 instr WEST instr.dx = -1, instr.dy = 0
23 Synthesized Attributes Annotating the parse tree Production Semantic Actions seq seq 1 instr seq.x = seq 1.x + instr.dx seq.y = seq 1.y + instr.dy seq BEGIN seq.x = 0, seq.y = 0 instr NORTH instr.dx = 0, instr.dy = 1 instr SOUTH instr.dx = 0, instr.dy = -1 instr EAST instr.dx = 1, instr.dy = 0 instr WEST instr.dx = -1, instr.dy = 0 x= y= x= y= seq x= y= seq x= y= seq dx= dy= instr seq dx= dy= instr S dx= dy= instr S Input: BEGIN N W S S x= y= seq BEGIN dx= dy= instr N W
24 Synthesized Attributes Annotating the parse tree Production Semantic Actions seq seq 1 instr seq.x = seq 1.x + instr.dx seq.y = seq 1.y + instr.dy seq BEGIN seq.x = 0, seq.y = 0 instr NORTH instr.dx = 0, instr.dy = 1 instr SOUTH instr.dx = 0, instr.dy = -1 instr EAST instr.dx = 1, instr.dy = 0 instr WEST instr.dx = -1, instr.dy = 0 x=0 y=1 x=-1 y=1 seq x=-1 y=0 seq x=-1 y=01 seq dx=-1 dy=0 instr seq dx=0 dy=-1 instr S dx=0 dy=-1 instr S Input: BEGIN N W S S x=0 y=0 seq BEGIN dx=0 dy=1 instr N W
25 Inherited Attributes Inherited attributes if an attribute is not synthesized, it is inherited. Example: Production A B C D Semantic Rules B.b := A.a + C.b Convenient for expressing the dependence of a programming language construct in the context in which it appears inherited attribute can keep track of whether an identifier appears on the left or right side of an assignment inherited attribute can keep track of type information
26 Inherited Attributes Determining types Productions Decl Type Type int Type real 1, id id Semantic Actions.in = Type.type Type.type = INT T.type = REAL 1.in =.in, addtype(id.entry..in) addtype(id.entry,.in)
27 Inherited Attributes Example Productions Decl Type Type int Semantic Actions.in = Type.type Type.type = INT type= Decl Type in= Type real T.type = REAL 1, id id 1.in =.in, addtype(id.entry..in) addtype(id.entry,.in) int in=,, in= id = c id = b Input: int a,b,c id = a
28 Inherited Attributes Example Productions Decl Type Type int Type real 1, id id Input: int a,b,c Semantic Actions.in = Type.type Type.type = INT T.type = REAL 1.in =.in, addtype(id.entry..in) addtype(id.entry,.in) type=int Type int Decl in=int,, id = a in=int in=int id = c id = b
29 Example grammar ber Sign Sign + Bit Bit Bit 0 1 This grammar describes signed binary numbers We would like to augment it with rules that compute the decimal value of each valid input string
30 Examples or 1 or 101 ber Sign Bit 1 ber Sign Sign Bit Sign 1 Sign Bit 1 Sign 1 1 Sign ber Sign Bit 0 1 ber Sign Bit Bit 1 Sign Bit Bit 0 1 1
31 Add rules to compute the decimal value of a signed binary number Productions Attribution Rules ber Sign.pos 0 If Sign.neg then ber.val.val else ber.val.val Sign + Sign.neg false Sign.neg true 0 1 Bit 1.pos 0.pos + 1 Bit.pos 0.pos 0.val 1.val + Bit.val Bit Bit.pos.pos.val Bit.val Bit 0 Bit.val 0 1 Bit.val 2 Bit.pos Symbol ber Sign Bit Attributes val neg pos, val pos, val
32 Rules + parse tree imply an attribute dependence graph Back to the Examples or 1 neg true Sign ber ber.val.val 1.pos 0.val Bit.val 1 One possible evaluation order: 1.pos 2 Sign.neg 3 Bit.pos 4 Bit.val 5.val 6 ber.val Bit Bit.pos 0 Bit.val 2 Bit.pos 1 Other orders are possible 1 Knuth suggested a data-flow model for evaluation Independent attributes first Others in order as input values become available Evaluation order must be consistent with the attribute dependence graph
33 Back to the Examples Sign ber neg: true val: 5 pos: 1 val: 4 pos: 0 val: 5 Bit pos: 0 val: 1 This is the complete attribute dependence graph for 101. It shows the flow of all attribute values in the example. Some flow downward pos: 2 val: 4 Bit pos: 1 val: 0 1 inherited attributes Some flow upward Bit 1 pos: 2 val: 4 0 or 101 synthesized attributes A rule may use attributes in the parent, children, or siblings of a node
34 The Rules of the Game Attributes associated with nodes in parse tree Rules are value assignments associated with productions Attribute is defined once, using local information Label identical terms in production for uniqueness Rules & parse tree define an attribute dependence graph Graph must be non-circular This produces a high-level, functional specification Synthesized attribute Depends on values from children Inherited attribute Depends on values from siblings & parent
35 Using Attribute Grammars Attribute grammars can specify context-sensitive actions Take values from syntax Perform computations with values Insert tests, logic, Synthesized Attributes Use values from children & from constants S-attributed grammars Evaluate in a single bottom-up pass Good match to LR parsing Inherited Attributes Use values from parent, constants, & siblings directly express context can rewrite to avoid them Thought to be more natural Not easily done at parse time We want to use both kinds of attribute
36 Evaluation Methods Dynamic, dependence-based methods Build the parse tree Build the dependence graph Topological sort the dependence graph Define attributes in topological order Rule-based methods Analyze rules at compiler-generation time Determine a fixed (static) ordering Evaluate nodes in that order Oblivious methods Ignore rules & parse tree Pick a convenient order (at design time) & use it (treewalk) (passes, dataflow)
37 Back to the Example ber Sign Bit Bit 1 Bit 0 1 or 101
38 Back to the Example ber val: Sign neg: pos: 0 val: pos: val: Bit pos: val: pos: val: Bit pos: val: 1 Bit pos: val: 0 1 or 101
39 Back to the Example ber val: 5 Inherited Attributes Sign neg: true pos: 0 val: 5 pos: 1 val: 4 Bit pos: 0 val: 1 pos: 2 val: 4 Bit pos: 1 val: 0 1 Bit pos: 2 val: or 101
40 Back to the Example ber val: 5 Synthesized attributes Sign neg: true pos: 0 val: 5 pos: 1 val: 4 Bit pos: 0 val: 1 pos: 2 val: 4 Bit pos: 1 val: 0 1 Bit pos: 2 val: or 101
41 Back to the Example ber val: 5 Synthesized attributes Sign neg: true pos: 0 val: 5 pos: 1 val: 4 Bit pos: 0 val: 1 pos: 2 val: 4 Bit pos: 1 val: 0 1 Bit pos: 2 val: or 101
42 Back to the Example ber val: 5 If we show the computation... Sign neg: true pos: 0 val: 5 pos: 1 val: 4 Bit pos: 0 val: 1 & then peel away the parse tree... pos: 2 val: 4 Bit pos: 1 val: 0 1 Bit pos: 2 val: or 101
43 Back to the Example val: 5 All that is left is the attribute dependence graph. neg: true pos: 0 val: 5 This succinctly represents the flow of values in the problem instance. pos: 2 val: 4 pos: 1 val: 4 pos: 1 val: 0 1 pos: 0 val: 1 The dynamic methods sort this graph to find independent values, then work along graph edges. pos: 2 val: 4 0 The rule-based methods try to discover good orders by analyzing the rules. 1 or 101 The oblivious methods ignore the structure of this graph. The dependence graph must be acyclic
44 Attribute Dependency An attribute b depends on an attribute c if a valid value of c must be available in order to find the value of b. The relationship among attributes defines a dependency graph for attribute evaluation. Dependencies matter when considering syntax directed translation in the context of a parsing technique.
45 Dependency Graph rom dependencies, we can construct dependency graph which shows the order in which evaluations must be performed assuming acyclic graph do topological ordering to evaluate nodes ni evaluate ni before nj nj is dependent on ni nj Algorithm : Refer Page 296 of Aho, Sethi and Ullman Text 45
46 Attribute Dependencies Production E E 1 + T E T T T 1 * T num ( E ) Semantic Actions E.val = E 1.val + T.val E.val = T.val T.val = T 1.val *.val T.val =.val.val = value(num).val = E.val Synthesized attributes dependencies always up the tree E E + T T Val = 2 = 2 Val = 2 Val = 2 Val = 14 T * Val = 3 Val = 3 = 3 Val = 12 Val = 4 = 4
47 Acyclic Dependency Graphs for Parse Trees A.a A X Y X.x Y.y A.a := f(x.x, Y.y) A.a X.x Y.y X.x := f(a.a, Y.y) Direction of A.a value dependence X.x Y.y Y.y := f(a.a, X.x)
48 Dependency graph E.val = 10 E E.val = 6 E + T T.val = 4 T.val = 6 T.val = 4 T.val = 2 T *.val = 3 digit digit = 4.val = 2 digit digit = 3 Dash edges shows the dependencies digit = 2 digit
49 Attribute Dependencies Productions Decl Type Type int Type real 1, id id Semantic Actions.in = Type.type Type.type = INT T.type = REAL 1.in =.in, addtype(id.entry..in) addtype(id.entry,.in) Type=int Type int Decl in=int addtype(b,int),, id = a in=int addtype(c,int) id = c id = b in=int addtype(a,int)
50 Dependency graph of declaration grammar D T.type = int T L1 L1.in = T.type int L2 L2.in = L1.in, id addtype(id.entry, L1.in) L.in = L.in L3, id addtype(id.entry, L2.in) id addtype(id.entry, L3.in) dashed edges show dependencies
51 Evaluation Order A topological sort of a directed acyclic graph (DAG) is any ordering m 1, m 2,, m n of the nodes of the graph, such that if m i m j is an edge then m i appears before m j Any topological sort of a dependency graph gives a valid evaluation order for the semantic rules
52 Input: real p,q D L.in=real 4 T L T.type=real L1.in=real 6, addtype(q,real) 5 real L, id 3 addtype(p,real) 7 id.entry=q 2 id parse tree id.entry=p 1 dependency graph
53 Synthesized Attributes and LR Parsing Synthesized attributes have natural fit with LR parsing Attribute values can be stored on stack with their associated symbol When reducing by production A a, both a and the value of a s attributes will be on the top of the LR parse stack!
54 Examples covered in Lecture Class 54
Chapter 2: Syntax Directed Translation and YACC
Chapter 2: Syntax Directed Translation and YACC 長庚大學資訊工程學系陳仁暉助理教授 Tel: (03) 211-8800 Ext: 5990 Email: jhchen@mail.cgu.edu.tw URL: http://www.csie.cgu.edu.tw/~jhchen All rights reserved. No part of this
More informationContext-sensitive Analysis
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], h, i, j, k; char *p; fie(h,i, ab,j, k); k =
More informationContext-sensitive Analysis
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; { } What is wrong with this program? (let me count the ways ) fee() { int
More informationContext-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Context-sensitive Analysis Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int
More informationContext-sensitive Analysis Part II Chapter 4 (up to Section 4.3)
Context-sensitive Analysis Part II Chapter 4 (up to Section 4.3) Attribute Grammars Add rules to compute the decimal value of a signed binary number Two kinds of Attributes Synthesized attribute Bottom-Up
More informationGrammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones
Grammars CS434 Lecture 5 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled
More informationSyntax-Directed Translation Part I
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011 2 The Structure of our Compiler Revisited Character stream
More informationSyntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans
Syntax-Directed Translation CS3300 - Compiler Design Syntax Directed Translation V. Krishna Nandivada IIT Madras Attach rules or program fragments to productions in a grammar. Syntax directed definition
More informationSyntactic Directed Translation
Syntactic Directed Translation Attribute Grammars & Syntax-Directed Definitions Copyright 206, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern
More informationContext-sensitive Analysis Part II
Context-sensitive Analysis Part II Attribute Grammars Add rules to compute the decimal value of a signed binary number Back to the Examples For Symbol Attributes.neg Number val neg pos, val pos, val Back
More information[Syntax Directed Translation] Bikash Balami
1 [Syntax Directed Translation] Compiler Design and Construction (CSc 352) Compiled By Central Department of Computer Science and Information Technology (CDCSIT) Tribhuvan University, Kirtipur Kathmandu,
More informationCompilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1
Compilers 5. Attributed Grammars Laszlo Böszörmenyi Compilers Attributed Grammars - 1 Adding Attributes We connect the grammar rules with attributes E.g. to implement type-checking or code generation A
More informationSyntax-Directed Translation
Syntax-Directed Translation What is syntax-directed translation? The compilation process is driven by the syntax. The semantic routines perform interpretation based on the syntax structure. Attaching attributes
More informationThere is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar
Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { } What is wrong with this program? (let me count the ways ) fee() { int f[3],g[0], h, i, j, k; char
More informationSyntax-Directed Translation
Syntax-Directed Translation 1 Syntax-Directed Translation 1. We associate information with the programming language constructs by attaching attributes to grammar symbols. 2. Values of these attributes
More informationContext-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis
Semantic Processing The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure
More informationSyntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions
Concepts Introduced in Chapter 5 Syntax-Directed Definitions Translation Schemes Synthesized Attributes Inherited Attributes Dependency Graphs Syntax-Directed Translation Uses a grammar to direct the translation.
More informationSyntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?
Syntax-directed translation Context-sensitive analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Syntax- Directed Transla>on The Structure of the
More informationSyntax-Directed Translation
Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic
More informationSyntax-Directed Translation. Introduction
Syntax-Directed Translation Introduction Translation of languages guided by context-free grammars Attach attributes to the grammar symbols Values of the attributes are computed by semantic rules associated
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 10! Con:nua:on of the course Syntax- Directed Transla:on
More informationSyntax Directed Translation
Syntax Directed Translation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax
More informationCOP5621 Exam 3 - Spring 2005
COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred
More informationSyntax-Directed Translation
Syntax-Directed Translation Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are evaluated
More informationSyntax-Directed Translation Part II
Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed
More informationSemantic Analysis Attribute Grammars
Semantic Analysis Attribute Grammars Martin Sulzmann Martin Sulzmann Semantic Analysis Attribute Grammars 1 / 18 Syntax versus Semantics Syntax Analysis When is a program syntactically valid? Formalism:
More informationΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos
ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Syntax-directed TranslaPon (SDT) Μετάφραση Κατευθυνόμενη από τη Σύνταξη We
More informationSemantic Analysis with Attribute Grammars Part 1
with Attribute Grammars Part 1 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture Introduction
More informationSyntactic Directed Translation
Sntactic Directed Translation What is Sntax-Directed Translation? Translation Process guided b Context-Free Grammars Attach Attributes to Grammar Smbols Attribute Grammars & Sntax-Directed Definitions
More informationSyntax-Directed Translation
Syntax-Directed Translation ALSU Textbook Chapter 5.1 5.4, 4.8, 4.9 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 What is syntax-directed translation? Definition: The compilation
More informationWe now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a
Attribute Grammars Attribute Grammars were invented by Don Knuth as a way to unify all of the stages of compiling into one. They give a formal way to pass semantic information (types, values, etc.) around
More informationCompiler Principle and Technology. Prof. Dongming LU April 15th, 2019
Compiler Principle and Technology Prof. Dongming LU April 15th, 2019 PART TWO 6. Semantic Analysis Contents Part One 6.1 Attributes and Attribute Grammars Part Two 6.2 Algorithms for Attribute Computation
More informationSemantic analysis. Syntax tree is decorated with typing- and other context dependent
Semantic analysis Semantic analysis Semantic analysis checks for the correctness of contextual dependences: { nds correspondence between declarations and usage of identiers, { performs type checking/inference,
More informationPART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 4 - SYNTAX DIRECTED TRANSLATION F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 109 / 309 Setting Translation of context-free languages Information attributes of grammar symbols Values
More information5. Semantic Analysis!
5. Semantic Analysis! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/! http://www.cs.purdue.edu/homes/hosking/!
More information10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.
Outline Semantic Analysis In Text: Chapter 3 Static semantics Attribute grammars Dynamic semantics Operational semantics Denotational semantics N. Meng, S. Arthur 2 Syntax vs. Semantics Syntax concerns
More informationSummary: Semantic Analysis
Summary: Semantic Analysis 1 Basic Concepts When SA is performed: Semantic Analysis may be performed: In a two-pass compiler: after syntactic analysis is finished, the semantic analyser if called with
More informationAttribute Grammars. Attribute Grammars
Attribute Grammars Definitions: synthesized, inherited, dependence graph Example: syntax-directed translation S-attributed grammars -attributed grammars Bottom Up euation of inherited attributes Top Down
More informationLecture 14 Sections Mon, Mar 2, 2009
Lecture 14 Sections 5.1-5.4 Hampden-Sydney College Mon, Mar 2, 2009 Outline 1 2 3 4 5 Parse A parse tree shows the grammatical structure of a statement. It includes all of the grammar symbols (terminals
More informationAbstract Syntax Trees Synthetic and Inherited Attributes
Abstract Syntax Trees Synthetic and Inherited Attributes Lecture 22 Sections 5.1-5.2 Robb T. Koether Hampden-Sydney College Mon, Mar 16, 2015 Robb T. Koether (Hampden-Sydney College)Abstract Syntax TreesSynthetic
More informationintermediate code generator syntax tree token stream intermediate representation parser checker tree
Context-sensitive analysis context-sensitive questions might the compiler What ask? 1. Is x a scalar, anarray, or a function? 2. Is x declared before it is used? 3. Are any names declared but not used?
More information5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz
5. Semantic Analysis Mircea Lungu Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/
More informationAbstract Syntax Tree
Abstract Syntax Tree Condensed form of parse tree, useful for representing language constructs. The production S if B then s1 else s2 may appear as if-then-else B s1 s2 1 Abstract Syntax tree Chain of
More information1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]
Code No: R05311201 Set No. 1 1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8] 2. Compute the FIRST and FOLLOW sets of each
More informationChapter 4. Action Routines
Chapter 4 Action Routines Syntax and Semantics In general: Syntax form Semantics meaning In programming languages: Syntax part of the language definition that can be described via a context-free grammar
More informationSyntax Directed Translation
CS143 Handout 16 Summer 2011 July 6 th, 2011 Syntax Directed Translation Handout written by Maggie Johnson and revised by Julie Zelenski. Syntax-directed translation refers to a method of compiler implementation
More informationCOMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis
Prelude COMP 8 October, 9 What is triskaidekaphobia? Fear of the number s? No aisle in airplanes, no th floor in buildings Fear of Friday the th? Paraskevidedekatriaphobia or friggatriskaidekaphobia Why
More informationCS 406/534 Compiler Construction LR(1) Parsing and CSA
CS 406/534 Compiler Construction LR(1) Parsing and CSA Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy and
More information5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz
5. Semantic Analysis Mircea Lungu Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/
More informationCOP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher
COP4020 Programming Languages Semantics Robert van Engelen & Chris Lacher Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees Static Semantics Syntax concerns the form
More informationCOP4020 Programming Languages. Semantics Prof. Robert van Engelen
COP4020 Programming Languages Semantics Prof. Robert van Engelen Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees COP4020 Spring 2011 2 Static Semantics Syntax concerns
More informationAbstract Syntax Trees & Top-Down Parsing
Abstract Syntax Trees & Top-Down Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationSemantic Analysis with Attribute Grammars Part 3
with Attribute Grammars Part 3 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture Introduction
More informationAbstract Syntax Trees & Top-Down Parsing
Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree
More informationChapter 4 :: Semantic Analysis
Chapter 4 :: Semantic Analysis Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter04_Semantic_Analysis_4e - Tue November 21, 2017 Role of Semantic Analysis
More informationSemantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.
SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens
More informationGujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)
Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov 2014 Compiler Design (170701) Question Bank / Assignment Unit 1: INTRODUCTION TO COMPILING
More informationCS 406: Syntax Directed Translation
CS 406: Syntax Directed Translation Stefan D. Bruda Winter 2015 SYNTAX DIRECTED TRANSLATION Syntax-directed translation the source language translation is completely driven by the parser The parsing process
More informationThe Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis
The Compiler So Far Overview of Semantic Analysis Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Lexical analysis Detects inputs with illegal tokens Parsing Detects inputs with ill-formed
More informationStatic and Dynamic Semantics
Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Semantic Analysis In this set of notes you will learn about: Static semantics Dynamic semantics Attribute grammars Abstract syntax trees
More informationCS415 Compilers Context-Sensitive Analysis Type checking Symbol tables
CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Lecture 18 1 Announcements
More informationSYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram
CS6660 COMPILER DESIGN Question Bank UNIT I-INTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools
More informationPART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 264
PART 4 - SYNTAX DIRECTED TRANSLATION F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2015 109 / 264 Setting Translation of context-free languages Information attributes of grammar symbols Values
More informationError Handling Syntax-Directed Translation Recursive Descent Parsing
Error Handling Syntax-Directed Translation Recursive Descent Parsing Lecture 6 by Professor Vijay Ganesh) 1 Outline Recursive descent Extensions of CFG for parsing Precedence declarations Error handling
More informationCOMPILER CONSTRUCTION (Evaluation Orders for SDD s)
COMPILER CONSTRUCTION (Evaluation Orders for SDD s) Prof. K R Chowdhary Email: kr.chowdhary@jietjodhpur.ac.in Campus Director, JIET, Jodhpur Thursday 18 th October, 2018 kr chowdhary Code generation 1/
More informationCSE3322 Programming Languages and Implementation
Monash University School of Computer Science & Software Engineering Sample Exam 2004 CSE3322 Programming Languages and Implementation Total Time Allowed: 3 Hours 1. Reading time is of 10 minutes duration.
More informationSyntactic Directed Translation
Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission
More informationUNIT IV INTERMEDIATE CODE GENERATION
UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of
More informationChapter 5 Syntax Directed Translation
Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 5060: Compiler Design Discussion Chapter 5 Syntax Directed Translation Eng. Eman R. Habib May, 2014 2 Computer Architecture
More informationLECTURE NOTES ON COMPILER DESIGN P a g e 2
LECTURE NOTES ON COMPILER DESIGN P a g e 1 (PCCS4305) COMPILER DESIGN KISHORE KUMAR SAHU SR. LECTURER, DEPARTMENT OF INFORMATION TECHNOLOGY ROLAND INSTITUTE OF TECHNOLOGY, BERHAMPUR LECTURE NOTES ON COMPILER
More informationNARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1
SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different
More informationAnatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?
Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator
More informationSemantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University
Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a
More informationStatic Checking and Intermediate Code Generation Pat Morin COMP 3002
Static Checking and Intermediate Code Generation Pat Morin COMP 3002 Static Checking and Intermediate Code Generation Parser Static Checker Intermediate Code Generator Intermediate Code Generator Parse
More informationProgramming Languages
Programming Languages Tevfik Koşar Lecture - IX February 14 th, 2006 1 Roadmap Semantic Analysis Role of Semantic Analysis Static vs Dynamic Analysis Attribute Grammars Evaluating Attributes Decoration
More informationCompilers. Compiler Construction Tutorial The Front-end
Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers
More informationProblem Score Max Score 1 Syntax directed translation & type
CMSC430 Spring 2014 Midterm 2 Name Instructions You have 75 minutes for to take this exam. This exam has a total of 100 points. An average of 45 seconds per point. This is a closed book exam. No notes
More informationMore Assigned Reading and Exercises on Syntax (for Exam 2)
More Assigned Reading and Exercises on Syntax (for Exam 2) 1. Read sections 2.3 (Lexical Syntax) and 2.4 (Context-Free Grammars) on pp. 33 41 of Sethi. 2. Read section 2.6 (Variants of Grammars) on pp.
More information5. Syntax-Directed Definitions & Type Analysis
5. Syntax-Directed Definitions & Type Analysis Eva Rose Kristoffer Rose NYU Courant Institute Compiler Construction (CSCI-GA.2130-001) http://cs.nyu.edu/courses/spring15/csci-ga.2130-001/lecture-5.pdf
More informationLECTURE 3. Compiler Phases
LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent
More informationSemantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?
Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate
More informationUNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.
UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are
More informationCSCI Compiler Design
CSCI 565 - Compiler Design Spring 2015 Midterm Exam March 04, 2015 at 8:00 AM in class (RTH 217) Duration: 2h 30 min. Please label all pages you turn in with your name and student number. Name: Number:
More informationCSCI Compiler Design
University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 CSCI 565 - Compiler Design Fall 26 Midterm Exam Solution Problem : Context-Free-Grammars and Parsing Algorithms [4 points]
More informationIntermediate Code Generation
Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target
More informationEvaluation of Semantic Actions in Predictive Non- Recursive Parsing
Evaluation of Semantic Actions in Predictive Non- Recursive Parsing José L. Fuertes, Aurora Pérez Dept. LSIIS School of Computing. Technical University of Madrid Madrid, Spain Abstract To implement a syntax-directed
More information10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG
Attribute Evaluation Order Determining attribute 2 expected_type evaluation order actual_type actual_type for any attribute grammar is a 5 complex problem, 1 [1] [2] requiring
More informationSemantic Analysis. Lecture 9. February 7, 2018
Semantic Analysis Lecture 9 February 7, 2018 Midterm 1 Compiler Stages 12 / 14 COOL Programming 10 / 12 Regular Languages 26 / 30 Context-free Languages 17 / 21 Parsing 20 / 23 Extra Credit 4 / 6 Average
More informationAttribute Grammars. An Example. Using an Attribute Grammar. Recall the context-sensitive language from Chapter 1: L = { a n b n c n n!
Attribute Grammars An attribute grammar is a context-free grammar that has been extended to provide contextsensitive information by appending attributes to some of its nonterminals. Each distinct symbol
More informationIntroduction to Computer Science Unit 2. Exercises
Introduction to Computer Science Unit 2. Exercises Note: Curly brackets { are optional if there is only one statement associated with the if (or ) statement. 1. If the user enters 82, what is 2. If the
More informationRYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS
RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 STUDENT ID: INSTRUCTIONS Please write your student ID on this page. Do not write it or your name
More informationMore On Syntax Directed Translation
More On Syntax Directed Translation 1 Types of Attributes We have productions of the form: A X 1 X 2 X 3... X n with semantic rules of the form: b:= f(c 1, c 2, c 3,..., c n ) where b and the c s are attributes
More informationSemantic Analysis. Compiler Architecture
Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Source Compiler Architecture Front End Scanner (lexical tokens Parser (syntax Parse tree Semantic Analysis
More informationLecture Compiler Construction
Lecture Compiler Construction Franz Wotawa wotawa@ist.tugraz.at Institute for Software Technology Technische Universität Graz Inffeldgasse 16b/2, A-8010 Graz, Austria Summer term 2017 F. Wotawa (IST @
More informationA programming language requires two major definitions A simple one pass compiler
A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:
More informationStatic semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics
Lecture 3-6: Semantics Static semantics Attribute grammars Dynamic semantics Denotational semantics: semantic equations Axiomatic semantics: inference rules and correctness proofs Static semantics Semantics
More informationLecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating
1 ecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating records for arrays o Short-circuiting Or o If statement
More informationLecture 7: Type Systems and Symbol Tables. CS 540 George Mason University
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing
More information