CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Abstract Syntax Trees Compiler Baojian Hua
Context-Free Grammars Lecture 7
Chapter 2 A Simple Compiler
Chapter 2 A Simple Compiler
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
CPSC 388 – Compiler Design and Construction
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
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.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Prof. Bodik CS 164 Lecture 51 Building a Parser I CS164 3:30-5:00 TT 10 Evans.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
Abstract Syntax Trees Compiler Baojian Hua
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter 3 Describing Syntax and Semantics
LESSON 04.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Programming Languages 2nd edition Tucker and Noonan
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing
Parsing and Parser Parsing methods: top-down & bottom-up
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Java CUP.
Context-Free Grammars
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation
Context-Free Grammars
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Syntax-Directed Translation
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Operator precedence and AST’s
Operator Precedence and Associativity
Context-Free Grammars
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation

Syntax Directed Translation  Translating from a sequence of tokens to some other form, based on the underlying syntax.  augment the CFG: a translation rule is defined for each production. A translation rule defines the translation of the left-hand side nonterminal as a function of: constants the right-hand-side nonterminals' translations the right-hand-side tokens' values (e.g., the integer value associated with an INTLIT token, or the String value associated with an ID token)

Translating Strings 1.Build the parse tree. 2.Use the translation rules to compute the translation of each nonterminal in the tree, working bottom up (since a nonterminal's value may depend on the value of the symbols on the right-hand side, you need to work bottom-up so that those values are available). 3.The translation of the root node is the translation of the string

Build a Parse Tree Build a parse tree for the input: 2*4+5 exp → exp PLUS term exp → exp MINUS term exp → term term → term TIMES factor term → term DIVIDE factor term → factor factor → LPAREN exp RPAREN factor → INT_LIT exp1.trans = exp2.trans + term.trans exp1.trans = exp2.trans - term.trans exp.trans = term.trans term1.trans = term2.trans * factor.trans term1.trans = term2.trans / factor.trans term.trans = factor.trans factor.trans = exp.trans factor.trans = INT_LIT.value

Annotated Parse Tree  Parse Tree can be annotated using the translation rules associated with each production  Use translation rule for root node and recurse into sub-nodes as the translation rule dictates

Example Annotated Parse Tree Consider the following CFG, which defines expressions that use the three operators: +, &&, ==. Let's define a syntax-directed translation that type checks these expressions; i.e., for type-correct expressions, the translation will be the type of the expression (either INT or BOOL), and for expressions that involve type errors, the translation will be the special value ERROR. We'll use the following type rules: 1.Both operands of the + operator must be of type INT. 2.Both operands of the && operator must be of type BOOL. 3.Both operands of the == operator have the same (non-ERROR) type.

Example Annotated Parse Tree CFGTranslation rules === ================= exp -> exp + termif ((exp2.trans == INT) and (term.trans == INT) then exp1.trans = INT else exp1.trans = ERROR exp -> exp && termif ((exp2.trans == BOOL) and (term.trans == BOOL) then exp1.trans = BOOL else exp1.trans = ERROR exp -> exp == termif ((exp2.trans == term.trans) and (exp2.trans != ERROR)) then exp1.trans = BOOL else exp1.trans = ERROR exp -> termexp.trans = term.trans term -> trueterm.trans = BOOL term -> falseterm.trans = BOOL term -> intliteralterm.trans = INT term -> ( exp )term.trans = exp.trans Try Input: ( ) == 4 Construct Annotated Parse Tree

Create Translation Rules  The following grammar defines the language of base- 2 numbers: b -> 0 b -> 1 b -> b 0 b -> b 1  Define a syntax-directed translation so that the translation of a binary number is its base 10 value. Illustrate your translation scheme by drawing the parse tree for 1001 and annotating each nonterminal in the tree with its translation.

Building AST from Parse Trees  So far, our example syntax-directed translations have produced simple values (an int or a type) as the translation of an input. In practice however, we want the parser to build an abstract-syntax tree as the translation of an input program.  But that is not really so different from what we've seen so far; we just need to use tree-building operations in the translation rules instead of, e.g., arithmetic operations.

Diffferences between AST and Parse Tree  Operators appear at internal nodes instead of at leaves.  "Chains" of single productions are collapsed.  Lists are "flattened".  Syntactic details (e.g., parentheses, commas, semi-colons) are omitted.  In General ASTs omit details having to do with the source language, and just contains information about the essential structure of the program.

Example Abstract Syntax Tree * Construct Parse Tree for 3*(4+2) For constructs other than expressions, the compiler writer has some choices when defining the AST -- but remember that lists (e.g., lists of declarations lists of statements, lists of parameters) should be flattened, that operators (e.g., "assign", "while", "if") go at internal nodes, not at leaves, and that syntactic details are omitted.

Translation Rules for ASTs First need some java classes for nodes in AST class ExpNode { } class IntLitNode extends ExpNode { public IntLitNode(int val) {...} } class PlusNode extends ExpNode { public PlusNode( ExpNode e1, ExpNode e2 ) {... } } class TimesNode extends ExpNode { public TimesNode( ExpNode e1, ExpNode e2 ) {... } }

Translation Rules for ASTs CFGTranslation rules ==================== exp -> exp + termexp1.trans = new PlusNode(exp2.trans, term.trans) exp -> termexp.trans = term.trans term -> term * factorterm1.trans = new TimesNode(term2.trans, factor.trans) term -> factorterm.trans = factor.trans factor -> INTLITERALfactor.trans = new IntLitNode(INTLITERAL.value) factor -> ( exp )factor.trans = exp.trans Add to this CFG and Translation Rules for minus and divide Draw the Parse Tree for 2+3*4 and annotate with translation