How to write a parser for haml-like languages?
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!
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.
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.
You can use JavaCC. It is a yacc like parser generator. The output is the Java source code for the parser.
精彩评论