views:

59

answers:

1

In my master's I've seen how to write parsers, compilers using ANTLR. But in the real world, often times we have a requirement of parsing and extracting relevant content from a heavy load of in-coming stream data. Each language has it's own regular expression engine which can be conveniently used to parse the data. Alternatively we can write an EBNF grammar and take a slick tool like ANTLR to automatically generate the parser. The latter approach is less error prone and guaranteed to be more reliable than the former (especially in case of some extra spaces, new lines).

I would just like to know what would be the borderline between this 2 world's when one would go and write a whole grammar and generate his own parser vs. one quickly uses the inbuilt language regex engine and rollout a petty parser that can do the work quick enough. Again I am not looking for arguments but trying to analyze to what extent and approach people go for writing parsers.

+1  A: 

If your input stream is processable by a regular expression and it isn't complex, then use a regex. A stream of records where each record has a slot and value can be processed pretty reasonably this way.

If the stream has arbitrarily nested records, doing it by regex is impractical (in fact impossible), and you should switch to using a BNF and parser generator.

Ira Baxter
@Ira Incase of safety critical applications such as financial and healthcare where even a small minor mistake will cause havoc, do u think regex is recommendable at all??. I mean what about reliability even for a simple records?.
A_Var
Why are regular expressions any less reliable than the "if" or any other statement in your langauge, if your langauge is well implemented? In fact, in the case of "safety critical" applications, if you really mean that, you'll have to demonstrate *all* of your code is safe, not just the parts that read the records, and you'll find that much more difficult than whatever you use to do record reading. But I'm not sure I understand "safety critical" and "financial" in the same same sentence; are you serious?
Ira Baxter
Safety critical can be restricted to healthcare rather (well many other too exist like aeronautical etc. ). Sorry finance doesn't fall under the same. my mistake.
A_Var