tags:

views:

35

answers:

3

I want to write a parser and converter of haml-like languages, to parse them, and convert them into html content.

I found people usually use regular-expression to do this, but we have to write a lot of difficult regular expressions, which is not easy. Is there any tools or libraries to do it? I hope it in java and easy to use.

And, is there any articles about how to write such a parser? Thanks in advance!

+1  A: 

Regular expressions are usually a poor-mans-parser. A regex is not a real parser.

Parsers are usually generated by a parser generator. You specify the language in a specification file and the parser generator will convert this to sourcecode for your parser.

Sjoerd
If I don't use regex, what should I use? For example, to parse and convert haml
Freewind
Simply compare strings and characters. Here is an example: http://github.com/raymyers/JHaml/blob/master/src/main/java/com/cadrlife/jhaml/internal/JHamlParser.java
Sjoerd
A: 

You can use JavaCC. It is a yacc like parser generator. The output is the Java source code for the parser.

athena
thank you, but I found parboiled is very good for my job
Freewind
A: 

After some research and testing, I have to say, parboiled is the best tool for this job.

I have spent one day on the PEG and the good examples parboiled has provided, and another day on writing a simple sass parser. It was so easy and nature. Much easier and clearer than Regex. And the best thing is that I can use only Java to write the program, no external DSL needs to learn.

I want to say thank you very much to the author of parboiled, it's a great tool that I'm looking for.

Freewind