Program to abstract syntax tree constructed, JAVA Programming

Please submit your answers    

In this homework, we are going to generate Java Virtual Machine codes by processing the abstract syntax tree constructed from the parsing process.

We are going to support all features of C-- language for integer data type. That is, for the purpose of this homework, float data type is not supported.

Please refer to the following documents about Java Virtual Machine codes:

  • Instructions Set (limited to instructions that are related to integers only)
  • Compiling Examples

The template for codes generation program is similar to that given for hw7. Also, click here for an executable for hw8.

One can obtain the Java Virtual Machine codes for the test data as follows: Take the example of data04. We can present it as a Java program as follows:

public class data04 {

public static


Factorial(int v)


   int limit = 7;

   if (v < 0 || v > limit) return -1;


       int c = 0, fact = 1;

       /* Loop invariant: fact == c! */

       while (c < v) {

          c = c+1; fact = fact*c;


       return fact;




The commands are:


  javap -c data04 >

The command javap will produce the Java Virtual Machine Codes in readable format.

You can also compare the output by javap with the output of hw8 by running

  hw8 < data04 > data04.out

NOTE: The codes produced by hw8 may differ from that by the Java compiler. But the codes are close. Sometimes, hw8 has a few more unnecessary branching statements which can be cleaned up by a simple pass over the generated codes.

Programming hints:

Previously, traverse_ast does not return any value. You may want to extend the function traverse_ast to return the number of lines of codes generated. In order that traverse_ast can generate the right line numbers for the Java Virtual Machine codes, the function traverse_ast takes one extra parameter next_line_number which is the line number of the first instruction generated in the processing of the subtree rooted at node.

The (unsophisticated) codes for AST_ADD in traverse_ast can be very simple as follows:

case AST_ADD:

             size  = traverse_ast( node->left  , next_line_number        );

             size += traverse_ast( node->right , next_line_number + size );

             printf("%4d:   iadd\n", next_line_number+size);


             return size;

You need to modify your symtbl module so that each ID symbol has an entry with a field local_var_index. Note that we do not make use of any temp variable for codes generation in this homework. We need a global variable local_var_num, which use is similar to that of temp_num, to keep track of the largest variable number that is currently in use. The function symtbl_dump_entry needs to be revised to print also the local_var_index associated with an identifier.

Also, modify hw5parser.y so that float data type is no longer supported.

type_spec   : INT

              { data_type = TYPE_INT; }

            | FLOAT

              { yyerror("float unsupported\n"); exit(-1); }


Java uses special comparison instructions when one of the operands is 0. They are ifeq, ifne, iflt, ifge, ifgt, ifle. For your first attempt, you can just use the general comparison instructions if_icmpeq, if_icmpne, if_icmplt, if_icmpge, if_icmpgt, if_icmple so that you can avoid testing if one of the operands is 0.

NOTE: the codes that your program generates do not have to match the quality of the codes generated by the Java (javac) compiler, or my hw8.

NOTE: You can assume that each integer constant in a C-- program is of small value, and can be represented in one byte so that there is no need to use the runtime constant pool. Also, you can assume that the number of local variables is small such that the index into the local variable array can be achieved in one byte.

Posted Date: 2/28/2013 5:14:15 AM | Location : United States

Related Discussions:- Program to abstract syntax tree constructed, Assignment Help, Ask Question on Program to abstract syntax tree constructed, Get Answer, Expert's Help, Program to abstract syntax tree constructed Discussions

Write discussion on Program to abstract syntax tree constructed
Your posts are moderated
Related Questions
Need Java Assignment for course Project Description: I want to get an assignment done. Involves building on a minesweeper game written in Java. You subsequent code is to be u

How many static initializers can you have ? As many as you want, but the static initializers and class variable initializers are implemented in textual order and may not refer

(CarbonFootprint Interface: Polymorphism) Using interfaces, as you learned in this chapter, you can specify similar behaviors for possibly disparate classes. Governments and compan

What is casting? There are two types of casting, casting among primitive numeric types and casting among object references. Casting among numeric types is used to convert large

I have to weave two positive integers together. For example, if the numbers are 137 and 064, the output integer would be 103647. This has to be done using integer arithmetic (not s

send me the java code for minimum shelf program

This language was designed to adapt the changing environment and behavior. New methods and instance variables can be added to the client side without any major changes happening at

THE FOLLOWING PROGRAM SHOULD BE JAVA IN ECLIPSE Minimal Spanning Tree Decreasing Edge Dismissal Reverse-delete algorithm. Develop an implementation that computes the MST as fol

What is Overriding? When a class explains a method using the similar name, return type, and arguments as a method in its superclass, the method in the class overrides the metho

Task I have an Android mobile app project that I''m trying to get profiles to transfer by connecting to a server at port 1337. I have to send a json string to the se