Syntax-Directed Definition

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
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.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Chapter 2 A Simple Compiler
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Simple One-Pass Compiler
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
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.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Chapter 2: A Simple One Pass Compiler
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.
C HAPTER 2. A S IMPLE S YNTAX -D IRECTED T RANSLATOR DR. NIDJO SANDJOJO, M.Sc.
CSE 3302 Programming Languages
A Simple Syntax-Directed Translator
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Compiler Construction
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Parsing with Context Free Grammars
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
Syntax-Directed Translation Part I
CS 363 Comparative Programming Languages
4 (c) parsing.
3.2 Language and Grammar Left Factoring Unclear productions
Lexical and Syntax Analysis
Chapter 5. Syntax-Directed Translation
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
A Simple One-Pass Compiler to Generate Bytecode for the JVM
Recursive decent parsing
Chapter 2: A Simple One Pass Compiler
CSE 3302 Programming Languages
COP4020 Programming Languages
Chapter 2: A Simple One Pass Compiler
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Designing a Predictive Parser
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
Presentation transcript:

Syntax-Directed Definition Each Production Has a Set of Semantic Rules Each Grammar Symbol Has a Set of Attributes For the Following Example, String Attribute “t” is Associated With Each Grammar Symbol recall: a Derivation for 9 + 5 - 2? expr  expr – term | expr + term | term term  0 | 1 | 2 | 3 | … | 9 expr  expr - term  expr + term - term  term + term - term  9 + term - term  9 + 5 - term  9 + 5 - 2

Syntax-Directed Definition (2) Each Production Rule of the CFG Has a Semantic Rule Note: Semantic Rules for expr define t as a “synthesized attribute” i.e., the various copies of t obtain their values from “children t’s” Production Semantic Rule expr  expr + term expr.t := expr.t || term.t || ‘+’ expr  expr – term expr.t := expr.t || term.t || ’-’ expr  term expr.t := term.t term  0 term.t := ‘0’ term  1 term.t := ‘1’ …. …. term  9 term.t := ‘9’

Semantic Rules are Embedded in Parse Tree expr.t =95- expr.t =9 expr.t =95-2+ term.t =5 term.t =2 term.t =9 2 + 5 - 9 How Do Semantic Rules Work ? What Type of Tree Traversal is Being Performed? How Can We More Closely Associate Semantic Rules With Production Rules ? (“postorder depth-first”)

Translation Schemes Embed Semantic Actions into the right sides of the productions. expr  expr + term {print(‘+’)}  expr - term {print(‘-’)}  term term  0 {print(‘0’)} term  1 {print(‘1’)} … term  9 {print(‘9’)} term expr 9 5 2 - + {print(‘-’)} {print(‘9’)} {print(‘5’)} {print(‘2’)} {print(‘+’)}

Parsing – Top-Down & Predictive Top-Down Parsing  Parse tree / derivation of a token string occurs in a top down fashion. For Example, Consider: Start symbol type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Suppose input is : array [ num dotdot num ] of integer Parsing would begin with type  ???

Top-Down Parse (type = start symbol) Lookahead symbol Input : array [ num dotdot num ] of integer type ? type ] simple of [ array type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Start symbol Lookahead symbol Input : array [ num dotdot num ] of integer type ] simple of [ array num dotdot

Top-Down Parse (type = start symbol) Lookahead symbol Input : array [ num dotdot num ] of integer type ] simple of [ array num dotdot type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Start symbol type ] simple of [ array num dotdot integer

Top-Down Process Recursive Descent or Predictive Parsing Parser Operates by Attempting to Match Tokens in the Input Stream Utilize both Grammar and Input Below to Motivate Code for Algorithm array [ num dotdot num ] of integer type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num procedure match ( t : token ) ; begin if lookahead = t then lookahead : = nexttoken else error end ;

Top-down algorithm (continued) procedure simple ; begin if lookahead = integer then match ( integer ); else if lookahead = char then match ( char ); else if lookahead = num then begin match (num); match (dotdot); match (num) end else error end ; type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num

Top-Down Algorithm (Continued) procedure type ; begin if lookahead is in { integer, char, num } then simple else if lookahead = ‘’ then begin match (‘’ ) ; match( id ) end else if lookahead = array then begin match( array ); match(‘[‘); simple; match(‘]’); match(of); type end else error end ; type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num

Tracing type  simple |  id | array [ simple ] of type simple  integer | char | num dotdot num Input: array [ num dotdot num ] of integer To initialize the parser: set global variable : lookahead = array call procedure: type Procedure call to type with lookahead = array results in the actions: match( array ); match(‘[‘); simple; match(‘]’); match(of); type Procedure call to simple with lookahead = num results in the actions: match (num); match (dotdot); match (num) Procedure call to type with lookahead = integer results in the actions: simple Procedure call to simple with lookahead = integer results in the actions: match ( integer )

Limitations Can we apply the previous technique to every grammar? NO: type  simple | array [ simple ] of type simple  integer | array digit digit  0|1|2|3|4|5|6|7|8|9 consider the string “array 6” the predictive parser starts with type and lookahead= array apply production type  simple OR type  array digit ??