recursive descent parsing

Page 1 of 1

0 Replies - 243 Views - Last Post: 16 April 2013 - 01:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=318708&amp;s=cb92d787eb7671c8ec0a4ea451b05a70&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 muhteremmuho

Reputation: -2
• Posts: 165
• 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

true result

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() {
}
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;