String Expression Parser

J

James

Does anybody know of a simple open source parser I can use to do simple
String comparisons that are slightly more involved then a standard
inString, for example with Ands, Ors and Nots in.

For example if the users enter expressions in the format "A && !(B ||
C)" then it should compare a given String to see if it contains the
substring 'A' and neither of the substrings 'B' or 'C'.

Failing that does anybody know a simple way to write such a parser?
 
T

Thomas Weidenfeller

James said:
Does anybody know of a simple open source parser I can use to do simple
String comparisons that are slightly more involved then a standard
inString, for example with Ands, Ors and Nots in.

Instead of such a parser, you could try something completely different
for a start: Java's regular expressions:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
Failing that does anybody know a simple way to write such a parser?

The straight forward way, by hand? Write down the grammar in BNF.
Implement a scanner which generates the tokens. Implement the BNF by
hand, as a recursive-decent parser.

Alternatively, you could bring out the big canon (probably way overdone
for your task). Get some parser generator (also called
compiler-compiler) like JavaCC. Write the grammar and token
specifications in the tool's specific notation, compile it, integrate
the generated code, be done.

/Thomas
 
R

Roedy Green

Does anybody know of a simple open source parser I can use to do simple
String comparisons that are slightly more involved then a standard
inString, for example with Ands, Ors and Nots in.

There are a ton of them. See http://mindprod.com/jgloss/parser.html

I found JavaCC very easy to use. The key is looking at the examples
which are ever so much clearer than the documentation.
 
R

Roedy Green

Does anybody know of a simple open source parser I can use to do simple
String comparisons that are slightly more involved then a standard
inString, for example with Ands, Ors and Nots in.

I remember teaching students how to do this sort of thing in Fortran
back in the 60s.

The big question is, should you write your own tokenizer/parser or
should you go for a big gun?

Advantages of doing it yourself:

1. Small learning curve. You may need to polish up on regex, but the
tools you use are very familiar. see
http://mindprod.com/jgloss/regex.html

2. easier to debug. Since it is your code, you know what it is
supposed to be doing. The generated Java code from a parser is pretty
opaque.

3. Your code will be smaller and faster.

4. You are not reliant on a 3rd party package. Your package in self
contained.

5. Your code is direct. Parser code has to be generated then compiled.

The disadvantages of doing it yourself:

1. you need some at least rudimentary knowledge of how to write a
parser. You may not have that.

2. If you keep adding features, your code will turn into a rat's nest.
It will be much easier to maintain your string language with a parsing
package.

3. You will miss out on all the fun of discovering how packages like
JavaCC work. That is a skill that could come in more generally
useful.

4. With the power of the parsing package, you can go wild in your
String language inventing syntax you would never have dared otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top