# A Step-by-Step Solution Methodology for Mathematical Expressions

^{1}

^{2}

^{*}

## Abstract

**:**

## 1. Introduction

## 2. Materials and Methods

#### 2.1. Review of the Literature

#### 2.2. Methodology Illustration

**Phase 1**:- Parser definition
**Steps**: Grammar design, grammar conversion, compiler-compiler input construction, code generation **Phase 2**:- Input data analysis
**Steps**: Token generation, syntax analysis, AST generation **Phase 3**:- Evaluation and interpretation
**Steps**: Solving expressions, simplification, printing intermediate evaluation results.

#### 2.3. Evaluation of Mathematical Expressions

#### 2.3.1. BNF Grammar Definition

Listing 1. An Extended-BNF grammar for mathematical expression. |

G = {Σ, T, V, P, S} V = {expr, op, func, var, number, digit}⊆ Σ T = {x, Sin, Cos, Tan, Log, Exp, Sqrt, +, -, *, /}⊆Σ Σ = T ∪ V, S = { expr } <expr>::= <expr> <op> <expr> |(<expr>) |<func>(<expr>) |<var> |<Number> <op>::=‘+’ | ‘-’ | ‘*’ | ‘/’ | ‘^’ <func>::= ‘Sin’ | ‘Cos’ | ‘Tan’ | ‘Log’ |’Exp’ | ‘Sqrt’ <var>::=‘x’ <number>::= ‘-’ ? <digit> + (‘. ‘ <digit> +)? <digit>::=[‘ 0’-’9’] |

#### 2.3.2. Grammar Conversion

Listing 2. A LL(1) grammar for mathematical expressions. |

G = {Σ, T, V, P, S} V = {expr, element, term, unary, power, func, number, digit}⊆Σ T = {x, Sin, Cos, Tan, Log, Exp, Sqrt, (,), +, -, *, /, ^}⊆Σ Σ = T ∪ V, S = { expr } <expr>→<unary> <term> [(“+” | ”-”) <term>]* <unary>→(“+” | ”-”)? <term>→<power> [(“*” | ”/”)<power>]* <power>→<element> (“^”<power>)? <element>→<func> (<expr>) | <number> | “x” <func>→”Sin “ | “Cos” | “Tan” | “Log” | “Exp” | “Sqrt” <number>→”-”? <digit> + (“.”<digit>+)? <digit>→[“ 0”-”9”] |

#### 2.3.3. Definition of Syntax Classes

Listing 3. The definition of some syntax classes shown in Table 1. |

public abstract class Exp { public Exp exp1, exp2; public Exp (Exp e1, Exp e2){ this.exp1 = e1; this.exp2 = e2; } } public class Plus extends Exp { … } … public class Sin extends Exp { public Sin(Exp e) {super(e, null); } } … public class Num extends Exp { public double num; public Num(double n){ this.num = n; } } public class Var extends Exp { private String var; public Var() { super(null, null) } public Var(String var) { super(null, null); this.var = var; } } |

Listing 4. The definition of some syntax classes for eval() method. |

abstract class Exp { … public abstract double Eval(int x); } class Plus extends Exp { Exp exp1, exp2, tmp; public Plus(Exp e1, Exp e2) { exp1 = e1; exp2 = e2; } public double Eval(int x) { return (exp1.Eval(x)+exp2.Eval(x)); } … } class Minus extends Exp { Exp exp1, exp2; Data val2; Exp tmp; public Minus(Exp e1, Exp e2) { exp1 = e1; exp2 = e2; } public double Eval(int x) { return (exp1.Eval(x)- exp2.Eval(x)); } … } class Times extends Exp { Exp exp1, exp2, tmp; Data val2; public Times(Exp e1, Exp e2) { exp1 = e1; exp2 = e2; } public double Eval(int x) { return (exp1.Eval(x)*exp2.Eval(x)); } … } class Var extends Exp { public Var(String var) { this.var = var; } public double Eval(int x) { return (x); } … } |

#### 2.3.4. Parser Construction

#### Token Specification

Listing 5. JavaCC token declarations of the terminals in the grammar Listing 2. |

