0

I try to implement a parser and below statements should be found

if @loglevel < 1 or
   @loglevel > 3
begin
   dothis
   dothat
end
else
   foo
   makethis
end

The parser should work with regular expressions and should return

condtion part (=> @loglevel < 1 or @loglevel > 3)

then part (=> everthing between begin dothis dothat end)

else part (=> everthing between else begin ....end)

Of course it should be possible that the code do not contain the else part

Is it possible to do this with on or more reqex's expressions or should I implement a sequenciell "readline parser"

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • 5
    Do a sequential parser. Using regular expression for non-trivial grammars is asking for trouble. The primary reason is the (presumed) possibility of nesting your statements. – Amadan Apr 16 '14 at 07:13
  • 1
    Adding to what Amadan said, using regular expressions is only sane when you have very regular patterns as input. If nesting, recursion, etc were not allowed, regular expressions might work for a non-recursive, finite grammar. It's still a challenge though. –  Apr 16 '14 at 07:16

1 Answers1

0

The comments are correct, you should not use regular expressions for parsing, here is an article describing why it's a bad idea.

You could look at ANTLR for help with your parser. There are is a question here with some info

Community
  • 1
  • 1
Mike H-R
  • 7,726
  • 5
  • 43
  • 65