Syntax-Directed Translation

Slides:



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

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Context-Free Grammars Lecture 7
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.
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
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
MiniJava Compiler A multi-back-end JIT compiler of Java.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Lecture 9 Symbol Table and Attributed Grammars
Announcements/Reading
Chapter 3 – Describing Syntax
ASTs, Grammars, Parsing, Tree traversals
CUP: An LALR Parser Generator for Java
A Simple Syntax-Directed Translator
Constructing Precedence Table
CS 3304 Comparative Languages
Introduction to Parsing
CS510 Compiler Lecture 4.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Introduction to Parsing (adapted from CS 164 at Berkeley)
Semantic Analysis with Emphasis on Name Analysis
Java CUP.
Context-free grammars (CFGs)
Syntax versus Semantics
Syntax-Directed Translation Part I
Emily Leland (Not Nick) Spring 2017
CPSC 388 – Compiler Design and Construction
Top-Down Parsing.
Basic Program Analysis: AST
Syntax Analysis Sections :.
Compiler Design 4. Language Grammars
Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Preparing for MUPL! Justin Harjanto
Chapter 2: A Simple One Pass Compiler
ADTs, Grammars, Parsing, Tree traversals
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Adapted from slides by Nicholas Shahan and Dan Grossman
Syntax-Directed Translation Part I
CSC 4181 Compiler Construction Context-Free Grammars
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Nicholas Shahan Spring 2016
The Recursive Descent Algorithm
Topic 2: Compiler Front-End
Life is Full of Alternatives
Syntax-Directed Translation Part I
Faculty of Computer Science and Information System
Presentation transcript:

Syntax-Directed Translation

abstract-syntax tree rather than a parse tree CFGs so Far CFGs for Language Definition The CFGs we’ve discussed can generate/define languages of valid strings So far, we start by building a parse tree and end with some valid string CFGs for Language Recognition Start with a string 𝑤, and end with yes/no depending on whether 𝑤∈𝐿(𝐺) CFGs in a compiler Start with a string 𝑤, and end with a parse tree for 𝑤 if 𝑤∈𝐿(𝐺) Generally an abstract-syntax tree rather than a parse tree

CFGs for Parsing Language Recognition isn’t enough for a parser We also want to translate the sequence Parsing is a special case of Syntax-Directed Translation Translate a sequence of tokens into a sequence of actions

Syntax-Directed Translation (SDT) Augment CFG rules with translation rules (at least 1 per production) Define translation of LHS nonterminal as function of Constants RHS nonterminal translations RHS terminal value Assign rules bottom-up

Translation is the value of the input SDT Example CFG Rules B -> 0 B.trans = 0 | 1 B.trans = 1 | B 0 B.trans = B2.trans * 2 | B 1 B.trans = B2.trans * 2 + 1 Input string 10110 22 B 11 B Translation is the value of the input 5 B 1 2 B 1 1 B 1

SDT Example 2: Declarations Translation is a String of ids CFG Rules DList → ε DList.trans = “” | DList Decl DList.trans = DList2.trans + “ “ + Decl.trans Decl → Type id ; Decl.trans = id.value Type → int | bool “ xx yy” DList Input string int xx; bool yy; “ xx” “yy” DList Decl Type id “” “xx” DList Decl bool Type id ε int

Exercise Time Only add declarations of type int to the output String. Augment the previous grammar: CFG Rules DList → ε DList.trans = “” | Decl DList DList.trans = DList2.trans + “ “ + Decl.trans Decl → Type id ; Decl.trans = id.value Type → int | bool Different nonterms can have different types Rules can have conditionals

SDT Example 2b: ints only Translation is a String of int ids only CFG Rules DList → ε DList.trans = “” | Decl DList DList.trans = DList2.trans + “ “ + Decl.trans Decl → Type id ; Decl.trans = (Type.trans ? id.value : “”) Type → int Type.trans = true | bool Type.trans = false “ xx” DList Input string int xx; bool yy; “ xx” “” DList Decl false Type id “” “xx” Different nonterms can have different types DList Decl true bool Type id ε Rules can use conditional expressions int

SDT for Parsing In the previous examples, the SDT process assigned different types to the translation: Example 1: tokenized stream to an integer value Example 2: tokenized stream to a (Java) String For parsing, we’ll go from tokens to an Abstract-Syntax Tree (AST)

