Eternal21
Eternal21

Reputation: 4664

How to parse AND/OR input in c#?

I want to parse an SQL-like input, for example:

SEX = 'MALE' AND AGE > 20

Based on the 'sql' input I then search through my c# array of Items.

So for example above, I would search my data for all items that have their Item.Sex property set to 'MALE' and their Item.Age property is greater than 20.

This is a trivial example, and you can easily start imagining more complex scenarios. For example:

(SEX = 'MALE' AND AGE > 20) OR (SEX = 'FEMALE' AND AGE < 30)

I need to be able to support the following operators:

=
>
<
<>
() - for precedence
AND
OR

I have a feeling I'll end up having to code this myself from scratch, but don't want to re-invent the wheel. After looking into this for a bit, I did come across references to parsers/grammar etc, but am not quite sure if those fit the bill.

Upvotes: 0

Views: 259

Answers (1)

erikkallen
erikkallen

Reputation: 34401

You will probably need to parse the input yourself, but with a parser generator such as ANTLR, it is not too much work.

Upvotes: 1

Related Questions