Using Java Tree Builder Professor Yihjia Tsai Tamkang University.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
The Visitor Design Pattern and Java Tree Builder
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Guide To UNIX Using Linux Third Edition
The Visitor Design Pattern and Java Tree Builder
C++ fundamentals.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Design Patterns Part 3 (Slides by Mike Ernst and David Notkin) 1.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Group 4 Java Compiler Group Members: Atul Singh(Y6127) Manish Agrawal(Y6241) Mayank Sachan(Y6253) Sudeept Sinha(Y6483)
Recursive data structures noter ch.3 Modeling recursive structure by class hierarchy Recursive traversal of structure.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
11/25/2015© Hal Perkins & UW CSEH-1 CSE P 501 – Compilers Implementing ASTs (in Java) Hal Perkins Winter 2008.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
The Visitor Design Pattern and Java Tree Builder
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture 9 Symbol Table and Attributed Grammars
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
The need for Programming Languages
A Simple Syntax-Directed Translator
Data Structures & Algorithms
Classes, Objects and Methods
Presentation transcript:

Using Java Tree Builder Professor Yihjia Tsai Tamkang University

2 Lecture Outline Introduction Syntax Directed Translation Java Virtual Machine Examples Administration

3 Introduction The Java Tree Builder (JTB) is a tool used to automatically generate syntax trees with the Java Compiler Compiler (JavaCC) parser generator. Java Compiler Compiler It ’ s based on the Visitor design pattern please see the section entitled Why Visitors?

4 The Visitor pattern is one among many design patterns aimed at making object- oriented systems more flexible The issue addressed by the Visitor pattern is the manipulation of composite objects. Without visitors, such manipulation runs into several problems as illustrated by considering an implementation of integer lists, written in Java

5 Integer lists, written in Java (Without Generics) interface List {} class Nil implements List {} class Cons implements List { int head; List tail; } What happens when we write a program which computes the sum of all components of a given List object?

6 First Attempt: Instanceof and Type Casts List l; // The List -object we are working on. int sum = 0; // Contains the sum after the loop. boolean proceed = true; while (proceed) { if (l instanceof Nil) proceed = false; else if (l instanceof Cons) { sum = sum + ((Cons) l).head; // Type cast! l = ((Cons) l).tail; // Type cast! } What are the problems here?

7 What are the problems here? Type Casts? –We want static (compile time) type checking Flexible? –Probably not well illustrated with this example

