Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Computer Engineering Department Dr. Raed Al-Qadi Orwa Hamad.

Slides:



Advertisements
Similar presentations
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Lecture # 11 Grammar Problems.
Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
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)
Top-Down Parsing.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Slide1 Chapter 4 Lexical and Syntax Analysis. slide2 OutLines: In this chapter a major topics will be discussed : Introduction to lexical analysis, including.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
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.
9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)
Lexical and syntax analysis
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Syntax Directed Definitions Synthesized Attributes
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
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 Review Joey Paquet,
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CS 363 Comparative Programming Languages Semantics.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
CPS 506 Comparative Programming Languages Syntax Specification.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Simple One-Pass Compiler
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Parsing Top-Down.
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.
LESSON 04.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Syntax Directed Definition and Syntax directed Translation
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Top-Down Parsing.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
2016/7/9Page 1 Lecture 11: Semester Review COMP3100 Dept. Computer Science and Technology United International College.
Programming Languages Translator
Chapter 5 Syntax Directed Translation
ASTs, Grammars, Parsing, Tree traversals
Syntax-Directed Translation
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Nonrecursive Predictive Parsing
Chapter 5 Syntax Directed Translation
Presentation transcript:

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Computer Engineering Department Dr. Raed Al-Qadi Orwa Hamad

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Syntax-Directed Translation (SDT): Associate actions with grammar productions * when production is applied, action is invoked. * Natural way to build parse tree or generate code. * Order in which action applied depends on grammar and sentence being parsed (i.e. Syntax-directed). Start with translation rules * Expression result as a function of * Constants * RHS’s terminal’s value * RHS’s non-terminal’s translations results Hand-Convert to actions

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: String to integers: Simple translation since values from RHS used in order: ProductionTranslation Rules S  digit S.val= ord (digit.value)-ord(“0”) S  SS Reverse sequence of letters * Non-simple translation (values used in reverse order) Subscript identifiers the instance of SVal is the attribute of non-terminals used to discribe the translation

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: Binary to Decimal ProductionTranslation B  0 B.dec=0  1 B.dec=1  B0  B1 B B 1 B *2 1*2*2+1

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Implementing SDT: Have a semantic stack (SS) that parallels to the parse stack * Semantic stack holds results from RHS translations * When translation rule applied, it can find the results of previously applied translations on semantic stack. * Its result is pushed on stack. Action manipulate semantic stack directly Parse Stack Semantic Stack

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Writing a SDT: Write translation rules (one per production) * Define the name and meaning of symbol’s attributes. * LHS’s translation can use any (or none) result from the RHS’s previously applied translations. Convert rules to actions * Actions are mixed in with RHS of productions. * Actions manipulate semantic stack directly * Pop all of RHS’s results * Compute LHS’s function *Push result onto semantic stack Make grammar LL(1), carrying along actions

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: Binary to Decimal ProductionTranslation B  0 B.dec=0  1 B.dec=1  B0  B1 Actions #1 PushSS(0); #2 PushSS(1); #3 PushSS(2*popSS()); #4 PushSS(2*popSS()+1); New Grammar View as dew type of non-terminal. When production #1 is applied, code Is invoked

Introduction to compilers James Larus Lecture 11- Syntax-Directed Translation Example, cont’d Make grammar LL(1) by eliminating left-factoring and left-recursion removal: B  #1 B  0#1 X  #2  1#2 X  BB` X  B’X B’  0#3  1#4 InputParse StackSemantic StackProduction 101 B B  1#2 x 101 1#2X 01 #2X (#2  ) 01 X1 X  B`X 01 B`X1 B`  0#3 01 0#3X1 1 #3X1 (#3  ) 1 X2 B`  0#3  1#4

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: Arithmetic Expressions E  E+T I T T  T * F I F F  int I (E) Convert rules to actions: #1 (Ttmp =popSS(); /* RHS Pushed left to right */ Etmp=popSS(); /* S0 pop, right-to-left */ PushSS (Etmp+Ttmp);) #2 nop /* PushSS (popSS()) */

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example,Cont’d: Make grammar LL(1) E  TE` E`  +T#1E`I T  FT` T`  *F#2T`I F  int#3 I(E)

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Recursive-Descent Parsing: Can use in recursive-descent parse by writing a procedure for each non-terminal * procedure E() T(); E`(); procedure E`() int Ttmp, Etmp; If next_token()=PLUS b then match(plus); T(); Ttmp=popSS(); Etmp=popSS(); PushSS(Etmp+Ttmp); E`();