Want to know if there is a way to see the output of java lexical and analysis phase while compiling a java program. We can see the byte code(Virtual machine code) using javap. Is there a way to see the output of Lexical and Analysis Phase? Thanks in advance!
Asked
Active
Viewed 278 times
2
-
You might be able to find a tool that generates an abstract syntax tree. But why do you need this? – Colonel Thirty Two Mar 16 '15 at 14:55
-
I am reading a textbook and there is a question "Show the token classes, or “words”, put out by the lexical analysis phase corresponding to this Java source input: sum = sum + unit ∗ /∗ accumulate sum ∗/ 1.2e-12 ;". The theoretical solution is there in the book, wanted to know if there was any way to find the answer practically. – karma Mar 16 '15 at 14:58
-
1It's a lot more work to do it programmatically than to trace back knowing the rules of operator precedence and assocativity. – Makoto Mar 16 '15 at 16:16
-
1Getting a real parser right is hard. You want to use a tool that has been through trial-by-fire. For a tool and an example of what Java parsing looks like, see http://stackoverflow.com/questions/6376662/would-would-an-ast-abstract-syntax-tree-for-an-object-oriented-programming-lan/6378997#6378997 – Ira Baxter Mar 16 '15 at 17:15
1 Answers
1
If you want to analysis the abstract syntax tree
of the java source file. There are some samples:
- https://today.java.net/pub/a/today/2008/04/10/source-code-analysis-using-java-6-compiler-apis.html#accessing-the-abstract-syntax-tree-the-compiler-tree-api
- http://scg.unibe.ch/archive/projects/Erni08b.pdf
You can debug the first example and you will find the source java file will be loaded into a Trees
object.

chengpohi
- 14,064
- 1
- 24
- 42