CMPT 379 Compilers. Parse trees

Similar documents
Intermediate Representation

A Simple Syntax-Directed Translator

Semantic actions for declarations and expressions

Semantic actions for expressions

ASTs, Objective CAML, and Ocamlyacc

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

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Chapter 2 A Quick Tour

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Introduction to Parsing. Lecture 8

Semantic actions for declarations and expressions

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

Building Compilers with Phoenix

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

LECTURE 3. Compiler Phases

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Chapter 4. Abstract Syntax

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CS415 Compilers. Intermediate Represeation & Code Generation

Project Compiler. CS031 TA Help Session November 28, 2011

Error Handling Syntax-Directed Translation Recursive Descent Parsing. Lecture 6

Introduction to Compiler

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Intermediate Code Generation

CMPT 755 Compilers. Anoop Sarkar.

Topic 3: Syntax Analysis I

Context-Free Grammars

Introduction to Parsing Ambiguity and Syntax Errors

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Syntax-Directed Translation. Lecture 14

CSE 401 Midterm Exam Sample Solution 11/4/11

Principles of Programming Languages COMP251: Syntax and Grammars

Introduction to Parsing Ambiguity and Syntax Errors

Context-Free Grammars

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

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

Semantic Analysis. Role of Semantic Analysis

Introduction to Parsing. Lecture 5

CS 314 Principles of Programming Languages

Abstract Syntax Trees & Top-Down Parsing

CMSC 330: Organization of Programming Languages

Context-sensitive Analysis

Abstract Syntax Trees

A simple syntax-directed

Implementing Actions

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS 403: Scanning and Parsing

Introduction to Parsing. Lecture 5

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

Chapter 4 - Semantic Analysis. June 2, 2015

CSE 401 Midterm Exam Sample Solution 2/11/15

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 4 :: Semantic Analysis

ECE251 Midterm practice questions, Fall 2010

Syntax Analysis Part I

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

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

LL(k) Compiler Construction. Top-down Parsing. LL(1) parsing engine. LL engine ID, $ S 0 E 1 T 2 3

Context-free grammars (CFG s)

CS5363 Final Review. cs5363 1

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

CSE P 501 Exam 11/17/05 Sample Solution

Alternatives for semantic processing

CS 415 Midterm Exam Spring 2002

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

Error Handling Syntax-Directed Translation Recursive Descent Parsing

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

Semantic Analysis. Lecture 9. February 7, 2018

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Summary: Semantic Analysis

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

CSE 12 Abstract Syntax Trees

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

The role of semantic analysis in a compiler

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Defining syntax using CFGs

Compiling Techniques

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

Context-sensitive Analysis

CS502: Compilers & Programming Systems

Compiler Optimisation

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

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

Context-free grammars

LL(k) Compiler Construction. Choice points in EBNF grammar. Left recursive grammar

Undergraduate Compilers in a Day

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

Cool Abstract Syntax Trees

Functions & First Class Function Values

Time : 1 Hour Max Marks : 30

Transcription:

CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 10/25/07 1 Parse trees Given an input program, we convert the text into a parse tree Moving to the backend of the compiler: we will produce intermediate code from the parse tree This process is called syntax directed translation because we are using a CFG Parser output is a concrete syntax tree 10/25/07 2 1

Intermediate Representations A parse tree is an example of a very high level intermediate representation We can reconstruct the original source code from the concrete syntax tree Typically we want to check some semantic rules on the parse tree and report any errors The next step: semantic processing and code generation 10/25/07 3 Abstract Syntax Trees Take the concrete syntax tree and simplify it to the essential nodes For example, if the parser used an LL(1) grammar then the concrete syntax tree will have extra non-terminals Elimination of left-recursion, changing the grammar to remove shift/reduce conflicts 10/25/07 4 2

Abstract Syntax Trees Assume we have a top-down parser, e.g. an LL(1) parser. We have to eliminate left-recursion to use the parser E E + T T Becomes E T E 1 and E 1 + T E 1 ε For future steps, the AST might convert back into a tree that is compatible with the original grammar (before left-recursion elimination) 10/25/07 5 Abstract Syntax Trees Another example is the use of built-in functions, userdefined functions and operators In each case we have to call some code with a number of parameters Each case might have a separate syntax with different punctuation marks, e.g. () ; Punctuation marks are useful in language design but not useful when presenting a uniform tree for future analysis and code generation In an AST, all of these cases can be converted to a single tree format 10/25/07 6 3

Abstract Syntax Trees Other examples include lists of various kinds that involves recursion in CFGs: Program Function-List Function-List Function-Defn Function_List Function-Defn The extra nodes created due to these grammar changes are not useful The extra nodes might make things non-local (inconvenient) for the semantic processing and code generation 10/25/07 7 Abstract Syntax Trees Process the concrete syntax tree and convert into a tree that is useful for semantic processing and code generation Note that ambiguity is no longer a problem: we already have the parse tree Abstract syntax trees will typically have pointers to children and pointers to parent nodes 10/25/07 8 4

Example Consider the following fragment of a programming language grammar: Program Function-List Function-List Function-Defn Function-List Function-Defn Function-Defn fun id ( Param-List ) Body Body { Statement-List } 10/25/07 9 Example (cont d) Consider an example program: fun main () { statement } fun foo (int n) { n = n + 1 } 10/25/07 10 5

Concrete Parse Tree Program Function-List Function-Defn Function-List Function-Defn fun id ( params ) Body { } main ε fun id ( params ) Body foo param { assign } n expr int id = 10/25/07 11 n n op 1 Abstract Parse Tree Function-List Function Id: main Function Id: foo Other functions Subtree for params Subtree for body Subtree for body Subtree for assign Function Id: + Subtree for params 10/25/07 12 6

Code generation as Translation Code generation can be viewed as translation from the parse tree In other words, an alignment between the source code and the assembly code Typically we go to an intermediate representation and then to assembly Let s consider a simple case where the IR step can be skipped 10/25/07 13 concrete syntax tree + a * 10/25/07 14 b c 7

abstract parse tree + a * b c 10/25/07 15 Code generation GenerateCode(tree t, int resultregister) Recursively traverse the abstract syntax tree At each node produce the code needed for that binary operation based on the results from the recursive call results 10/25/07 16 8

Trace of code generation GenerateCode(+, 0) GenerateCode(a, 0) Write LOAD a, R0 GenerateCode(*, 1) GenerateCode(b, 1) Write LOAD b, R1 GenerateCode(c, 2) Write LOAD c, R2 Write MUL R1, R2 Write ADD R0, R1 10/25/07 17 Result of code generation The resulting assembly code: LOAD a, R0 LOAD b, R1 LOAD c, R2 MUL R1, R2 ADD R0, R1 Note that using the tree structure means that the registers do not conflict Later we will consider the optimal assignment of values to registers 10/25/07 18 9

Case Study: Lisp The term abstract syntax was coined by John McCarthy McCarthy designed Lisp which directly used an abstract syntax bypassing the concrete syntax step Structure of Lisp: (function arg-list) Directly represents the parse tree in syntax Lisp: Lots of Irritating Silly Parentheses 10/25/07 19 Directed Acyclic Graphs b*c+b*c + * * b c b c 10/25/07 20 10

Directed Acyclic Graphs + * b 10/25/07 21 c Summary The parser produces concrete syntax trees Abstract syntax trees: abstract away from any grammar transformations or remove unnecessary punctuation Tree is input for code generation Ad-hoc code generation from ASTs As before, we would like to formally specify translation from AST to assembly/machine code ASTs can also be the basis for semantic analysis 10/25/07 22 11