-2

Which is the good way to validate a mathematical variables expression. Whether we can use stack or with regex expression.

I have gone with http://www.sanfoundry.com/java-program-implement-evaluate-expression-using-stacks/

Note: The expression does not contain parenthesis. For example:

Valid:

  • x+3-y/2
  • x+y
  • 2+4+e

Invalid:

  • x++4+8
  • e-3r
  • e+r34-

How to validate the above expressions?I need to validate the mathematic expression input provided by user in java

bharathi
  • 6,019
  • 23
  • 90
  • 152
  • One option would be to use a stack, as is done with reverse polish notation. By the way, `x++4+8` is not invalid if you accept `++` as being an increment operator. – Tim Biegeleisen Nov 08 '16 at 08:01
  • How is this related to Java? –  Nov 08 '16 at 08:01
  • I need to validate the mathematic expression input provided by user in java – bharathi Nov 08 '16 at 08:06
  • Can input contain parentheses? – kgeorgiy Nov 08 '16 at 08:07
  • x++4+8 is a not a proper mathematic expression. Yes in java you can consider a increment operator, but we need to validate a mathematic expression – bharathi Nov 08 '16 at 08:08
  • parentheses wont be allowed as input from user. Just the expression having varaiables and then we need to validate and then pass the values to respective variables – bharathi Nov 08 '16 at 08:09
  • @bharathi unless you allow numbers to have a sign ('+') in this case. Btw. what was the problem when you used the linked program? – Henry Nov 08 '16 at 08:09
  • I will give you a mention , you can use stack to validation all operators so convert to postfix expression then check operator take two element ... for parantesis you can use also stack if { encounter import into stack until encounter } but if there is no { but you encounter with } error – Mithat Konuk Nov 08 '16 at 08:11
  • suppose expression is H:"x + 234 * y * 4 - 2 / z", first I have to push the variables in stack and then check the expression is valid and then pass the input to the x,y,z. and evaluate the result. My understanding is correct? – bharathi Nov 08 '16 at 08:15
  • The source you have cited is identifiable as 100% drivel from about line 33 onwards. For example, it doesn't handle either parentheses or even binary minus. Have a look around for recursive descent expression parsers, or the Dijkstra Shunting-yard algorithm. – user207421 Nov 08 '16 at 08:42
  • Please, add no-parentheses-allowed condition to the question, so the readers will not be confused by the answer. – kgeorgiy Nov 08 '16 at 08:56
  • And now tell us *why* parentheses are not allowed. That's about the first question the users will ask. Don't implement arbitrary restrictions just to suit yourself. I've seen this sort of thing crash and burn too many times. – user207421 Nov 08 '16 at 09:10

1 Answers1

0

If no parentheses (i.e. nesting expressions) are allowed, the validation could be done by simple regex:

final Pattern pattern = Pattern.compile("([0-9]+|[a-z]+)([-+*/]([0-9]+|[a-zAZ]+))*");

for (String s : Arrays.asList("123", "x+3-y/2", "x+y", "2+4+e", "x++4+8", "e-3r", "e+r34-", "---x+3-y/2")) {
    System.out.format("%5s %s\n", pattern.matcher(s).matches(), s);
}

The output is

 true 123
 true x+3-y/2
 true x+y
 true 2+4+e
false x++4+8
false e-3r
false e+r34-
false ---x+3-y/2
kgeorgiy
  • 1,477
  • 7
  • 9
  • Thanks Kgeorgiy. This answer is really helpful for me to start the analysis. Can I know which one is better one to use stack or with regex expression.Any idea? – bharathi Nov 08 '16 at 08:33
  • failed on `---x+3-y/2` – Aroniaina Nov 08 '16 at 08:35
  • Stack-based solution required when nesting expressions are allowed, but as this is not the case the regex solution is much more simplier. – kgeorgiy Nov 08 '16 at 08:36
  • @Aroniana As we see from examples, unary '+' is not allowed, so, probably, unary '-' should not be supported too. – kgeorgiy Nov 08 '16 at 08:38
  • @Aroniana It's pretty strange. I added `---x+3-y/2` to the test set and got `false`, as expected. – kgeorgiy Nov 08 '16 at 08:43
  • You cannot parse a context-free language with a regular-expression technique. – user207421 Nov 08 '16 at 08:43
  • @EJP Without nesting expressions (i.e. parentheses) this language is regular – kgeorgiy Nov 08 '16 at 08:45
  • There is nothing in the question about not using parentheses. – user207421 Nov 08 '16 at 08:51
  • To clarify all, This is a normal mathematic expression which we used solve it in school days. No preincrement and postincrement used here – bharathi Nov 08 '16 at 08:52
  • @EJP See comments under the question itself. I've explicitly asked whether they are allowed and got the positive answer before posting my solution. – kgeorgiy Nov 08 '16 at 08:54