开发者

Java to HTML Parser / State Machine

开发者 https://www.devze.com 2023-03-10 04:03 出处:网络
I wish to create a app that translates input java code into HTML formatted java code, For example: public class ReadWithScanner

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</spa开发者_运维问答n> 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.


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消