JJTree. The Compilation Task. Automated? JJTree. An easier way to create an Abstract Syntax Tree

Similar documents
Automated Tools. The Compilation Task. Automated? Automated? Easier ways to create parsers. The final stages of compilation are language dependant

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Abstract Syntax Trees

Definition of AST AST and Static Code Analysis Design Patterns. AST Features Applicability of AST JTB JJTree

Compiler Compiler Tutorial

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

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 2016

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

Semantic actions for expressions

Semantic actions for declarations and expressions

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

Compilers. Compiler Construction Tutorial The Front-end

Syntax-Directed Translation. Lecture 14

Compiler construction lecture 3

Syntax-Directed Translation. Introduction

Project Compiler. CS031 TA Help Session November 28, 2011

CPS2000 Compiler Theory & Practice

Dara Hazeghi 12/22/11 COMS 4115 Fall 2011

Semantic actions for declarations and expressions

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

Tree visitors. Context classes. CS664 Compiler Theory and Design LIU 1 of 11. Christopher League* 24 February 2016

CS 406: Syntax Directed Translation

CA Compiler Construction

Syntax and Grammars 1 / 21

Project 3 - Parsing. Misc Details. Parser.java Recursive Descent Parser... Using Lexer.java

Introduction to Programming Using Java (98-388)

Question Points Score

PL Revision overview

Chapter 4. Abstract Syntax

22c:111 Programming Language Concepts. Fall Syntax III

.jj file with actions

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G.

Intermediate Code Generation

Program Representations

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Undergraduate Compilers in a Day

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

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Parsing CSCI-400. Principles of Programming Languages.

CSE 12 Abstract Syntax Trees

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Semantic Analysis. Compiler Architecture

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

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

ASTs, Objective CAML, and Ocamlyacc

Grammars and Parsing, second week

Repeating patterns of boilerplate code (like parent/child navigation, Visitor pattern implementation, factory methods for parser tree construction)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Introduction to Lexing and Parsing

Objectives. Introduce static keyword examine syntax describe common uses

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

Prelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

COMP3131/9102: Programming Languages and Compilers

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSE P 501 Exam 8/5/04

Topic 14. The BinaryTree ADT

CS 536 Midterm Exam Spring 2013

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

Syntax Intro and Overview. Syntax

Project 1: Scheme Pretty-Printer

Syntax Errors; Static Semantics

CSCE 314 Programming Languages. Type System

Using an LALR(1) Parser Generator

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

Yacc: A Syntactic Analysers Generator

LECTURE 3. Compiler Phases

EECS 311: Data Structures and Data Management Program 1 Assigned: 10/21/10 Checkpoint: 11/2/10; Due: 11/9/10

MPLc Documentation. Tomi Karlstedt & Jari-Matti Mäkelä

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Lecture 14 Sections Mon, Mar 2, 2009

Lexical and Syntax Analysis

The SPL Programming Language Reference Manual

a. It will output It s NOT Rover b. Class Main should be changed to the following (bold characters show the changes)

Compiler construction in4303 lecture 3

CS 6353 Compiler Construction Project Assignments

CS453 Visitor patterns Type checking and Code Generation

Chapter 4 :: Semantic Analysis

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

Modules, Structs, Hashes, and Operational Semantics

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Abstract Syntax Trees

Inheritance. Transitivity

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Building Compilers with Phoenix

The Visitor Pattern. Design Patterns In Java Bob Tarr

Transcription:

JJTree An easier way to create an Abstract Syntax Tree The Compilation Task Input character stream Lexer Token stream Parser Abstract Syntax Tree Analyser Annotated AST Code Generator Code CC&P 2003 1 CC&P 2003 2 Automated? JJTree The initial stages of compilation are very mechanistic Lexer Text to stream of tokens Pattern matching Parser Application of grammar rules Recovery from errors AST Generation Data structure for subsequent stages Preprocessor to JavaCC Generates.jj file that includes code to build an AST JJTree generates code to construct parse tree nodes for each noninal in the language. This behaviour can be modified some noninals do not have nodes generated, or a node is generated for a part of a production's expansion CC&P 2003 3 CC&P 2003 4