TOKEN : { < NUMBER : ([“0”-”9”])+(“.” ([“0”-”9”])+)? > } TOKEN : { < EOL : “\n” > } TOKEN : /* OPERATORS */ { < PLUS: “+” > | < MINUS: “-” > | < TIMES: “*” > | < DIV : “/” > | < POW : “^” > …… } TOKEN : /* FUNCTIONS */ { < SQRT: “sqrt” > | < SIN: “sin” > | < COS: “cos” > …… } TOKEN : /* SYMBOLS */ { < X: “x” > | < LPR: “(“ > | < RPR: “)” > } SKIP : { “ “ | “\t” | “\r” } |

#### Parser Definition

#### 2.3.5. Generating Abstract Syntax Tree (AST)

Listing 6. The Java statements added to produce AST. |

Exp parse() : { Exp a; }{ a = expr() (<EOF> | <EOL>) { return a; } } Exp expr() : { Exp a, b; }{ a = term() ( <PLUS> b = term() { a = new Plus(a, b); } | <MINUS> b = term() { a = new Minus(a, b); } )* { return a; } } … Exp power() : { Exp a, b; }{ a = element() ( <POWER> b = power() { a = new Power(a, b); } )? { return a; } }… Exp element() : { Token t; Exp a; } { t=<NUMBER> { return (new Num(Double.parseDouble(t.image))); } | <X> { return (new Var()); } | <LPR> a = expr() <PPR> { return a;} | <SIN> <LPR> a=expr() <PPR> { return new Sin(a);} … }... |

#### 2.3.6. Evaluation of Mathematical Expressions

#### Direct Evaluation of Mathematical Expressions

^{3}+ 7x + 1” evaluated for x = 2, the combined process of parameter passing and evaluation will return 39.

Listing 7. Some JavaCC functions for string-based evaluation. |

double parse(double x) : { double a; } { a = expr(x) (<EOF> | <EOL>) { return a; } } double expr(double x) : { double a, b; }{ a = term(x) ( <PLUS> b = term(x) { a = a+b; } | <MINUS> b = term(x) { a = a-b; } )* { return a; } } double power(double x) : { double a, b; }{ a = element(x) ( <POWER> b = power(x) { a = Math.pow (a, b); } )? { return a; } } double element(double x) : { Token t; double a;} { t = <NUMBER> { return Double.parseDouble(t.image); } | <X> { return x; } | <LPR> a = expr(x) <RPR> { return a; } | <SIN> <LPR> a = expr(x) <RPR> { return Math.sin(a); } … } |

#### Evaluation of Mathematical Expressions Using AST

#### 2.4. Representation of Mathematical Expressions

#### 2.4.1. Human-Readable Format

#### 2.4.2. LaTex Format

#### 2.4.3. MathML Format

#### 2.5. Automatic Simplification

#### 2.5.1. Similar Operands

Listing 8. Simplifying similar operands for Num and Var in Plus. |

if (exp1 instanceOf Num && exp2 instanceOf Num) return new Num(exp1.getNum()+ exp2.getNum()); if (exp1 instanceOf Var && exp2 instanceOf Var ) return new Times (new Num(2), exp1); |

#### 2.5.2. Similar Operands on Different Levels

Listing 9. Collecting operands for similar operators. |

public ArrayList<Exp> sameOpList (){ ArrayList<Exp> a=new ArrayList<Exp>(); ArrayList<Exp> b=new ArrayList<Exp>(); if(exp1 instanceOf Plus || exp1 instanceOf Minus) a = exp1.sameOpList(); else a.add(exp1); if(exp2 instanceOf Plus || exp2 instanceOf Minus) b = exp2.sameOpList(); else b.add(exp2); return mergeList(a, b); } |

#### 2.5.3. Fraction Simplification

Listing 10. The simplify() method for Divide class. |

