Copyright Joey Paquet, 20001 Lecture 9 Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)

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

Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Table-driven syntax-directed.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation.
Syntax Trees MathWorks Compiler Course – Day 5. Syntax Trees MathWorks Compiler Course – Day 5 Parser lexemes shift/reduce seq. Cfg tables Tree Symbols.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
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.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Chapter 2 A Simple Compiler
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Chapter 5 Intermediate Code Generation. Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and.
Lecture 14 Syntax-Directed Translation Harry Potter has arrived in China, riding the biggest initial print run for a work of fiction here since the Communist.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Computer Engineering Department Dr. Raed Al-Qadi Orwa Hamad.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
YANG Chap7 Semantic Processing Syntax-directed translation - analysis: variable declarations, type errors - synthesis: IR or actual code The semantic action.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
Chapter 7 Semantic Processing Prof Chung /9/17.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Introduction to code generation.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Code Generation and Intermediate Representations
Introduction to Compiling
Chapter 1 Introduction Major Data Structures in Compiler
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Joey Paquet, 2000, 2002, 2008, Lecture 8 Syntax-Directed Translation.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Joey Paquet, 2000, Lecture 8 Syntax-Directed Translation.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
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.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Lecture 9 Symbol Table and Attributed Grammars
Compiler design Introduction to code generation
An Overview to Compiler Design
Intermediate Code Generation
Syntax-Directed Translation Part II
CS 3304 Comparative Languages
Syntax-Directed Translation
Syntax-Directed Translation Part II
Syntax-Directed Translation Part II
Syntax-Directed Translation Part II
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)
Compiler design Table-driven syntax-directed translation
COMP 442/6421 – Compiler Design
Compiler design Review COMP 442/6421 – Compiler Design
Presentation transcript:

Copyright Joey Paquet, Lecture 9 Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)

Copyright Joey Paquet, Part I Top-Down Table-Driven Syntax-Directed Translation

Copyright Joey Paquet, Top-Down Table-Driven SDT Augment the parser algorithm to implement attribute migration Problem: the attributes have to be pushed and popped at a different pace compared to symbols on the stack Solution: use an additional stack (the semantic stack) to store the attributes

Copyright Joey Paquet, Top-Down Table-Driven SDT Insert example slides here

Copyright Joey Paquet, Part II Bottom-Up Table-Driven Syntax-Directed Translation

Copyright Joey Paquet, Bottom-Up SDT Syntax-directed translation is much easier to implement bottom-up than top-down. Synthetized attributes are propagated from the bottom-up, so we have this propagation mechanism for free in a bottom-up parse The presence of inherited attributes generally comes from the elimination of left-recursions and ambiguities. As these are not a problem in bottom-up parsing, we seldom need to process inherited attributes in bottom-up translation In bottom-up translation, the parse and semantic stacks move in synchronism. Semantic rules are triggered as handles are popped from the stack

Copyright Joey Paquet, Bottom-Up SDT The tree is built by grafting subtrees (handles) to the nodes The semantic actions build the subtrees and for the grafts generally through simple pointer manipulations Generally, all nodes (internal or leaves) are of the same type. Differences can be managed by using a variant record structure: nodeKind = (internal,leaf) treeNode = record token : tokenType case kind : nodeKind of internal : (left,right : *treeNode) leaf : (location : integer) end

Copyright Joey Paquet, Bottom-Up SDT Tree nodes may also contain a pointer to a linked list of instruction (e.g. 3AC or ASM) that will be filled later in the code generation (translation) phase. Such a representation enables high-level code optimization To manage the creating of the tree, the frames on the stack need to incorporate tree structures: symbolType = (nonTerm,term) frame = record case symbolType of nonTerm : (root : *nodePtr) term : (loc : integer) end

Copyright Joey Paquet, Bottom-Up SDT We need a makeTree function that will be used to create subtrees and a makeLeaf function that will be used to create tree leaves: nodePtr makeTree(op tokenType, rightSon,leftSon nodePtr{ nodePtr = new(nodePtr) rootPtr.kind = internal rootPtr.token = op rootPtr.left = leftSon rootPtr.right = rightSon return rootPtr}

Copyright Joey Paquet, Bottom-Up SDT nodePtr makeLeaf(tok tokenType, location integer{ rootPtr = new(nodePtr) leafPtr.kind = leaf leafPtr.token = tok leafPtr.location = location return leafPtr}

Copyright Joey Paquet, Bottom-Up SDT: Example Insert example slides here

Copyright Joey Paquet, Bottom-Up SDT We can use a similar process to build other kinds of intermediate representations A similar process can also be used to generate target code directly, but that diminishes the possibilities of high-level code optimization