Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a string without regular expressions

I'm currently trying to create a software component that would be able to interprete dynamic strings such as:

%TO_LOWER%(%DELETE_WHITESPACES%("A SAMPLE TEXT"))

Which would result in this string:

asampletext

I would like to be able to define a set of available functions, with semantical parameters, etc. I already know (more or less) how to do it using regular expressions.

My questions are:

  • Is lexing/parsing way better than regexp for such a purpose, or should I just go with regexp and forget about that?
  • Does such a library already exist in Java?
  • Do you know any tutorial showing some sample parsing/lexing algorithms?

Thanks!

like image 662
Nanocom Avatar asked Sep 16 '12 00:09

Nanocom


1 Answers

Is lexing/parsing way better than regexp for such a purpose, or should I just go with regexp and forget about that?

Regexes cannot express a recursive grammar, and your syntax would appear to require a recursive grammar. If this is the case, then regexes simply won't solve the problem.

Does such a library already exist in Java?

This is not a problem that a library would solve. You either need to use a parser generator system (such as Antlr or Javacc) to generate the lexer and parser, or write it / them virtually from scratch. The former approach is probably better ... unless you've taken a Uni-level subject that covers this field, or are prepared to do extensive reading.

Do you know any tutorial showing some sample parsing/lexing algorithms?

Both Antlr and Javacc have extensive tutorial material and examples.

like image 189
Stephen C Avatar answered Nov 20 '22 11:11

Stephen C