5

I would like to create an Java source code parsing functionality in C++. The purpose of this application is to generate a syntax tree into the internal data structure so that I am able to walk the data tree structure and analyse it on my own.

Which tools could help me to achive my goal?

melter
  • 71
  • 7
  • 1
    There are many tools for this. Google for parser generators. – Marcelo Cantos Dec 12 '11 at 12:46
  • 2
    The obvious question would be why do you want to do this? You may find it simpler and easier to run `javac` and parse the byte code generated instead. – Peter Lawrey Dec 12 '11 at 12:47
  • Doing things like these helps tremendously in understanding the basic principles of the material you're working on. E.G. I once wrote a full SGML parser, and that helped me answer a lot of questions that I didn't even knew I had. – Mr Lister Dec 12 '11 at 12:55
  • @MarceloCantos So, at first I need to pick up a parser generator? OK. I found Bison and YACC softwares. How about one of these? – melter Dec 12 '11 at 13:04
  • 1
    @PeterLawrey: At the moment I am doing some preliminary research about how could I break the language structure into peaces. This way I am able to understand the Java language better - I think :-). – melter Dec 12 '11 at 13:08
  • If you just want to look at a real Java parse tree, you can see a small one here: http://stackoverflow.com/a/6378997/120163 – Ira Baxter Dec 13 '11 at 08:32
  • @melter: "Break the langauge into pieces"? I thought that was what the grammar did, in effect. – Ira Baxter Dec 18 '11 at 19:13

3 Answers3

3

Perhaps ANTLR could help you, but understanding a Java program requires to load the relevant classes referenced in that program.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Moreover, they may be existing grammars for C++. – Agemen Dec 12 '11 at 12:48
  • @Agemen: I found out that ANTLR is able to generate C++ source code but I have determined that I am not going to use any language that is using virtual machine. :-) – melter Dec 12 '11 at 13:11
1

You could use yacc with this grammar.

Andy
  • 8,870
  • 1
  • 31
  • 39
1

You can also try;

http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/

It has different ports like C#, Java, C++, F#, VB.Net, Oberon etc. and it has a nice language scanner & parser for different langauges. So, you can use it for parsing java from c++.

Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57