Page 1 of 1

Implement "public void print(int indent_level)" method for ASTIntegerLiteral and ASTOperatorExpression classes The print

Posted: Sun May 15, 2022 1:57 pm
by answerhappygod
Implement "public void print(int indent_level)" method for
ASTIntegerLiteral and ASTOperatorExpression classes
The print function should print the tree side way as follow:
Expression: 1+2*(2-10);
Tree printed:
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 1
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 1 (1.55 KiB) Viewed 50 times
+
|__ 1
|__ *
|__ 2
|__ -
|___2
|___10
Calc1.jj
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 2
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 2 (7.91 KiB) Viewed 50 times
ASTExpression.java
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 3
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 3 (8.99 KiB) Viewed 50 times
ASTIntegerLiteral.java
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 4
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 4 (16.66 KiB) Viewed 50 times
ASTOperatorExpression.java
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 5
Implement Public Void Print Int Indent Level Method For Astintegerliteral And Astoperatorexpression Classes The Print 5 (29.9 KiB) Viewed 50 times
1+2* (2-10); + + 1 2 2 2 10

Calci > JRE System Library [JavaSE-15] src (default package) # ASTCalc EA ASTExpression.java > DASTIntegerLiteral.java > DASTOperatorExpression.java

* 3 6 10 /** 2 Javacc template file created by SF Javacc plugin 1.5.28+ wizard for Javacc 1.5.0+ */ 4e options 5 { static = true; 7} 90 PARSER_BEGIN(Calculator1) 10 package ASTCalc; 120 public class Calculatori 8 11 13 { == 140 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public static void main(String args[]) throws ParseException { Calculatori parser = new Calculator1(System.in); ASTExpression expression; while (true) { System.out.println("Reading from standard input..."); System.out.print("Enter an expression like \"1+(2+3)*4;\" :"); try { expression = Calculatori.one_line(); if (expression null) System.out.println("Goodbye."); else System.out.println(expression.eval(); } catch (Exception e) { System.out.println("NOK."); System.out.println(e.getMessage(); Calculator1. ReInit(System.in); } catch (Error e) { System.out.println("Oops."); System.out.println(e.getMessage); break; } j占占占占占占总 } 44 45 46} }

11* 47 48 PARSER_END (Calculator) 49 500 SKIP: 51 { 52 53 | "\r" 54 "It" 55 | "\n" 56 } 57 580 TOKEN : /* OPERATORS */ 59 { 60 < PLUS : "+" > 61 | < MINUS : 62 | <MULTIPLY : > 63 | <DIVIDE : "/"> 64 } 65 660 TOKEN: 67 { 68 < CONSTANT : (< DIGIT >)+ > 69 | <#DIGIT : [ ""-"9" ] > 70 } 71 720 ASTExpression one_line(): 73 { 74 ASTExpression tree; 75 } 76 { 77 tree = sum) ";" 78 { return tree; } 81 | ";" 82 { 83 return null; } 四四四四四四四弘弘矿郎 80 84 85 } 86 870 ASTExpression sum(): 88 { 89 ASTExpression tree; 90 ASTExpression right; 91 Token operator; 92 }

93 { 94 tree - term() 95 C 96 ( 97 operator = < PLUS > 98 | operator = < MINUS > 99 ) 100 right - term() { 101 tree = new ASTOperator Expression (tree, right, operator.image); 102 } 103 )* 104 { 105 return tree; 106 } 107 } 108 1090 ASTExpression term() : 110 { 111 ASTExpression tree; 112 ASTExpression right; 113 Token operator; 114 } 115{ 116 tree = unary() 117 ( 118 ( 119 operator = < MULTIPLY > 120 | operator = <DIVIDE > 121 ) 122 right unary() { 123 tree = new ASTOperator Expression (tree, right, operator.image); 124 } 125 )* 126 { 127 return tree; 128 } 129 } 130 =

133 1310 ASTExpression unary(): 132 { ASTExpression tree; 134 } 135 { 136 < MINUS > tree = element) 137 { return new ASTOperatorExpression (new ASTIntegerLiteral(®), tree, ASTOperator Expression.MINUS); } 138 | tree = element) 139 { return tree; } 140 } 141 1420 ASTExpression element(): 143 { 144 ASTExpression tree; Token t; 146 } 147 { t = < CONSTANT > { return new ASTIntegerLiteral(Integer.parseInt(t.image)); } 149 | "(" tree = sum() ")" { return tree; } 150 } 151 145 148

2 1 package ASTCalc; 3 public abstract class ASTExpressioni abstract public int eval(); 5 abstract public void print(int indent_level); { 4 10 1+2* (2-10); + 1 60 7 8 9 10 11 12 13 14 15 16 17 18 19 2 AINS 0 1 2 10 } 2011

1 package ASTCalc; 2 4 60 7 8 11 12 3 public class ASTIntegerLiteral extends ASTExpression { private int value_; 5 public ASTIntegerLiteral(int value) { // constructor value_ = value; } 9 100 public int value() { // getter return value_; } 13 public void setValue(int value) { // setter value_ = value; 16 } 17 public int eval() { return value_j 20 } 21 2220 public void print(int indent_level) { 23 // implement this !! 24 25 } 26 } 27 140 15 A180 19

1 package ASTCalc; WOOOWNP 10 11 12 150 17 18 3 public class ASTOperator Expression extends ASTExpression { private ASTExpression left_; private ASTExpression right_j private int operator_; public static final int ERROR = 0; 9 public static final int PLUS = 1; public static final int MINUS = 2; public static final int MULTIPLY = 3; public static final int DIVIDE = 4; 13 14 // constructors public ASTOperator Expression (ASTExpression left, ASTExpression right, int operator) { 16 left_ = left; right_ = right; operator_ = operator; 19 } 20 21 public ASTOperatorExpression (ASTExpression left, ASTExpression right, String operator) { 22 left_ = left; 23 right_ = right; 24 25 if (operator.compareTo("+") 0) 26 operator PLUS; 27 else if (operator.compareTo("-") 0) 28 operator_ = MINUS; 29 else if (operator.compareTo("*") 0) 30 operator_ - MULTIPLY; 31 else if (operator.compareTo("/") 0) 32 operator_ = = DIVIDE; 33 else 34 operator_ = ERROR; 35 } 36 = = == = == WWW.NNNNN == == =

= == A370 38 39 40 41 42 43 44 45 46 47 48 49 == public int eval() { int leftValue = left().eval(); int rightValue = right().eval(); if (operator) PLUS) return leftValue + rightValue; else if (operator) MINUS) return leftValue - rightValue; else if (operator() == MULTIPLY) return leftValue * rightValue; else if (operator() == DIVIDE) return leftValue / rightValue; else return 0; } 50 51 52 public void print(int indent_level) { // implement this !! } // getter public ASTExpression left() { return left_; } A530 54 55 56 57 58 596 60 61 62 630 64 65 66 670 68 69 70 } 71 public ASTExpression right() { return right_; } public int operator() { return operator_j }