Parse1.java

Allikas: Lambda
import java.io.*;

class pdata {
  int ival;   
  ExpNode expval;  
  int strpt;
  boolean success=true;
  /*  
  pdata () {
   success=true;
  } 
  */    
}    

abstract class ExpNode {              
  abstract double value();  // Return the value of this node.           
} // end class ExpNode


class VarNode extends ExpNode {  // Represents a node that holds a number 
  String var;  
    
  VarNode( String str ) {       // Constructor.  Create a node to hold val.
     var = str;
  }
  double value() {      // The value is just the number that the node holds.
    return 10;
  }
} // end class ConstNode

class ConstNode extends ExpNode {  // Represents a node that holds a number 
  double number;  // The number in the node. 
    
  ConstNode( double val ) {       // Constructor.  Create a node to hold val.
     number = val;
  }
  double value() {      // The value is just the number that the node holds.
    return number;
  }
} // end class ConstNode

class BinOpNode extends ExpNode {   // Represents a node that holds an operator.
  char op;        // The operator.
  ExpNode left;   // The left operand.
  ExpNode right;  // The right operand.
  BinOpNode( char op, ExpNode left, ExpNode right ) {           // Constructor.  
    this.op = op;
    this.left = left; 
    this.right = right;
  }
  double value() {
     double leftVal = left.value();
     double rightVal = right.value();
     switch ( op ) {
         case '+':  return leftVal + rightVal;
         case '-':  return leftVal - rightVal;
         case '*':  return leftVal * rightVal;
         case '/':  return leftVal / rightVal;
         default:   return Double.NaN;  // Bad operator.
     }
  }
} // end class BinOpNode


public class parse1 {      
      public static void main(String[] args) {                          
         pdata res;          
         System.out.println("Parse: "+args[0]);          
         res=parseexp(args[0],0); 
         System.out.println("res ival: "+res.ival); 
         System.out.println("res expval: "+
           res.expval.value());  
          
       }

  static pdata parseexp(String str, int pt) {
    int i1;
    int i2;
    ExpNode exp; 
    char s;  
    pdata el1;   
    pdata el2;  
    pdata res;  
    int len;
      
    len=str.length();  
      
    if (Character.isDigit(str.charAt(pt))) {
      return parseint(str,pt);
    }    
    
    if (str.charAt(pt)!='(') {
      return parsevar(str,pt);                     
    }   
    
    pt++;
    el1=parseexp(str,pt);
    if ((el1.strpt)>=len) {
      return parseerr("cannot find operator");
    }     
    s=str.charAt(el1.strpt);  
    if ((el1.strpt+1)>=len) {
      return parseerr("cannot find second arg");
    }    
    el2=parseexp(str,el1.strpt+1);  
    if (str.charAt(el2.strpt)!=')') {
      return parseerr("cannot find )");
    }  
    /*  
    System.out.println(
        "el1 ival: "+  
        el1.ival+
        " el1 strpt: "+
        el1.strpt+
       "s: "+s+
       "el2 ival: "+  
        el2.ival+
        " el2 strpt: "+
        el2.strpt);      
    */
    res=new pdata();
    res.strpt=el2.strpt+1;
    /*
    if (s=='+') res.ival=el1.ival+el2.ival;
    else if (s=='*') res.ival=el1.ival*el2.ival;        
    else if (s=='-') res.ival=el1.ival-el2.ival;    
    else if (s=='/') res.ival=el1.ival/el2.ival;                 
    else return parseerr("unknown operator");
    */
    exp = new BinOpNode(s,el1.expval,el2.expval);
    res.expval=exp;
    return res;        
  }      
       
    
  static pdata parseint(String str, int pt) {
      int ires=0;   
      int i;     
      ExpNode exp;      
      int len;
      String tmp;
      pdata res; 
      len=str.length();      
      for(i=pt; i<len; i++) {
        if (!Character.isDigit(str.charAt(i))) {
          break;
        }    
      }          
      //System.out.println("str: "+str);
      //System.out.println("i: "+i);
      tmp=str.substring(pt,i);            
      //System.out.println(" tmp: "+tmp);
      ires = Integer.parseInt(tmp);        
      res=new pdata();
      //res.ival=ires;
      exp= new ConstNode(ires);
      res.expval=exp;
      res.strpt=i;
      return res;               
    }
  
  static pdata parsevar(String str, int pt) {    
      pdata res;
      ExpNode exp;      
      res = new pdata(); 
      exp= new VarNode(str.substring(pt,pt+1));
      res.expval=exp;
      res.strpt=pt+1;
      return res;               
    }  
    
  static pdata parseerr(String errstr) {
    pdata res;
    res = new pdata();  
    res.success=false;
    System.out.println("ERROR : "+errstr);      
    return res;
  }      
}