JAVA Compiliation

JAVA Compiliation

How do I code and compile in the areas that say "IMPLEMENT ME"?

import java.util.Scanner;

import java.util.Stack;

class EvalException extends Exception{ public EvalException(String msg) { super(msg); }

interface Expr {

   public int eval() throws EvalException;

}

class Atom implements Expr {

  String value;

  public Atom(String value) { this.value = value; }

  public String toString() { return value; }

  public int eval() throws EvalException {

  /* IMPLEMENT ME
   * We'll assume Atoms represent integers when we eval them, so try to
   * parse the value as an int.  If this can't be done, we'll want to throw
   * an EvalException with the proper error message.
   */
  }

}

class List<E> implements Expr {

  private Node<E> head;

  public List(E... elements) {
     head = null;

     for(int i = elements.length - 1; i >= 0; i--) {
        head = new Node<E>(elements[i], head);
     }
  }

  public void append(E element) {
     if (head == null) {
        head = new Node<E>(element, null);
     }
     else {
        Node<E> last = head;
        while (last.link != null) last = last.link;
        last.link = new Node<E>(element, null);
     }
  }

  public String toString() {
     String s = "( ";
     Node<E> elements = head;
     while (elements != null) {
        s += elements.data + " ";
        elements = elements.link;
     }
     return s + ")";
  }

  public void evalAndPrintEach() {
     Node<Expr> current = (Node<Expr>) head;
     while (current != null) {
        try {
           System.out.println( ((Expr) current.data).eval() );
        }
        catch (EvalException e) {
           System.out.println(e);
        }
        current = current.link;
     }
  }

  private Node<Integer> evalArgs(Node<Expr> xs) throws EvalException {
     if (xs == null) return null;
        return new Node<Integer>(xs.data.eval(), evalArgs(xs.link));
  }

  public int eval() throws EvalException {
     Node<Expr> elts = (Node<Expr>) head;
     if (elts == null) throw new EvalException("Empty list.");
        String op = ((Atom) elts.data).value;

     /* IMPLEMENT ME
      * To evaluate a (non-empty) List<Expr>, we look at the first element
      * (which we assume is an Atom), evaluate each of the remaining elements
      * (using evalArgs), then apply the appropriate operator to the results.
      * See the examples in the handout.
      *
      * Attempting to evaluate an empty list results in an error.  If op isn't
      * one of "+", "-", "*", "/", then this too is an error.
      */
  }

  private int add(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Find the sum of xs using a right fold.
      */
  }

  private int mul(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Find the product of xs using a left fold.
      */
  }

  private int sub(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Calculate the result by using a tail-recursive helper method.
      */
  }

  private int div(Node<Integer> xs) throws EvalException {
     /* IMPLEMENT ME
      * Calculate the result iteratively.  If xs doesn't have at least 2
      * elements, throw an Exception.
      */
  }

  private class Node<E> {
     E data;
     Node<E> link;

     public Node (E data, Node<E> link) {
        this.data = data;
        this.link = link;
     }
  }

}

class UnclosedListException extends Exception { }

class Lisp {

  public static List<Expr> parse(String[] tokens) throws Exception {
     Stack<List<Expr>> stack = new Stack<List<Expr>>();

     stack.push(new List<Expr>());

     for (String token : tokens) {
        if (token.equals("(")) {
           stack.push(new List<Expr>());
        }

        else if (token.equals(")")) {
           List<Expr> toAppend = stack.pop();
           stack.peek().append(toAppend);
        }

        else {
           stack.peek().append(new Atom(token));
        }
     }

     if (stack.size() > 1)
        throw new UnclosedListException();

     return stack.pop();
  }

  public static void main(String[] args) throws Exception {
     Scanner in = new Scanner(System.in);

     while(true) {
        System.out.print("lisp> ");
        String line = in.nextLine();
        try {
           parse(line.split("\\s+")).evalAndPrintEach();
        }
        catch (Exception e) {
           System.out.println("Parsing error.");
        }
     }
  }

}

How do I code and compile in the areas that say "IMPLEMENT ME"?

View Answers









Related Tutorials/Questions & Answers:
JAVA Compiliation
JAVA Compiliation  How do I code and compile in the areas that say "IMPLEMENT ME"? import java.util.Scanner; import java.util.Stack; class EvalException extends Exception{ public EvalException(String msg) { super(msg
compiliation - JSP-Servlet
Advertisements
java
java  diff bt core java and java
java
java  what is java
JAVA
JAVA  how the name came for java language as "JAVA
java
java   why iterator in java if we for loop
java
java  explain technologies are used in java now days and structure java
java
java  different between java & core java
Java
Java   Whether Java is pure object oriented Language
java
java  is java open source
java
java  what is java reflection
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java  give a simple example for inheritance in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java   What is ?static? keyword
java
java  RARP implementation using java socket
java
java  sample code for RARP using java
Java
Java  how to do java in command prompt
java
java  Write a java code to print "ABABBABCABABBA
java
java  write a program in java to acess the email
java
java  send me java interview questions
java
java  how use java method
java
java  is java purely object oriented language
java
java  why multiple inheritance is not possible in java
java
java  write a java program using filenotfoundexception
java
java  give a simple example for inheritance in java
java
java  why to set classpath in java
java
java  Does java allows multiline comments
java
java  what are JAVA applications development tools
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  explain object oriented concept in java
java
java   difference between class and interface
Java
Java  how to draw class diagrams in java
java
java  hi im new to java plz suggest me how to master java[email protected]
java
java   How to set java Policy for applet using jdk 6
java
java pattern code for a given words  java pattern code for a given words pattern
java
java  dear, i want a field for date picker using java/java script
java
java  create java program for delete and update the details,without using database, just a normal java program
java
java  why methods in java raise exceptions   Have a look at the following link: Java Exceptions
java
java code to search the nodes  how to write the java code to search the nodes using routers
java
java online telephone directory  i need coding for online telephone directory..by using java....pls help me
java
java  how to edit text document by using java then how to edit starting and ending of text document by using java
java
java  different between java & core java print("code sample
java
java  different between java & core java print("code sample
java
java  how can use sleep in java   which book learn of java language
java
java  how to invoke one chart java file from another java file
java
java  how to prepare the java   Hi Friend, If you want to learn how to install java, creating and running a java program then go through the following links: http://www.roseindia.net/java/beginners/index.shtml http

Ads