recursive descent parsing

Page 1 of 1

0 Replies - 143 Views - Last Post: 16 April 2013 - 01:37 PM Rate Topic: -----

#1 muhteremmuho  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 164
  • Joined: 06-January 11

recursive descent parsing

Posted 16 April 2013 - 01:37 PM

Hi all.My expression is "3*8-(5-3)*7-(2*5-3)/(5/(2+3))" I have a queston about recursive descent parsing .I could print the tree.My sequence is true but I need to leave a space in front of some expressions.Look at my tree and the true one below.How can I do right tree, arithmetic operation's right and left child must be at same line.Do you have any idea? (i.e. creating a variable and do something on this or specific method?)

this is my tree
Posted Image

true result
Posted Image

this is a one of the my classes. I think work will be done at this class.

/** Basic methods in a simple expression.
 * Only constants and four binary operations are implemented. 
 * Parentheses may be used witout any limitation.
 * 
 * Jan 2002   First version
 * 30.08.2002 redesigned
 * 23.12.2002 package
 * 07.04.2013 Simplified
 * 
 * @author M A Eyler
 */
public interface Expression {
   /** Returns float value of this Expression */    
   float fValue();
   /** String representation of this Expression */   
   String toString();
   /** Converts this Expression to postfix (RPN) */   
   String toPostfix();
   /** Converts this Expression to a tree */   
   String toTree();
}

class Constant implements Expression  {
   float num;
   /** Constructs a constant Expression. */   
   public Constant(float n) { num = n; }
   public float fValue() { return num; }
   public String toPostfix() { return ' '+numToStr(num); }
   public String toString() { return numToStr(num); }
   /** Converts argument to String, removing ".0" 
    * at the end of an integer constant.
    */   
   public static String numToStr(float n) {
      String s = ""+n;
      if (s.endsWith(".0"))  //integers need no decimal point
         s = s.substring(0, s.length()-2);
      return s;
   }
   public String toTree() { return numToStr(num); }
}

class Binary implements Expression  {
   Expression left, right; Token oper;
   /** Constructs a binary operation from two Expressions and an operator. 
    */   
   public Binary(Expression le, Token op, Expression re) {
      left = le; right = re; oper = op;
   }
   public float fValue() { 
      if (oper == Token.PLUS)  return left.fValue()+right.fValue();
      if (oper == Token.MINUS) return left.fValue()-right.fValue();
      if (oper == Token.STAR)  return left.fValue()*right.fValue();
      if (oper == Token.SLASH) return left.fValue()/right.fValue();
      return Float.NaN;
   }
   public String toPostfix() { 
      return left.toPostfix()+right.toPostfix()+' '+oper; 
   }
   int precedence() {
      if (oper == Token.PLUS || oper == Token.MINUS) return 10;
      if (oper == Token.STAR || oper == Token.SLASH) return 20;
      throw new IllegalArgumentException("operation "+oper);
   }
   public String toString() {
      return toString(left, false)+ oper +toString(right, true);
   }
   String toString(Expression e, boolean atRight) {
      String s = e.toString();
      if (!(e instanceof Binary)) return s;
      int prec = this.precedence();
      int p = ((Binary)e).precedence();
      if (prec<p ||(prec==p && !atRight)) return s;
      return Token.LEFT +s+ Token.RIGHT;
   }
   public String toTree() { return oper.name()+"\n"+left.toTree()+"\n"+right.toTree(); }
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1