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.

Slides:



Advertisements
Similar presentations
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Advertisements

Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
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.
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Abstract Syntax Tree (AST)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
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.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Syntax-Directed Translation
Chapter 2 A Simple Compiler
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.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
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.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Syntax Directed Definition and Syntax directed Translation
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
Top-Down Parsing.
Course Revision.. Contents  Lexical Analysis  Our Compiler Structure  Tokens, Lexemes & Patterns  RE’s & Regular Definitions  Transition Diagrams.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
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.
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.
Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation.
Semantic Analysis Attribute Grammar. Semantic Analysis  Beyond context free grammar  Is x declared before it is used?  Is x declared but never used?
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
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.
Semantic analysis Jakub Yaghob
Syntax-Directed Translation
Constructing Precedence Table
Chapter 5 Syntax Directed Translation
Syntax-Directed Translation Part I
CS 3304 Comparative Languages
Syntax-Directed Translation
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
SYNTAX DIRECTED TRANSLATION
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Nonrecursive Predictive Parsing
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Chapter 5 Syntax Directed Translation
Presentation transcript:

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 attributes) by arranging that 1.the parser first builds a tree, ignoring the actions of the SDT 2.the tree is then walked, executing the actions of the SDT Two interesting cases where SDDs may be implemented by SDTs whose actions can be executed during parsing, in 1 pass, without a parse tree being constructed  S-attributed SDD and parsing is LR (deterministic bottom-up) –this is the simplest case –all actions can be grouped at the end of a production –result is a “postfix SDT”  L-attributed SDD and parsing can be LL (deterministic top-down) –if a grammar can be parsed LL, it can also be parsed LR Compiler Construction1

Postfix SDTs If grammar can be parsed deterministically bottom-up, & all attributes “synthetic” Then all actions of a production can be executed along with the parser’s reduce step They can all be grouped at the end of the production, forming a postfix to it Compiler Construction2 L ::= E \n E ::= E 1 + T E ::= T T ::= T 1 * F T ::= F F ::= ( E ) F ::= digit { print(E.val) } { E.val=E 1.val+T.val } { E.val=T.val } { T.val=T 1.val*F.val } { T.val = F.val } { F.val=E.val } { F.val=digit.lexval } e.g. Postfix SDT for very-simple desk calculator all attributes are synthetic, all actions occur at end of production and from the parser’s point of view they are part of the production, the grammar is LR(1).

Implemention issues with postfix SDTs Attributes can be stored in records/structs/dictionaries on the symbol stack, alongside LR parser states on the state stack. Furthermore the grammar symbol itself need not be kept on the symbol stack If some attribute values have unbounded size – e.g. for strings or code lists – it is best to store fixed-size pointers in the stack structures, and store the actual values elsewhere When a parser reduce occur – say for T ::= T 1 * F – the stack records containing needed values (T1’s for T1.val, F’s for F.val) will be at known offsets (-1, 0) relative to the top of stack. The result can be left on top of the stack after reduce step. With single productions, e.g. T ::= F, no change to the topmost stack entry is necessary since the item at the top must stay at the top. (but top state will usually change) Compiler Construction3

Parser stack manipulations made explicit Compiler Construction4 L ::= E \n E ::= E 1 + T E ::= T T ::= T 1 * F T ::= F F ::= ( E ) F ::= digit { print (stack[top-1].val; top=top-1; } { stack[top-2].val=stack[top-2].val+stack[top].val; top=top-2; } { } stack[top-2].val=stack[top-2].val*stack[top].val; top=top-2; } { } { stack[top-2].val=stack[top-1].val; top=top-2; } { } // if we may assume that val and lexval attributes // occupy the same locations in a struct T: T.val=3 * E: E.val=6 … … … … … … … top effect is to place 9 into the existing E element’s.val and make top point to it

SDTs with actions inside productions - 1 Where SDD is not S-attributed (it may be L-attributed or worse) then no postfix SDT can be constructed: actions inside productions will be necessary productions have the form X ::=  {a}  where  is not  If such a SDT cannot be handled during parsing – because it is worse than L- attributed while still being acyclic – then  parse, ignoring actions, and producing an explicit parse tree  then, knowing what productions have been used to parse, add extra child nodes for the particular actions within those productions  perform preorder traversal of tree, executing the actions Compiler Construction5 X  X  {a}

SDTs with actions inside productions - 2 If such a SDT can be handled during parsing, then  if parsing bottom-up, treat the action like a non-terminal: –invent a unique marker non-terminal for each action –such a non-terminal has one empty production –perform the action when that non-terminal is on the top of the stack  if parsing top-down –perform the action just before processing the next grammar symbol checking the next terminal, if  begins with a terminal expanding the next non-terminal, if  begins with a non-terminal Compiler Construction6 X ::=  M 39  M 39 ::= 

Eliminating left recursion from SDTs Grammars, and hence SDTs, cannot be used for top-down parsing if left recursive Treat actions like terminals, since order of terminals is preserved by left recursion elimination  (although if any production begins with an action, the grammar with SDT will not be possible to parse deterministically either top-down or even bottom-up, and it will then be necessary to walk a tree) General idea of left recursion elimination (without actions) is to replace left recursion by right recursion involving a new non-terminal capable of generating  Compiler Construction7 A ::= A  |  A ::= b R R ::=  R |  With actions {a} and {b} in SDD, similar transformation requires care A ::= A  {a} |  {b} A ::= b {?1} R {?2} R ::=  {?3} R {?4} |  {?5}

… the right-recursive version should achieve same result Compiler Construction8 Example left-recursive original SDT with only one S-attribute ‘q’ A ::= A 1 b { A.q=f(A 1.q, b.q);} | c { A.q=g(c);} For sample input ‘cbb’, flow of information is always upwards A.q=f(g(c),b.q) A.q=f(f(g(c),b.q),b.q) A A A b c b A.q=g(c) After eliminating left recursion, the shape of the parse tree will be radically different, but the end result calculated for topmost A.q should be the same

… needing inherited attributes Compiler Construction9 A.q=f(g(c),b.q) A.q=f(f(g(c),b.q),b.q) A A A b c b A.q=g(c) A R R R b b c  Only at this point can it be known that the correct result is f(f(g(c),b.q),b.q) A.q=R.s R.i=g(c) R.s=R 1.s R 1.i=f(g(c),b.q) R.s=R 1.s R 1.i=f(f(g(c),b.q),b.q) R.s=R.i with left recursive grammar

… and appropriate evaluation order Getting the actions done in the right order and with the right information available requires use of inherited attributes  values of inherited attributes must be calculated immediately before the right- recursive use of the non-terminal R Getting the result(s) back to the top node involves a new synthesized attribute(s)  values of synthesized attributes may be calculated right at the ends of productions, just as with postfix SDTs Compiler Construction10 A ::= c {R.i=g(c)} R {A.q=R.s} R ::= b {R 1.i=f(R.i, b.q)} R {R.s=R 1.s} R ::=  {R.s=R.i}