简体   繁体   中英

Java to HTML Parser / State Machine

I wish to create a app that translates input java code into HTML formatted java code,

For example:

public class ReadWithScanner

Would become

<span class="public">public</span> <span class="class">class</span> ReadWithScanner

However it gets quite complicated when it comes to parameters and regular expressions. Now I have a bit of time on my hands, and I wish to write my own code parser.

How would I start this? and is there any tutorials or online content to not only help me write this, but understand it.

Thanks

For help with the complexity of parsing, you'll need to rely on the Java Language Specification .

As I seem to recall, Java is an LL(k) language (see here , for instance). However, the Java language, despite all attempts to keep it "compact", is still quite large and complex. The grammar is spread out over the entire document. This is not a project for the faint at heart. You might consider using a Java parsing tool (like Java-front ).

What you need to do is use ANTLR, it already has Java grammars for parsing Java, then you just need to supply your own templates to output whatever you want from the Abstract Syntax Tree you generate with ANTLR.

I think you need a lexical analyzer. I used early the Flex lexical analyzer . It is not too complicated to use. If you need to parse the analyzed text you can use the bison c++ bisoncpp.sourceforge.net/ (C++ konwledge need and linux environment)

If you need a resource for learning about parsers, I can recommend Basics of Compiler Design , which is available as a free download.

It covers more than just parsers, but if you read the first few chapters, you should have a good basic understanding of both lexers and parsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM