简体   繁体   中英

How to create Java ANTLR parser for Swift language?

I'm trying to make AST of Swift sources in Java. I also use Windows

What I did:

  1. Downloaded antlr-4.7.1-complete.jar
  2. Added it to classpath
  3. Created files grun.bat and antlr4.bat , added them to PATH
  4. Downloaded https://github.com/antlr/grammars-v4/blob/master/swift3/Swift3.g4
  5. Ran antlr4 -Dlanguage=Java Swift3.g4
  6. Parser created a few files (including Java sources)
  7. I built antlr4-runtime-4.7.2-SNAPSHOT.jar using those instructions https://github.com/antlr/antlr4/blob/master/doc/building-antlr.md

But now I don't understand what should I do with https://github.com/antlr/grammars-v4/blob/master/swift3/src/main/java/SwiftSupport.java which is also a part of Swift grammar. What should I do with generated files? Simply add them to my sources, define package and job is done? I also see that ANTLR generates only tokens, will I have to build AST by myself? Thanks!

Looks like I did everything good, now I can parse Swift files with code

Swift3Lexer swiftLexer = new Swift3Lexer(CharStreams.fromFileName(file.getAbsolutePath()));
CommonTokenStream commonTokenStream = new CommonTokenStream(swiftLexer);
Swift3Parser swiftParser = new Swift3Parser(commonTokenStream);

Top_levelContext topLevel = swiftParser.top_level(); // it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM