Skip to content

Algebra Syntax Definition

Nateowami edited this page Aug 13, 2015 · 5 revisions

There has been a great need to formally define the grammar for algebra accepted by Solve4x. I say "great need" because I've had to constantly rewrite the solver because I wrote it wrong.

Note to myself and everyone else: don't even think about starting a project like this without making a definition of the grammar supported, even if you know what's accepted. You still have define what is what. Think you've thought everything through? Fat chance. 😝

Equation

Two expressions, delimited by an = sign. It has two children, both of which must be AlgebraicParticles (not necessarily expressions). Equations are represented by the class com.github.nateowami.solve4x.solver.Equation.

Expression

A list of algebraic particles each beginning with a - or + sign (unless it is the first, in which cast the + may be inferred) with length 2 or greater. Not all + and - signs (hereafter called simply "signs") mark the beginning of a new term. In particular, an expression may start with one, but may never end with one. Secondly, if a sign is nested in parentheses, it should not be taken into account. Expressions are represented by the class com.github.nateowami.solve4x.solver.Expression.

Term

A list of algebraic particles. Decimals, expressions, fractions, roots, and variables, (aka algebraic particles) will be handled by their respective classes, each inheriting from AlgebraicParticle. Multiplication by each of theses is implied, because they are all members of the same term. Terms will be represented by the class com.github.nateowami.solve4x.solver.Term.

Algebraic Particle

A term, decimal, expression, fraction, root, or variable, optionally followed a superscript exponent. The ambiguity is is due to the fact that algebraic particles will be the super class of the mentioned miscellaneous particles. An algebraic particle should be able to tell you whether a string can be parsed as an algebraic particle by calling the class's parseable() method. It will then query all the sub-classes' respective static parseable() methods to see if it is a valid algebraic particle. The same goes for constructing algebraic particles. Call the getInstance() method of AlgebraicParticle and it will find the correct sub-class that can construct it. Algebraic particles are represented by the class com.github.nateowami.solve4x.solver.AlgebraicParticle.

Number

At least one digit (may only start with 0 digit if number of digits in this section is 1), optionally followed by a '.' and one or more digits. Kinda wordy, but you get the idea. Needs at least one digit before the decimal sign; if there's a decimal sign at least one digit following it. Decimals are represented by the class com.github.nateowami.solve4x.solver.Number.

Fraction

Two algebraic particles each surrounded by parentheses and separated by a fraction sign (/). That's really all there is too it. Fractions are represented by com.github.nateowami.solve4x.solver.Fraction.

Root

An (optional) non-zero non-zero-starting positive subscript followed by and an algebraic particle surrounded by parentheses. Roots are represented by com.github.nateowami.solve4x.solver.Root.

Variable

Any single letter a to z or A-Z. As with all other algebraic particles, it it can have a positive/negative sign and a power which is held in its superclass (AlgebraicParticle). Variables are represented by com.github.nateowami.solve4x.solver.Variable.

MixedNumber

A whole number followed by a fraction of which top and bottom contain whole numbers, and the top number is larger than the bottom number. All three numbers must be positive. For example, 5(6)/(7). If you want a negative mixed number, make the whole thing negative, not one of the numbers. MixedNumbers are represented by com.github.nateowami.solve4x.solver.MixedNumber.

This is certainly not completely unambiguous, but it answers a lot of the uncertainties I had while working on this part of the solver.

Clone this wiki locally