Click here to Skip to main content
15,909,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am a newbie and I apologize for that but I need help.

I want to read an input string Ex1 var1-var != var3
Ex2. var1 + var2 == var3
Ex3. var1 + var2 >= var3


The string will consist of numeric Ex. 201-200!=1 or 300-400<50

The inputs are restricted to integers. I am looking for the easiest way.

How can I verify the input expession?(true or false)

Can I make the expression dynamic so that the number of input variables dosen't matter and I can use + - * / and different operators greater then, equal, less then and so on.

Grateful for all help I can get.

//Nicke
Posted
Updated 2-Jun-11 0:34am
v5
Comments
Sergey Alexandrovich Kryukov 1-Jun-11 16:47pm    
Not clear. Why verify? Is it string, not numeric type?
What's the type anyway?
--SA
yesotaso 1-Jun-11 17:04pm    
This is most probably about custom compiler related stuff, I have hazy memories of LL LR SLR LALR parsers... Even thought of those makes me nauseous.
Sergey Alexandrovich Kryukov 1-Jun-11 20:10pm    
Nah! OP answered my question. Just strings.
--SA
nlarzon 1-Jun-11 17:23pm    
I have a string input. I want to parse the int variables(var 1 &2) and add them(or subtract) and compare them to var3 using the different operators from the string(ex !=)
--N
Sergey Alexandrovich Kryukov 1-Jun-11 20:10pm    
The write about it in your question. Use "Improve question" now.
--SA

Am I missing something, or is all you need a little simple algebra?
For all integers n - (n + 1) is equal to -1, so you expression will always be true, regardless of the value (or datatype) on "n".

Why are you asking this re C#?
 
Share this answer
 
Comments
Jpuckett 1-Jun-11 15:37pm    
I noticed this too, but it's obviously a homework problem, and therefore it's pointless in the real world.
Well, as Bob pointed out the parsing subject is very big one. Also, what you need to do and what you need to show may not always the same ( thats when ad hoc hacking involves :) ) I'd like to give you an outline. First define your rules. Lets say your input is an "expression" then rules might be like:
VB
expression: relational_expression .
relational_expression: a_expression .
relational_expression: a_expression relop a_expression .
a_expression: term .
a_expression: a_expression addop term .
term: factor .
term: term mulop factor .

factor: number .
factor: 'true' .
factor: 'false' .

addop: '+' / '-' .
mulop: '*' / '/' / '%' .
relop: '==' / '!=' / '<' / '>' / '<=' / '>=' .

Note that here operator precedence is taken in to account thats why rule set is a little long. For instance, you do not want to evaluate "400<50" before "300-400" in "300-400<50".
I am not going into details of those rules, one might argue that this is too long, short, juicy or even squishy... Whatever, following those rules you see that first you need to locate comparison operator and evaluate left and right of it, rest is up to you. Note that above rules are just guideline not code. System.Text.RegularExpressions[^] namespace has all the tools you need to chop down input strings.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900