public class Divide extends Exp { public Exp simplify() { if (exp1 instanceof Divide || exp2 instanceof Divide){ if (!exp1 instanceof Divide) exp1 = new Divide(exp1, new Num(1)); if (!exp2 instanceof Divide) exp2 = new Divide(exp2, new Num(1)); } … //codes for GCD … return new Divide(exp1.simplify(), exp2.simplify()); } } |

Listing 11. Changes in the simplify() method for Divide class. |

exp1 = new Times(exp1.exp1.simplify(), exp2.exp2.simplify()).simplify(); exp2 = new Times(exp2.exp1.simplify(), exp1.exp2.simplify()).simplify(); … //codes for GCD … |

Listing 12. Modifications in class Plus. |

… if (exp1 instanceOf Divide || exp2 instanceOf Divide){ if (!exp1 instanceOf Divide) exp1 = new Divide (exp1, new Num(1)); if (!exp2 instanceOf Divide) exp2 = new Divide (exp2, new Num(1)); Exp a = exp1.exp1; Exp b = exp1.exp2; Exp c = exp2.exp1; Exp d = exp2.exp2; Exp G = polynomialLCM (b, d); a = new Times (a, polynomialDivide(G, b).result); c = new Times (c, polynomialDivide(G, d).result); return new Divide(new Plus(a, c), G); } … |

#### 2.5.4. Exponential Simplifications

^{a*b*c}in terms of left-associativity.

Listing 13. Simplify method for Power class. |

public class Power extends Exp { … public ArrayList<Exp> sameOpList(){ ArrayList<Exp> a = new ArrayList<Exp>(); if(exp2 instanceOf Power ) return mergeList(a, exp2.sameOpList()); else return exp2.simplify(); } … public Exp simplify() { Exp b = exp1.exp1; Exp e = new Num(1); ArrayList<Exp> pow = sameOpList(); for(Exp tmp: pow) e = new Times(e, tmp); return new Power(b, e); } … } |

^{n}.v

^{n}and u

^{n}.v

^{m}.

#### 2.5.5. Removal of Radical Expression in Denomination

Listing 14. Simplify method for Divide class. |

… if (exp2 instanceof Sqrt){ Exp sq = exp2.exp1; return new Divide(new Times(exp1, sq), sq); } … |

#### 2.5.6. Equality of Expressions

Listing 15. Equality of mathematical expressions. |

public class Exp{ public boolean isEqual(Exp e) { Exp a = this.simplify(); Exp b = e.simplify(); if(a.eval().equal(“0”) && b.eval().equal(“0”)) return true; Exp res = new Divide(a, b); return res.eval().equal(“0”); } } |

#### 2.5.7. Other Simplifications

- ${x}^{2}+\left(a+b\right)x+a.b\text{}=\left(x+a\right).\left(x+b\right)$
- ${x}^{2a}-{y}^{2b}=\left({x}^{a}+{y}^{b}\right).\left({x}^{a}-{y}^{b}\right)$
- $\mathrm{Sin}\text{}\left(\text{}exp1+\text{}exp2\right)=\mathrm{Sin}\left(exp1\right)\ast \mathrm{Cos}\left(exp2\right)+\text{}\mathrm{Cos}\left(exp1\right)\ast \mathrm{Sin}\left(exp2\right)$
- ${a}^{2}-2ab+{b}^{2}={\left(a-\mathrm{b}\right)}^{2}$
- ${a}^{3}+{b}^{3}=\left(a+b\right).\left({a}^{2}-ab+{b}^{2}\right)$
- ${a}^{3}-{b}^{3}=\left(a-b\right).\left({a}^{2}+ab+{b}^{2}\right)$

#### 2.6. Controlling the Step-by-Step Solution

- Print the transformation into an output buffer, and continue.
- Return to the root node after performing each transformation

Listing 16. The main loop for simplifications. |

public Exp performSimplification(Exp root){ do{ Controls.resetFlags(); root = root.simplify(); if(Controls.hasError() != null){ System.out.println(Controls.getError()); return null; } root.Print(); } while ( Controls.isSkipSimplifying() ); root.Print(); return root; } |

Listing 17. Modifications for the simplify() method. |

public Exp simplify(){ if(Controls.isSkipSimplifying()) return this; … // rest of the codes simplifying the element … // in case of performing any // simplification call Controller.skipSimplification(); … } |

#### Expression Simplification Control

Listing 18. The control of the simplification of radical removal using control flags. |

public Exp simplify(){ if(Controls.isSkipSimplifying()) return this; … if (exp2 instanceof Sqrt && Controls.isSqrtRemoveal()){ Exp sq = exp2.exp1; // Notify the transformation to controller Controller.skipSimplification(); return new Divide(new Times(exp1, sq), sq); } … } |

## 3. Conclusions and Results

## Author Contributions

## Funding

## Conflicts of Interest

## References

- Finkel, R.A.; Finkel, R.A. Advanced Programming Language Design; Addison-Wesley Reading: Boston, MA, USA, 1996. [Google Scholar]
- Watt, D.A.; Findlay, W.; Hughes, J. Programming Language Concepts and Paradigms; Prentice Hall Englewood Cliffs: Upper Saddle River, NJ, USA, 1990; Volume 234. [Google Scholar]
- Thisted, R.A. Elements of Statistical Computing: Numerical Computation; Routledge: Abingdon, UK, 2017. [Google Scholar]
- Van Hulzen, J.; Calmet, J. Computer algebra systems. In Computer Algebra; Springer: Berlin/Heidelberg, Germany, 1983; pp. 221–243. [Google Scholar]
- Dunham, P.H.; Dick, T.P. Research on graphing calculators. Math. Teach.
**1994**, 87, 440. [Google Scholar] - Palmiter, J.R. Effects of computer algebra systems on concept and skill acquisition in calculus. J. Res. Math. Educ.
**1991**, 22, 151–156. [Google Scholar] [CrossRef] - Artigue, M. Learning mathematics in a cas environment: The genesis of a reflection about instrumentation and the dialectics between technical and conceptual work. Int. J. Comput. Math. Learn.
**2002**, 7, 245. [Google Scholar] [CrossRef] - Pfister, G.-M.G.G.; Schonemann, H. Singular—A Computer Algebra System for Polynomial Computations. In Symbolic Computation and Automated Reasoning: The CALCULEMUS-2000 Symposium; AK Peters/CRC Press: Bocaton, FL, USA, 2001; p. 227. [Google Scholar]
- Peeters, K. Introducing cadabra: A symbolic computer algebra system for field theory problems. arXiv, 2007; arXiv:hep-th/0701238. [Google Scholar]
- Kajler, N. Cas/pi: A Portable and Extensible Interface for Computer Algebra Systems. In Proceedings of the Papers from the International Symposium on Symbolic and Algebraic Computation, Berkeley, CA, USA, 27–29 July 1992; ACM: New York, NY, USA, 1992; pp. 376–386. [Google Scholar]
- Hosseinpour, S.; Milani, M.M.R.A.; Pehlivan, H. A grammar-based methodology for producing mathematical expressions. Int. J. Innov. Stud. Sci. Eng. Technol.
**2017**, 3, 27–36. [Google Scholar] - Gulwani, S.; Korthikanti, V.A.; Tiwari, A. Synthesizing Geometry Constructions; ACM SIGPLAN Notices; ACM: New York, NY, USA, 2011; pp. 50–61. [Google Scholar]
- Singh, R.; Gulwani, S.; Rajamani, S.K. Automatically Generating Algebra Problems; AAAI: Palo Alto, CA, USA, 2012. [Google Scholar]
- Rasila, A.; Harjula, M.; Zenger, K. Automatic Assessment of Mathematics Exercises: Experiences and Future Prospects. In Proceedings of the ReflekTori 2007 Symposium of Engineering Education, Espoo, Finland, 3–4 December 2007; pp. 70–80. [Google Scholar]
- Rasila, A.; Havola, L.; Majander, H.; Malinen, J. Automatic assessment in engineering mathematics: Evaluation of the impact. In Proceedings of the ReflekTori 2010 Symposium of Engineering Education, Espoo, Finland, 9–10 December 2010; pp. 37–45. [Google Scholar]
- Cohen, J.S. Computer Algebra and Symbolic Computation: Mathematical Methods; Universities Press: Telangana, India, 2003. [Google Scholar]
- Von zur Gathen, J.; Gerhard, J. Modern Computer Algebra; Cambridge University Press: Cambridge, UK, 2003. [Google Scholar]
- Buchberger, B.; Loos, R. Algebraic simplification. In Computer Algebra; Springer: Berlin/Heidelberg, Germany, 1982; pp. 11–43. [Google Scholar]
- Moses, J. Algebraic Simplification A Guide for the Perplexed. In Proceedings of the Second ACM Symposium on Symbolic and Algebraic Manipulation, Los Angeles, CA, USA, 23–25 March 1971; ACM: New York, NY, USA, 1971; pp. 282–304. [Google Scholar]
- Shatnawi, M.; Youssef, A. Equivalence Detection Using Parse-Tree Normalization for Math Search. In Proceedings of the 2nd International Conference on Digital Information Management, Lyon, France, 28–31 October 2007; pp. 643–648. [Google Scholar]
- Youssef, A.; Shatnawi, M. Math search with equivalence detection using parse-tree normalization. In Proceedings of the 4th International Conference on Computer Science and Information Technology, Yangon, Myanmar, 23–24 February 2006. [Google Scholar]
- Apostolico, G. Pattern Matching Algorithms; Oxford University Press on Demand: Don Mills, ON, Canada, 1997. [Google Scholar]
- Zhang, K.; Shasha, D. Simple fast algorithms for the editing distance between trees and related problems. SIAM J. Comput.
**1989**, 18, 1245–1262. [Google Scholar] [CrossRef] - Demana, F.; Waits, B.K. Calculators in mathematics teaching and learning. Past, present, and future. In Learning Mathematics for a New Century; National Concil of Teachers of Mathematics: Reston, VA, USA, 2000; pp. 51–66. [Google Scholar]
- Judson, P.T. Effects of Modified Sequencing of Skills and Applications in Introductory Calculus; University of Texas at Austin: Austin, TX, USA, 1988. [Google Scholar]
- Runde, D.C. The Effect of Using the ti-92 on Basic College Algebra Students’ Ability to Solve Word Problems; ERIC Document Reproduction Service No. ED409046; Manatee Community College: Bradenton, FL, USA, 1997. [Google Scholar]
- Brown, R. Computer Algebra Systems in the Junior High School. In Proceedings of the 3rd International Derive/TI-92 Conference, Gettysburg, PA, USA, 14–17 July 1998. [Google Scholar]
- Drijvers, P. Assessment and new technologies: Different policies in different countries. Int. J. Comput. Algebra Math. Educ.
**1998**, 5, 81–94. [Google Scholar] - Heid, M.K. Resequencing skills and concepts in applied calculus using the computer as a tool. J. For Res. Math. Educ.
**1988**, 1, 3–25. [Google Scholar] [CrossRef] - Herget, W.; Heugl, H.; Kutzler, B.; Lehmann, E. Indispensable Manual Calculation Skills in a Cas Environment. Micromath
**2000**, 16, 8–17. [Google Scholar] - Bennett, G. Calculus for general education in a computer classroom. Int. Derive J.
**1995**, 2, 3–11. [Google Scholar] - Day, R.P. Algebra and technology. J. Comput. Math. Sci. Teach.
**1993**, 12, 29–36. [Google Scholar] - Tall, D. Functions and calculus. In International Handbook of Mathematics Education; Springer: Berlin/Heidelberg, Germany, 1996; pp. 289–325. [Google Scholar]
- Heid, M.K. The technological revolution and the reform of school mathematics. Am. J. Educ.
**1997**, 106, 5–61. [Google Scholar] [CrossRef] - Repo, S. Understanding and reflective abstraction: Learning the concept of derivative in a computer environment. Int. Derive J.
**1994**, 1, 97–113. [Google Scholar] - Small, D.B.; Hosack, J.M. Computer algebra system, tools for reforming calculus instruction. In Toward a Lean and Lively Calculus; Tulane University: New Orleans, LA, USA, 1986; pp. 143–155. [Google Scholar]
- Hearst, M.A. The debate on automated essay grading. IEEE Intell. Syst. Their Appl.
**2000**, 15, 22–37. [Google Scholar] [CrossRef] - Mohler, M.; Bunescu, R.; Mihalcea, R. Learning to grade short answer questions using semantic similarity measures and dependency graph alignments. In Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies—Volume 1, Portland, OR, USA, 19–24 June 2011; pp. 752–762. [Google Scholar]
- Farnsworth, C.C. Using computer simulations in problem-based learning. In Proceedings of the Thirty-Fifth ADCIS Conference, Nashville, TN, USA, 15–19 February 1994; pp. 137–140. [Google Scholar]
- Kashy, E.; Sherrill, B.; Tsai, Y.; Thaler, D.; Weinshank, D.; Engelmann, M.; Morrissey, D. Capa—An integrated computer-assisted personalized assignment system. Am. J. Phys.
**1993**, 61, 1124–1130. [Google Scholar] [CrossRef] - Dragon, T.; Mavrikis, M.; McLaren, B.M.; Harrer, A.; Kynigos, C.; Wegerif, R.; Yang, Y. Metafora: A web-based platform for learning to learn together in science and mathematics. IEEE Trans. Learn. Technol.
**2013**, 6, 197–207. [Google Scholar] [CrossRef] - Gutierrez-Santos, S.; Geraniou, E.; Pearce-Lazard, D.; Poulovassilis, A. Design of teacher assistance tools in an exploratory learning environment for algebraic generalization. IEEE Trans. Learn. Technol.
**2012**, 5, 366–376. [Google Scholar] [CrossRef] - Barker, D.S. Charlie: A Computer-managed homework, assignment and response, learning and instruction environment. In Proceedings of the Frontiers in Education 1997 27th Annual Conference, Teaching and Learning in an Era of Change, Pittsburgh, PA, USA, 5–8 November 1997; pp. 1503–1509. [Google Scholar]
- Baldwin, D. Three Years’ Experience with Gateway Labs; ACM SIGCSE Bulletin: New York, NY, USA, 1996; pp. 6–7. [Google Scholar]
- Bridgeman, S.; Goodrich, M.T.; Kobourov, S.G.; Tamassia, R. Pilot: An Interactive Tool for Learning and Grading; ACM SIGCSE Bulletin: New York, NY, USA, 2000; pp. 139–143. [Google Scholar]
- Brooker, R. The Solution of Algebraic Equations on the Edsac; Mathematical Proceedings of the Cambridge Philosophical Society; Cambridge University Press: Cambridge, UK, 1952; pp. 255–270. [Google Scholar]
- Maehly, H.J. Zur iterativen auflösung algebraischer gleichungen. Zeitschrift für Angewandte Mathematik und Physik ZAMP
**1954**, 5, 260–263. [Google Scholar] [CrossRef] - Butler, G.; Cannon, J. The Design of Cayley—A Language for Modern Algebra. In Proceedings of the International Symposium on Design and Implementation of Symbolic Computation Systems, Gmunden, Austria, 15–17 September 1993; Springer: Berlin/Heidelberg, Germany, 1990; pp. 10–19. [Google Scholar]
- Schönert, M.; Besche, H.U.; Breuer, T.; Celler, F.; Eick, B.; Felsch, V.; Hulpke, A.; Mnich, J.; Nickel, W.; Pfeiffer, G. Groups Algorithms and Programming; Lehrshuhl D fur Mathematik, RWTH Aachen: Aachen, Germany, 1994. [Google Scholar]
- Capani, A.; Niesi, G. Cocoa User’s Manual (v. 3.0 b); Department of Mathematics, University of Genova: Genova, Italy, 1996. [Google Scholar]
- Van Leeuwen, M. Lıe, a software package for lie group computations. Euromath. Bull.
**1994**, 1, 83–94. [Google Scholar] - Kurz, T.L.; Middleton, J.A.; Yanik, H.B. A taxonomy of technological tools for mathematics instruction. Contemp. Issues Technol. Teach. Educ.
**2005**, 5, 123–137. [Google Scholar] - Handal, B.; Herrington, A. Mathematics teachers’ beliefs and curriculum reform. Math. Educ. Res. J.
**2003**, 15, 59–69. [Google Scholar] [CrossRef] - Johnson, S.C. Yacc: Yet Another Compiler-Compiler; Bell Laboratories: Murray Hill, NJ, USA, 1975; Volume 32. [Google Scholar]
- Levine, J.R.; Levine, J.R.; Mason, T.; Brown, D. Lex & Yacc; O’Reilly Media, Inc.: Sebastopol, CA, USA, 1992. [Google Scholar]
- Kodaganallur, V. Incorporating language processing into java applications: A javacc tutorial. IEEE Softw.
**2004**, 21, 70–77. [Google Scholar] [CrossRef] - Viswanadha, S. Java Compiler Compiler (javacc)—The Java Parser Generator. Available online: https://javacc.org/ (accessed on 15 April 2018).
- Ryan, C.; O’Neill, M.; Collins, J. Grammatical Evolution: Solving Trigonometric Identities. In Proceedings of the Mendel, Brno, Czech Republic, 24–26 June 1998. [Google Scholar]
- Donnelly, C.; Stallmen, R. The Bison Manual: Using the Yacc-Compatible Parser Generator, for Bison Version 1.875; GNU Press: Boston, MA, USA, 2004. [Google Scholar]
- Earley, J. An efficient context-free parsing algorithm. Commun. ACM
**1970**, 13, 94–102. [Google Scholar] [CrossRef][Green Version] - Gill, A.; Marlow, S. Happy: The Parser Generator for Haskell; University of Glasgow: Glasgow, Scotland, UK, 1995. [Google Scholar]
- Graver, J.O. The evolution of an object-oriented compiler framework. Softw. Pract. Exp.
**1992**, 22, 519–535. [Google Scholar] [CrossRef] - Hudson, S. Javacup: Lalr Parser Generator for Java; GVU Center, Georgia Institute of Technology: Atlanta, GA, USA, 1996. [Google Scholar]

Component | Syntax Format | Syntax Class | Attributes |
---|---|---|---|

+ | Exp + Exp | Plus | exp1, exp2 |

* | Exp * Exp | Times | exp1, exp2 |

^ | Exp ^ Exp | Power | exp1, exp2 |

Sin | Sin(Exp) | Sin | exp |

Cos | Cos(Exp) | Cos | exp |

Numbers | n | Num | n |

Variables | ---- | Var | ---- |

Normal Rules | Combined Rules |
---|---|

< expr >→< term > < expr’ > < expr’ >→(“+” | ”-”) < term > < expr’ > < expr’ >→λ | < expr >→< term > { (“+” | ”-”) < term > }* |

< term >→< unary > < term’ > < term’ >→(“*” | ”/”) < unary > < term’ > < term’ >→λ | < term >→ < unary >{ (“*” | ”/”) < unary >}* |

Grammar Rule | Corresponding Method |
---|---|

< start >→< expr > $ | void parse() : { } { expr() (<EOF> | <EOL>) } |

< expr >→< term > { (“+” | ”-”) < term > }* | void expr() : { }{ term()( <PLUS> term() | <MINUS> term() )* } |

< power >→< element > (“^”< power >)? | void power() : { }{ element() ( <POWER> power() )? } |

Class Name | Print Method |
---|---|

Exp | public String Print() { return ““; } public static String Out(String str) { if( str.contains(“-”) || str.contains(“+”) || str.contains(“*”) || str.contains(“/”) ) str = “(“ + str + “)”; return str; } |

Plus | public String Print() { String a = exp1.print(); String b = exp2.print(); return Exp.Out(a) + “+” + Exp.Out(b); } |

Minus | public String Print() { String a = exp1.print(); String b = exp2.print(); return Exp.Out(a) +”-” + Exp.Out(b); } |

Times | public String Print() { String a = exp1.print(); String b = exp2.print(); return Exp.Out(a) + “*” + Exp.Out(b); } |

Functions | public String Print() { return “sin(“ + exp.Print() + “)”; } |

Num | public String Print() { return Integer.toString(num); } |

Class Name | LaTex Method |
---|---|

Exp | public String LaTex() { return ““; } public static String Out(String str) { if( str.contains(“-”) || str.contains(“+”) || str.contains(“*”) || str.contains(“/”)) str = “(“ + str + “)”; return str; } |

Plus | public String LaTex() { String a = exp1.LaTex(); String b = exp2.LaTex(); return Exp.Out(a) + “+” + Exp.Out(b); } |

Divide | public String Print() { String a = exp1.LaTex(); String b = exp2.LaTex(); return “\\frac {“ + a + “} {“ + b + “}”; } |

Power | public String LaTex() { String a = exp1.LaTex(); String b = exp2.LaTex(); return “(“ + a + “) ^ {“ + b + “}”; } |

Functions | public String LaTex() { return “\\sin {“ + exp.Print() + “}”; } |

Num | public String LaTex() { return Integer.toString(num); } |

Class Name | MathML Method |
---|---|

Exp | public String MathML() { return ""; } |

Plus | public String MathML() { String a = exp1.MathML(); String b = exp2.MathML(); return "<mi>" + a + "<mo>+</mo>" + b + "</mi>"; } |

Divide | public String MathML() { String a = exp1.MathML(); String b = exp2.MathML(); return "<mfrac>" + a + b + "</mfrac>"; } |

Times | public String MathML() { String a = exp1.MathML(); String b = exp2.MathML(); "<mi>" + a + "*" + b + "</mi>"; } |

Functions | public String MathML() { return "<mi>sin</mi>" + "<mi>" + exp.MathML() + "<mi>"; } |

Num | public String MathML() { return "<mn>" + Integer.toString(num) + "<mn>"; } |

Raw View | Rendered View |
---|---|

<math xmlns="http://www.w3.org/1998/Math/MathML"> <mfrac> <mrow> <mo>(</mo> <mn>3</mn> <mi>sin</mi> <mo>(</mo> <mi>x</mi> <mo>+</mo> <mn>1</mn> <mo>)</mo> <mo>)</mo> </mrow> <mn>2</mn> </mfrac> </math> | $\frac{\left(3\mathrm{sin}\left(x+1\right)\right)}{2}$ |

Original Term | Object Tree | Simplification Results |
---|---|---|

0 + exp | Plus (Num (0), exp) | exp |

exp + 0 | Plus (exp, Num (0)) | exp |

0 − exp | Minus (Num (0), exp) | Times (Num (−1), exp) |

exp − 0 | Minus (exp, Num (0)) | exp |

0 * exp | Times (Num (0), exp) | Num (0) |

exp * 0 | Times (exp, Num (0)) | Num (0) |

1 * exp | Times (Num (1), exp)) | exp |

exp * 1 | Times (exp, Num (1)) | exp |

0/exp | Divide (Num (0), exp)) | Num (0) |

exp/1 | Divide (exp, Num (1)) | exp |

Class Name | Corresponding Method |
---|---|

Exp | public abstract Exp Simplify(); |

Plus | public Exp Simplify() { Exp e1 = exp1.Simplify(); Exp e2 = exp2.Simplify(); if (e1.eval().equals(“0”)) return e2; if (e2.eval().equals(“0”)) return e1; return new Plus(e1, e2); } |

Times | public Exp Simplify() { Exp e1 = exp1.Simplify(); Exp e2 = exp1.Simplify(); if (e1.eval().equals(“0”)) return new Num(0); if (e2.eval().equals(“0”)) return new Num(0); if (e1.eval().equals(“1”)) return e2; if (e2.eval().equals(“1”)) return e1; return new Times(e1, e2); } |

Sin | Public Exp Simplify() { return new Sin(exp.Simplify); } |

Variable Name | Usage |
---|---|

isSkipSimplifying | Status of simplification |

isExapnding | Expand algebraic formulas |

isSqrtRemoveal | Remove radicals in denomination |

isPowerSimplify | Exponential simplification |

isMergeFractions | Merge fractions |

……… |

Expression | Matlab | A Real Person | Our System |
---|---|---|---|

x + 3x + 2 | 4x + 2 | 4x + 2 | 4x + 2 |

2x(x^{2} + 2x + 1) | 2x(x + 1)^{2} | 2x^{3} + 4x^{2} + 2x | 2x^{3} + 4x^{2} + 2x |

(2x − 1)(3x + 1) | (2x − 1)(3x + 1) | 6x^{2} − x − 1 | 6x^{2} − x − 1 |

${({({2}^{a})}^{b})}^{3}$ | ${({2}^{a})}^{3b}$ | 2^{3ab} | 2^{3ab} |

${2}^{({a}^{({b}^{3})})}$ | ${2}^{({a}^{({b}^{3})})}$ | ${2}^{({a}^{({b}^{3})})}$ | ${2}^{({a}^{({b}^{3})})}$ |

$\frac{{x}^{2}-2x+1}{x-1}$ | x − 1 | x − 1 | x − 1 |

Expression | Number of Terms | Step-by-Step Solution | Fast Solution | ||
---|---|---|---|---|---|

Recalls | Average | Recalls | Average | ||

x + 3x + 2 | 3 | 5 | 1.67 | 3 | 1 |

2x(x^{2} + 2x + 1) | 4 | 18 | 4.5 | 13 | 3.25 |

(2x − 1)(3x + 1) | 4 | 16 | 4 | 11 | 2.75 |

${({({2}^{a})}^{b})}^{3}$ | 3 | 5 | 1.67 | 3 | 1 |

${2}^{({a}^{({b}^{3})})}$ | 3 | 3 | 1 | 3 | 1 |

$\frac{{x}^{2}+2x-1}{x-1}$ | 5 | 9 | 1.8 | 4 | 0.8 |

© 2018 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Hosseinpour, S.; Alavi Milani, M.M.R.; Pehlivan, H. A Step-by-Step Solution Methodology for Mathematical Expressions. *Symmetry* **2018**, *10*, 285.
https://doi.org/10.3390/sym10070285

**AMA Style**

Hosseinpour S, Alavi Milani MMR, Pehlivan H. A Step-by-Step Solution Methodology for Mathematical Expressions. *Symmetry*. 2018; 10(7):285.
https://doi.org/10.3390/sym10070285

**Chicago/Turabian Style**

Hosseinpour, Sahereh, Mir Mohammad Reza Alavi Milani, and Hüseyin Pehlivan. 2018. "A Step-by-Step Solution Methodology for Mathematical Expressions" *Symmetry* 10, no. 7: 285.
https://doi.org/10.3390/sym10070285