How do I make my own parser for java/jsf code?

2019-06-27 23:54发布

Hi I'd like to make my own 'parser', e.g: computing (4+(3-4^2))*2 or parsing java,jsf,html code.

In fact I did something like this but I feel it's not good.

Is there anything good for me? I've tried to read more, but I'm bit confused, LL, LR, AST,BNF,javacc yacc etc :). I'm not sure which way to go, when I would like to compute 4+...

or if I'd like to parse java,jsf code and produce something from this(another java code)

Is there anything generaly good enough like ast? or something which I can use for both?

thank you for help.

标签: java parsing
8条回答
Anthone
2楼-- · 2019-06-28 00:35

Using tools which generate Lexers and Parsers is generally far easier than writing your own from scratch.

In addition to whats already been listed, you could use things like JLex with CUP to create a simple interpreter for things like arithmetic expressions very easily.

查看更多
萌系小妹纸
3楼-- · 2019-06-28 00:45

ANTLR is probably the way to go for java. It is a little intense, the book is apparently very good (I have only struggled with the online docs).

If you can stretch to other languages, then lex/yacc (or flex/bison) is the standard for C although I wouldn't particularly recommend either of those combinations (steep learning curve, showing their age a little now).

Python has about a million parsers available (SimpleParse, Yapps) or there is TreeTop for Ruby - the developer even has a demo that does simple calculations as in your question - but note that this won't do everything that a LALR parser can accomplish.

查看更多
登录 后发表回答