8 Second Attempt: Dedicated Methods interface List { int sum(); } class Nil implements List { public int sum() { return 0; } } class Cons implements List { int head; List tail; public int sum() { return head + tail.sum(); }

9 Tradeoffs Can compute the sum of all components of a given List-object l by writing l.sum(). Advantage: type casts and instanceof operations have disappeared, and that the code can be written in a systematic way. Disadvantage: Every time we want to perform a new operation on List-objects, say, compute the product of all integer parts, then new dedicated methods have to be written for all the classes, and the classes must be recompiled

10 Third Attempt: The Visitor Pattern. interface List { void accept(Visitor v); } class Nil implements List { public void accept(Visitor v) { v.visitNil(this); } } class Cons implements List { int head; List tail; public void accept(Visitor v) { v.visitCons(this); } }

11 Second Part of Visitor Idea interface Visitor { void visitNil(Nil x); void visitCons(Cons x); } class SumVisitor implements Visitor { int sum = 0; public void visitNil(Nil x) {} public void visitCons(Cons x){ sum = sum + x.head; x.tail.accept(this); } }

12 Summary Each accept method takes a visitor as argument. The interface Visitor has a header for each of the basic classes. We can now compute and print the sum of all components of a given List-object l by writing SumVisitor sv = new SumVisitor(); l.accept(sv); System.out.println(sv.sum);

13 Summary Continued The advantage is that one can write code that manipulates objects of existing classes without recompiling those classes. The price is that all objects must have an accept method. In summary, the Visitor pattern combines the advantages of the two other approaches

14 Summary Table Frequent type casts? Frequent recompilation? Instanceof and type casts YesNo Dedicated methodsNoYes The Visitor patternNo

15 Overview of Generated Files To begin using JTB, simply run it using your grammar file as an argument –Run JTB without any argumentsfor list. This will generate an augmented grammar file, as well as the needed classes

16 Details jtb.out.jj, the original grammar file, now with syntax tree building actions inserted The subdirectory/package syntaxtree which contains a java class for each production in the grammar The subdirectory/package visitor which contains Visitor.java, the default visitor interface, also –DepthFirstVisitor.java, a default implementation which visits each node of the tree in depth-first order. ObjectVisitor.java, another default visitor interface that supports return value and argument. –ObjectDepthFirst.java is a defualt implemetation of ObjectVisitor.java.

17 General Instructions To generate your parser, simply run JavaCC using jtb.out.jj as the grammar file. Let's take a look at all the files and directories JTB generates.

18 The grammar file Named jtb.out.jj This file is the same as the input grammar file except that it now contains code for building the syntax tree during parse. Typically, this file can be left alone after generation. The only thing that needs to be done to it is to run it through JavaCC to generate your parser

19 The syntax tree node classes This directory contains syntax tree node classes generated based on the productions in your JavaCC grammar. Each production will have its own class. If your grammar contains 42 productions, this directory will contain 42 classes (plus the special automatically generated nodes--these will be discussed later), with names corresponding to the left-hand side names of the productions. Like jtb.out.jj, after generation these files don't need to be edited. Generate them once, compile them once, and forget about them

20 Example Let's examine one of the classes generated from a production. Take, for example, the following production void ImportDeclaration() : {} { "import" Name() [ "." "*" ] ";“ }

21 What gets produced? Part 1 // Generated by JTB // package syntaxtree; /** * Grammar production: * f0 -> "import" * f1 -> Name() * f2 -> [ "." "*" ] * f3 -> ";" */ public class ImportDeclaration implements Node { public NodeToken f0; public Name f1; public NodeOptional f2; public NodeToken f3 ; All parts of a production are represented in the tree, including tokens.

22 The Syntax Tree Classes Notice the package "syntaxtree". The purpose of separating the generated tree node classes into their own package is that it greatly simplifies file organization, particularly when the grammar contains a large number of productions. It ’ s often not necessary to pay the syntax classes any more attention. All of the work is to done to the visitor classes. Note that this class implements an interface named Node.

23 Automatically-Generated Tree Node Interface and Classes Node All tree nodes implement this Nodelistinterface List interface that NodeList, NodeListOptional, and NodeSeqeunce implement Nodechoice Represents ( A | B ) Nodelist Represents ( A ) + NodeListOptional Represents ( A ) * NodeOptional Represents [ A ] or ( A )? NodeSequence Represents nexted sequence like [ "extends" Name() ] NodeToken Represents a token string

24 Node The interface Node is implemented by all syntax tree nodes. Node looks like this: public interface Node extends java.io.Serializable { public void accept(visitor.Visitor v); public Object accept(visitor.ObjectVisitor v, Object argu); }

25 Nodes and Accept All tree node classes implement the accept() method. –In the case of all the automatically-generated classes, the accept() method simply calls the corresponding visit(XXXX n) (where XXXX is the name of the production) method of the visitor passed to it. –Note that the visit() methods are overloaded, i.e. the distinguishing feature is the argument each takes, as opposed to its name.

26 Two New Features Two features presented in JTB 1.2 may be helpful – The first is that Node extends java.io.Serializable, meaning that you can now serialize your trees (or subtrees) to an output stream and read them back in. –Secondly, there is one accept() method that can take an extra argument and return a value.

27 What gets produced? Part 1 // Generated by JTB // package syntaxtree; /** * Grammar production: * f0 -> "import" * f1 -> Name() * f2 -> [ "." "*" ] * f3 -> ";" */ public class ImportDeclaration implements Node { public NodeToken f0; public Name f1; public NodeOptional f2; public NodeToken f3 ; All parts of a production are represented in the tree, including tokens.

28 NodeListInterface The interface NodeListInterface is implemented by NodeList, NodeListOptional, and NodeSequence. NodeListInterface looks like this: public interface NodeListInterface extends Node { public void addNode(Node n); public Node elementAt(int i); public java.util.Enumeration elements(); public int size(); }

29 Details Interface not generally needed but can be useful when writing code which only deals with the Vector-like functionality of any of the three classes listed above. –addNode() is used by the tree-building code to add nodes to the list. –elements() is similar to the method of the same name in Vector, returning an Enumeration of the elements in the list. –elementAt() returns the node at the ith position in the list (starting at 0, naturally). –size() returns the number of elements in the list

30 NodeChoice NodeChoice is the class which JTB uses to represent choice points in a grammar. An example of this would be ( "abstract" | "final" | "public" ) JTB would represent the production void ResultType() : {} { "void" | Type() } –as a class ResultType with a single child of type NodeChoice.

31 Details The type stored by this NodeChoice would not be determined until the file was actually parsed. The node stored by a NodeChoice would then be accessible through the choice field. Since the choice is of type Node, typecasts are sometimes necessary to access the fields of the node stored in a NodeChoice.

32 Implementation public class NodeChoice implements Node { public NodeChoice(Node node, int whichChoice); public void accept(visitor.Visitor v); public Object accept(visitor.ObjectVisitor v, Object argu); public Node choice; public int which; }

33 Which One? Another feature of NodeChoice is the field which for determining which of the choices was selected The which field is used to see which choice was used – If the first choice is selected, which equals 0 (following the old programming custom to start counting at 0). –If the second choice is taken, which equals 1. The third choice would be 2, etc. –Note that your code could potentially break if the order of the choices is changed in the grammar.

34 NodeList NodeList is the class used by JTB to represent lists. An example of a list would be ( "[" Expression() "]" )+ JTB would represent the javacc production : void ArrayDimensions() : {} { ( "[" Expression() "]" )+ ( "[" "]" )* } –as a class ArrayDimensions() with children NodeList and NodeListOptional respectively.

35 Details NodeLists use java.lang.Vectors to store the lists of nodes. Like NodeChoice, typecasts may occasionally be necessary to access fields of nodes contained in the list.

36 Implementation public class NodeList implements NodeListInterface { public NodeList(); public void addNode(Node n); public Enumeration elements(); public Node elementAt(int i); public int size(); public void accept(visitor.Visitor v); public Object accept(visitor.ObjectVisitor v, Object argu); public Vector nodes; }

37 NodeToken This class is used by JTB to store all tokens into the tree, including JavaCC "special tokens" (if the -tk command-line option is used). command-line option In addition, each NodeToken contains information about each token, including its starting and ending column and line numbers.

38 Implementation public class NodeToken implements Node { public NodeToken(String s); public NodeToken(String s, int kind, int beginLine, int beginColumn, int endLine, int endColumn); public String toString(); public void accept(visitor.Visitor v); public Object accept(visitor.ObjectVisitor v, Object argu); // -1 for these ints means no position info is available. // … }

39 Continued public class NodeToken implements Node { // …. public String tokenImage; public int beginLine, beginColumn, endLine, endColumn; // -1 if not available. // Equal to the JavaCC token "kind" integer. public int kind; // Special Token methods below public NodeToken getSpecialAt(int i); public int numSpecials(); public void addSpecial(NodeToken s); public void trimSpecials(); public String withSpecials(); public Vector specialTokens; }

40 Token Details The tokens are simply stored as strings. The field tokenImage can be accessed directly, and the toString() method returns the same string. Also available is the kind integer. JavaCC assigns each type of token a unique integer to identify it. This integer is now available in each JTB NodeToken. For more information on using the kind integer, see the JavaCC documentation.

41 Member Variables of Generated Classes Next comes the member variables of the ImportDeclaration class. These are generated based on the RHS of the production. Their type depends on the various items in the RHS and their names begin with f0 and work their way up. Why are they public? –Visitors which must access these fields reside in a different package than the syntax tree nodes –Package visibility cannot be used. – Breaking encapsulation was a necessary evil in this case.

42 What gets produced? Part 2 public ImportDeclaration(NodeToken n0, Name n1, NodeOptional n2, NodeToken n3) { f0 = n0; f1 = n1; f2 = n2; f3 = n3; } public ImportDeclaration(Name n0, NodeOptional n1) { f0 = new NodeToken("import"); f1 = n0; f2 = n1; f3 = new NodeToken(";"); }

43 Constructors The next portion of the generated class is the standard constructor. It is called from the tree-building actions in the annotated grammar so you will probably not need to use it. Following the first constructor is a convenience constructor with the constant tokens of the production already filled-in by the appropriate NodeToken. This constructor's purpose is to help in manual construction of syntax trees.

44 What gets produced? Part 3 public void accept(visitor.Visitor v) { v.visit(this); } public Object accept(visitor.ObjectVisitor v, Object argu) { return v.visit(this,argu); }

45 The Accept Methods After the constructor are the accept() methods. These methods are the way in which visitors interact with the class. void accept(visitor.Visitor v) –works with Visitor Object accept(visitor.ObjectVisitor v, Object argu) –works with ObjectVisitor

46 References Java Tree Builder Documentation Why Visitors?Why Visitors Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object- Oriented Software. Addison-Wesley, 1995