Announcements. Working in pairs is only allowed for programming assignments and not for homework problems. H3 has been posted

Similar documents
Syntax-Directed Translation. Lecture 14

Principles of Programming Languages

HW2 posted. Announcements

Context-free grammars (CFGs)

CMSC 330: Organization of Programming Languages. Context Free Grammars

Last Time. What do we want? When do we want it? An AST. Now!

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Principles of Programming Languages

Syntax and Grammars 1 / 21

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

Defining syntax using CFGs

Code Genera*on for Control Flow Constructs

CS 230 Programming Languages

LECTURE 3. Compiler Phases

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

Compila(on /15a Lecture 6. Seman(c Analysis Noam Rinetzky

Compilers. Compiler Construction Tutorial The Front-end

Properties of Regular Expressions and Finite Automata

EDA180: Compiler Construc6on. More Top- Down Parsing Abstract Syntax Trees Görel Hedin Revised:

Related Course Objec6ves

Defining syntax using CFGs

Compila(on (Semester A, 2013/14)

CSCI 1260: Compilers and Program Analysis Steven Reiss Fall Lecture 4: Syntax Analysis I

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

Introduction to Syntax Analysis Recursive-Descent Parsing

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

CS 4120 Introduction to Compilers

CS 536 Midterm Exam Spring 2013

Programs as data Parsing cont d; first-order functional language, type checking

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Parsing II Top-down parsing. Comp 412

CS 314 Principles of Programming Languages

Syntactic Directed Translation

Optimizing Finite Automata

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

CMSC 330: Organization of Programming Languages

Semantic actions for declarations and expressions

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Semantic actions for declarations and expressions. Monday, September 28, 15

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

CMSC 330: Organization of Programming Languages

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing Techniques. AST Review. AST Data Structures. LL AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

ECE251 Midterm practice questions, Fall 2010

A programming language requires two major definitions A simple one pass compiler

CSCI Compiler Design

CMPT 755 Compilers. Anoop Sarkar.

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

CSE 401 Midterm Exam Sample Solution 2/11/15

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages

Revisit the example. Transformed DFA 10/1/16 A B C D E. Start

Introduction to Parsing. Lecture 8

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Compiler Optimisation

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CSE 401 Midterm Exam Sample Solution 11/4/11

Context-free grammars (CFG s)

Research opportuni/es with me

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Introduc)on. Tristan Naumann. Sahar Hasan. Steve Hehl. Brian Lu

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Syntax-Directed Translation Part II

CSE302: Compiler Design

CMSC 330: Organization of Programming Languages. Context Free Grammars

Ways to implement a language

Parsing II Top-down parsing. Comp 412

Semantics of programming languages

Undergraduate Compilers in a Day

COP4020 Spring 2011 Midterm Exam

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

In One Slide. Outline. LR Parsing. Table Construction

CMPT 379 Compilers. Parse trees

CS 230 Programming Languages

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

CSCI312 Principles of Programming Languages

CS606- compiler instruction Solved MCQS From Midterm Papers

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Semantic Analysis with Attribute Grammars Part 3

Bottom-Up Parsing. Lecture 11-12

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout. CS453 Shift-reduce Parsing 1

CSE302: Compiler Design

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Transcription:

Announcements Working in pairs is only allowed for programming assignments and not for homework problems H3 has been posted 1

Syntax Directed Transla@on 2

CFGs so Far CFGs for Language Defini&on 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 Recogni&on Start with a string and end with a parse tree for it 3

CFGs for Parsing Language Recogni@on isn t enough for a parser We also want to translate the sequence Parsing is a special case of Syntax- Directed Transla&on Translate a sequence of tokens o a sequence of ac@ons 4

Syntax Directed Transla@on Augment CFG rules with transla@on rules (at least 1 per produc@on) Define transla@on of LHS nonterminal as func@on of Constants RHS nonterminal transla@ons RHS terminal value Assign rules bopom- up 5

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

SDT Example 2: Declara@ons CFG Rules DList ε DList.trans = DList Decl DList.trans = Decl.trans + + DList 2.trans Decl Type id Decl.trans = id.value Type bool yy xx DList Transla4on is a String of ids Input string xx; bool yy; xx DList yy Decl DList xx Decl Type id ε Type id bool 7

Exercise Time Only add declara@ons of type to the output String. Augment the previous grammar: CFG Rules DList ε DList.trans = DList Decl DList.trans = Decl.trans + + DList 2.trans Decl Type id ; Decl.trans = id.value Type bool Different nonterms can have different types Rules can have condi4onals 8

SDT Example 2b: s only CFG Rules DList ε DList.trans = Decl DList DList.trans = Decl.trans + + DList 2.trans Decl Type id ; if (Type.trans) {Decl.trans = id.value} else {Decl.trans = } Type Type.trans = true bool Type.trans = false Input string xx; bool yy; xx DList xx DList Transla4on is a String of ids only Decl Different nonterms can have different types Rules can have condi4onals DList ε true xx Type Decl id false Type bool id 9

SDT for Parsing In the previous examples, the SDT process assigned different types to the transla@on: Example 1: tokenized stream to an eger value Example 2: tokenized stream to a (java) String For parsing, we ll go from tokens to an Abstract- Syntax Tree (AST) 10

Abstract Syntax Trees A condensed form of the parse tree Operators at ernal nodes (not leaves) Chains of produc@ons are collapsed Syntac@c details omiped Example: (5+2)*8 ( add Expr Term Term * Factor Factor Expr mult ) Parse Tree lit (8) (8) mult Expr + Term (5) add (2) (8) (5) Term Factor lit (5) Factor lit (2) (2) 11

Exercise #2 Show the AST for: (1 + 2) * (3 + 4) * 5 + 6 Expr - > Expr + Term Term Term - > Term * Factor Factor Factor - > lit ( Expr ) 12

AST for Parsing In previous slides we did our transla@on in two steps Structure the stream of tokens o a parse tree Use the parse tree to build an abstract syntax tree, throw away the parse tree In prac@ce, we will combine these o 1 step Ques@on: Why do we even need an AST? More of a logical view of the program Generally easier to work with 13

AST Implementa@on How do we actually represent an AST in code? We ll take inspira@on from how we represented tokens in JLex 14

ASTs in Code Note that we ve assumed a field- like structure in our SDT ac@ons: DList.trans = Decl.trans + + DList 2.trans In our parser, we ll define classes for each type of nonterminal, and create a new nonterminal in each rule. In the above rule we might represent DList as public class DList{ public String trans; } For ASTs: when we execute an SDT rule we construct a new node object for the RHS propagate its fields with the fields of the LHS nodes 15

Thinking about implemen@ng ASTs Consider the AST for a simple language of Expressions Input 1 + 2 Parse Tree Expr Tokeniza4on lit plus lit AST + 1 2 Naïve AST Implementa4on class PlusNode IntNode left; IntNode right; } Expr Term plus Term Factor class IntNode{ value; } Factor lit 1 lit 2 16

Thinking about implemen@ng ASTs Consider AST node classes We d like the classes to have a common inheritance tree AST + 1 2 Naïve AST Implementa4on class PlusNode { IntNode left; IntNode right; } Naïve java AST PlusNode IntNode lea: IntNode right: class IntNode { value; } IntNode value: IntNode 1 value: 2 17

Thinking about implemen@ng ASTs Consider AST node classes We d like the classes to have a common inheritance tree AST + 1 2 Naïve AST Implementa4on class PlusNode { IntNode left; IntNode right; } Beder java AST PlusNode ExpNode lea: ExpNode right: Make these extend ExpNode class IntNode { value; } IntNode value: IntNode 1 value: 2 18

Implemen@ng ASTs for Expressions CFG Expr - > Expr + Term Term Term - > Term * Factor Factor Factor - > lit ( Expr ) Transla4on 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(lit.value) Factor.trans = Expr.trans Example: 1 + 2 Expr Expr plus Term PlusNode ExpNode lea: ExpNode right: Term Factor Factor lit 1 lit 2 IntNode IntNode value: value: 1 2 19

An AST for a code snippet FuncBody void foo( x, y){ if (x == y){ return; } while ( x < y){ cout << hello ; x = x + 1; } } if while return == return < pr = x y x y hello x + x 1 20

Summary (1 of 2) Today we learned about Syntax- Directed Transla@on (SDT) Consumes a parse tree with ac@ons Ac@ons yield some result Abstract Syntax Trees (ASTs) The result of SDT for parsing in a compiler Some prac@cal examples of ASTs 21

Summary (2 of 2) Scanner Language abstrac4on: RegEx Output: Token Stream Tool: JLex Implementa4on: DFA walking via table Parser Language abstrac4on: CFG Output: AST by way of Parse Tree Tool: Java CUP Implementa4on:??? Next 4me Next week 22