Abstract Syntax Trees A condensed form of the parse tree Operators at internal nodes (not leaves) Chains of productions are collapsed Syntactic details omitted + intlit (2) Expr Term * Factor intlit (8) intlit (5) ( ) mult int (8) add Example: (5+2)*8 int (5) add (2) mult (8) int (2) int (5)

Exercise #2 Show the AST for: (1 + 2) * (3 + 4) * 5 + 6 Expr -> Expr + Term | Term Term -> Term * Factor | Factor Factor -> intlit | ( Expr ) Show the AST for: (1 + 2) * (3 + 4) * 5 + 6 Expr -> Expr + Term Expr1.trans = MkPlusNode(Expr2.trans, Term.trans)

AST for Parsing In previous slides we did the translation in two steps Structure the stream of tokens into a parse tree Use the parse tree to build an abstract-syntax tree; then throw away the parse tree In practice, we will combine these into one step Question: Why do we even need an AST? More of a “logical” view of the program: the essential structure Generally easier to work with an AST (in the later phases of name analysis and type checking) no cascades of exp → term → factor → intlit, which was introduced to capture precedence and associativity

AST Implementation How do we actually represent an AST in code?

ASTs in Code Note that we’ve assumed a field-like structure in our SDT actions: Expr -> Expr + Term Expr1.trans = MkPlusNode(Expr2.trans, Term.trans) In our parser, we’ll define a class for each kind of ADT node, and create a new node object in some rules In the above rule we would represent the Expr1.trans value via the class For ASTs: when we execute an SDT rule we construct a new node object, which becomes the value of LHS.trans populate the node’s fields with the translations of the RHS nonterminals public class PlusNode extends ExpNode { public ExpNode left; public ExpNode right; }

How to implement ASTs Consider the AST for a simple language of Expressions Input 1 + 2 Tokenization intlit plus intlit AST Naïve AST Implementation + class PlusNode IntNode left; IntNode right; } Parse Tree 1 2 Expr Expr plus Term class IntNode{ int value; } Term Factor Factor intlit 2 intlit 1

How to implement ASTs Consider AST node classes We’d like the classes to have a common inheritance tree Naïve AST Implementation Naïve Java AST AST + class PlusNode { IntNode left; IntNode right; } PlusNode IntNode left: IntNode right: 1 2 class IntNode { int value; } IntNode int value: IntNode int value: 1 2

How to implement ASTs Consider AST node classes We’d like the classes to have a common inheritance tree Naïve AST Implementation Better Java AST AST + class PlusNode { IntNode left; IntNode right; } PlusNode ExpNode left: ExpNode right: 1 2 class IntNode { int value; } IntNode int value: IntNode int value: 1 2 Make these extend ExpNode Make these fields be of class ExpNode

Implementing ASTs for Expressions CFG Expr -> Expr + Term | Term Term -> Term * Factor | Factor Factor -> intlit | ( Expr ) Translation Rules Expr1.trans = new PlusNode(Expr2.trans, Term.trans) Expr.trans = Term.trans Term1.trans = new TimesNode(Term2.trans, Factor.trans) Term.trans = Factor.trans Factor.trans = new IntNode(intlit.value) Factor.trans = Expr.trans Example: 1 + 2 Expr PlusNode ExpNode left: ExpNode right: Expr plus Term Term Factor Factor intlit 2 IntNode value: IntNode value: 2 intlit 1 1

An AST for an code snippet void foo(int x, int y){ if (x == y){ return; } while ( x < y){ cout << “hello”; x = x + 1; FuncBody if while return == return < print = x y x y “hello” x + x 1

Summary (1 of 2) Today we learned about Syntax-Directed Translation (SDT) Consumes a parse tree with actions Actions yield some result Abstract Syntax Trees (ASTs) The result of an SDT performed during parsing in a compiler Some practical examples of ASTs

Summary (2 of 2) Language abstraction: RegExp Output: Token Stream Tool: JLex Implementation: Interpret DFA using table (for 𝛿), recording most_recent_accepted_position and most_recent_token Scanner Language abstraction: CFG Output: AST by way of a syntax-directed translation Tool: Java CUP Implementation: ??? Parser Next week Next week