简体   繁体   English

如何为Swift语言创建Java ANTLR解析器?

[英]How to create Java ANTLR parser for Swift language?

I'm trying to make AST of Swift sources in Java. 我正在尝试用Java对Swift源代码进行AST。 I also use Windows 我也使用Windows

What I did: 我做了什么:

  1. Downloaded antlr-4.7.1-complete.jar 下载了antlr-4.7.1-complete.jar
  2. Added it to classpath 将其添加到classpath
  3. Created files grun.bat and antlr4.bat , added them to PATH 创建文件grun.batantlr4.bat ,将它们添加到PATH中
  4. Downloaded https://github.com/antlr/grammars-v4/blob/master/swift3/Swift3.g4 下载了https://github.com/antlr/grammars-v4/blob/master/swift3/Swift3.g4
  5. Ran antlr4 -Dlanguage=Java Swift3.g4 Ran antlr4 -Dlanguage=Java Swift3.g4
  6. Parser created a few files (including Java sources) 解析器创建了一些文件(包括Java源代码)
  7. I built antlr4-runtime-4.7.2-SNAPSHOT.jar using those instructions https://github.com/antlr/antlr4/blob/master/doc/building-antlr.md 我使用这些指令https://github.com/antlr/antlr4/blob/master/doc/building-antlr.md构建了antlr4-runtime-4.7.2-SNAPSHOT.jar

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. 但是现在我不明白该如何处理https://github.com/antlr/grammars-v4/blob/master/swift3/src/main/java/SwiftSupport.java ,它也是Swift语法的一部分。 What should I do with generated files? 我应该如何处理生成的文件? Simply add them to my sources, define package and job is done? 只需将它们添加到我的资源中,定义package并完成工作即可? I also see that ANTLR generates only tokens, will I have to build AST by myself? 我还看到ANTLR仅生成令牌,我是否必须自己构建AST? Thanks! 谢谢!

Looks like I did everything good, now I can parse Swift files with code 看起来我做得很好,现在我可以用代码解析Swift文件了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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