7

I am working on a static analysis tool and I need to work with Java source code and I'd like to write my code in Scala so that I can benefit from its functional features.

Is there any easy way to parse Java source code and have an Abstract Syntax Tree with Scala? I thought at using some existing Java parsers written in Java but I wanted to make sure that I'm not missing some cool Scala library

8
  • Scala's parser combinator can be your choice Commented Mar 28, 2013 at 15:22
  • Is there any already implemented Java parser? Commented Mar 28, 2013 at 15:24
  • 1
    I would not say it will be easy to create a tool for analyzing Java source code at a semantic level. There's far more to it than parsing, for one thing. Commented Mar 28, 2013 at 15:44
  • 1
    The point is that an Abstract Syntax Tree is not purely syntactic. It cannot be built properly without creating a proper symbol table for program, and this is effectively a global analysis. With separate compilation, the information comes either from analyzing source files or from .class files. Commented Mar 28, 2013 at 16:01
  • 1
    If you want to accept huge amounts of non-Java (Java look-alike) code, perhaps. Commented Mar 28, 2013 at 16:09

1 Answer 1

1

I thought Scala could call Java libraries.

You should be able to call any Java-implemented Java parser (e.g., ANTLR). That way you get a mature parser.

Sign up to request clarification or add additional context in comments.

2 Comments

In doing a little background study on this question, I came across a Java analysis tool written with ANTLR. That would surely be a better starting point than a blank slate! It is described here: svn.apache.org/repos/asf/maven/sandbox/trunk/jxr/maven-jxr/…
Yes, Scala can call Java libraries, as I reported in the question. I was just wondering if there is anything "better".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.