Question

I have a grammar Gram.ge I have to test it with Jasmin. The folder directory contains:

  • Gram.g
  • CodeGenerator.java
  • Instruction.java
  • Main.java
  • Opcode.java
  • SymbolTable.java
  • Type.java
  • jasmin.jar
  • Library directory that contains some programs (for example A.pas) very simple written according to my grammar and saved in .pas

While the parent folder (the one immediately above) contains:

  • antlr-3.5-complete.jar

Then from the command prompt (Windows) I position in the directory and launch the following commands:

java -cp .;../antlr-3.5-complete.jar Org.anltr.Tool Gram.g

javac -cp .;../antlr-3.5-complete.jar *.java

java -cp .;../antlr-3.5-complete.jar Main/Library/A.pas > Output.j

java -jar jasmin.jar Output.j

java Output

CodeGenerator.java file: import java.util.*;

public class CodeGenerator {

    private Vector<Instruction> instructions = new Vector<Instruction>(); 
    private int counter = 0;

    public Instruction emit(Opcode opCode) {
        Instruction ist = new Instruction(opCode); 
        instructions.add(ist); 
        return ist; 
    }

    public Instruction emit(Opcode opCode, int operand) {
        Instruction ist = new Instruction(opCode, operand);
        instructions.add(ist);
        return ist;
    }

    public void emitLabel(int operand) {
        emit(Opcode.LABEL, operand);
    }

    public int newLabel() {
        return counter++;
    }

    public String toJasmin() {
        String temp = "";
        temp += header; 
        for(Instruction ist : instructions) 
            temp += ist.toJasmin() + "\n";
        temp += footer;
        return temp;
    }

    public String toString() {
        String temp = "";
        for(Instruction ist : instructions)
            temp += ist + "\n";
        return temp;
    }

    private static final String header =
        ".class public Output \n"
        + ".super java/lang/Object \n"
        + "\n"
        + ".method public <init>()V\n"
        + "  aload_0 \n"
        + "  invokenonvirtual java/lang/Object/<init>()V\n"
        + "  return \n"
        + ".end method \n"
        + "\n"
        + ".method public static printBool(I)V\n"
        + "  .limit stack 3\n"
        + "  getstatic java/lang/System/out Ljava/io/PrintStream;\n"
        + "  iload_0 \n"
        + "  bipush 1\n"
        + "  if_icmpeq Ltrue \n"
        + "  ldc \"false\"\n"
        + "  goto Lnext \n"
        + "Ltrue:\n"
        + "  ldc \"true\"\n"
        + "Lnext:\n"
        + "  invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V\n"
        + "  return \n"
        + ".end method \n"
        + "\n"
        + ".method public static printInt(I)V\n"
        + "  .limit stack 2\n"
        + "  getstatic java/lang/System/out Ljava/io/PrintStream;\n"
        + "  iload_0 \n"
        + "  invokestatic java/lang/Integer/toString(I)Ljava/lang/String;\n"
        + "  invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V\n"
        + "  return \n"
        + ".end method \n"
        + "\n"
        + ".method public static run()V\n"
        + "  .limit stack 1024\n"
        + "  .limit locals 256\n";

    private static final String footer =
        "  return \n"
        + ".end method \n"
        + "\n"
        + ".method public static main([Ljava/lang/String;)V\n"
        + "  invokestatic Output/run()V\n"
        + "  return \n"
        + ".end method \n";     

}       

The commands are executed without any problems except the last (java Output) that generates this error:

    Exception in thread "main" java.lang.NoClassDefFoundError:
            at Output.run(Output.j)
            at Output.main(Output.j)
    Caused by: java.lang.ClassNotFoundException:
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 2 more

The Output.j file is:

.class public Output 
.super java/lang/Object 

.method public <init>()V
  aload_0 
  invokenonvirtual java/lang/Object/<init>()V
  return 
.end method 

.method public static printBool(I)V
  .limit stack 3
  getstatic java/lang/System/out Ljava/io/PrintStream;
  iload_0 
  bipush 1
  if_icmpeq Ltrue 
  ldc "false"
  goto Lnext 
Ltrue:
  ldc "true"
Lnext:
  invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
  return 
.end method 

.method public static printInt(I)V
  .limit stack 2
  getstatic java/lang/System/out Ljava/io/PrintStream;
  iload_0 
  invokestatic java/lang/Integer/toString(I)Ljava/lang/String;
  invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
  return 
.end method 

.method public static run()V
  .limit stack 1024
  .limit locals 256
  ldc 10
  ldc 20
  ldc 30
  imul
  iadd
  invokestatic printInt(I)V
  return 
.end method 

.method public static main([Ljava/lang/String;)V
  invokestatic Output/run()V
  return 
.end method 

The A.pas file is:

print(10 + 20 * 30)

Where could be the error? Thanks

Was it helpful?

Solution

Your error message says:

java.lang.ClassNotFoundException:

But the format of this message usually looks like this:

java.lang.ClassNotFoundException: SomeNonExistingClass

How comes that the name of the class that is not found is missing? Because you have an instruction in your example that refers to an empty class name (""). So you need to look for an instruction where you forgot to specify the class name. And that's here:

invokestatic printInt(I)V

Instead it should read:

invokestatic Output/printInt(I)V

this will fix your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top