JJTree Bottom up AST Generation JJTree constructs the parse tree from the bottom up Uses a stack to push created nodes When a parent node is created it pops the children from the stack adds them to the parent and pushes the new parent node on the stack The stack can be manipulated by user code -2+p("ok",3/4)-4; void () : [()] () ( () () )* CC&P 2003 5 CC&P 2003 6 Bottom up AST Generation Modes -2+p("ok",3/4)-4; void () : [()] () ( () () )* Stack Children of CC&P 2003 7 Simple each parse tree node is an instance of SimpleNode Multi in multi mode the type of the parse tree node is derived from the name of the node. If you don't provide an implementation of a node class JJTree will generate a sample implementation. CC&P 2003 8

Example 1 Only modify initial production from void start() : () ";" to SimpleNode start() : () ";" return jjtthis; Example 1 Generates the following AST where all nodes are instances of SimpleNode, eg. -2+p("ok",3/4)-4 start A literal number ident A literal CC&P 2003 9 CC&P 2003 10 Creating Nodes Creating Nodes A definite node constructed with a specific number of children. That many nodes are popped from the stack and made the children of the new node, which is then pushed on the stack itself. #ADefiniteNode(INTEGER EXPRESSION) A conditional node is constructed with all of the children on the stack if its condition evaluates to true. If it evaluates to false, the node is not constructed. #ConditionalNode(BOOLEAN EXPRESSION) CC&P 2003 11 CC&P 2003 12

Creating Nodes Example 2 By default JJTree treats each noninal as an indefinite node and derives the name of the node from the name of its production. You can give it a different name with the following syntax: void P1() #MyNode :...... CC&P 2003 13 Create specific node classes for literals, eg. ident public class ASTident extends SimpleNode private String text; public ASTident(int id) super(id); public ASTident(Expression p, int id) super(p, id); public void settext(string n) text = n; public String tostring() return "ident: " + text; Instance variable (ident text) Standard Constructors Accessor method To complete the job CC&P 2003 14 Example 2 Example 2 Modify.jjt file to create/allow nodes for each non inal production options MULTI=true; And to capture literals void ident() : Token t; Implicitly refers to specific active node t=<identifier> (ASTident) jjtthis.settext(t.image); CC&P 2003 15 Generates the following AST where all nodes are instances of Unique classes extending SimpleNode, eg.- 2+p("ok",3/4)-4 start number: 2 ident: p rvalue string: "ok" rvalue number: 3 multop CC&P 2003 16

Example 3 Example 3 Generates a "useful" AST where all nodes are instances of unique classes extending SimpleNode, eg. -2+p("ok",3/4)-4 start MinusNode AddNode MinusNode number: 2 ident: p parameterlist string: "ok" DivideNode number: 3 2 - + - P PL 4 "ok" / 3 4 CC&P 2003 17 The task is to only generate nodes where appropriate and generate an AST with a suitable shape. Remove superfluous inediate nodes void rvalue() #void : () string() Does not generate a node CC&P 2003 18 Example 3 Generate only appropriate nodes void () #void : "+" #AddNode "-" #MinusNode Does not generate an node But does generate either an AddNode or MinusNode CC&P 2003 19 Example 3 Reorder nodes void () #void : Node unaryop = null; [() unaryop = jjtree.popnode();] () if (unaryop!= null) unaryop.jjtaddchild(jjtree.popnode(),0); jjtree.pushnode(unaryop); ( () () Node n1,n2,op; n2 = jjtree.popnode(); op = jjtree.popnode(); n1 = jjtree.popnode(); op.jjtaddchild(n1,0); op.jjtaddchild(n2,1); jjtree.pushnode(op); )* Restructure tree with a binary operator having exactly 2 children If a unary operation exists make the following a child of it CC&P 2003 20

More features of JJTree Visitor design pattern support JJTree can insert an method into all of the node classes it generates, and also generate a visitor interface Questions How would a pretty printer be implemented using the visitor design pattern generation of JJTree? CC&P 2003 21 CC&P 2003 22