-
-
Save virtualsafety/9d27a62ab615c9425d830fb35a4d5b76 to your computer and use it in GitHub Desktop.
(一)idea配置ANTLR plugin | |
https://blog.dgunia.de/2017/10/26/creating-and-testing-an-antlr-parser-with-intellij-idea-or-android-studio/ | |
https://qiita.com/kazy/items/ad99142111bba2f0e708(kotlin版) | |
(二)简单例子 | |
--Hello.g4 | |
grammar Hello; | |
main: 'Hello ' name '!'; | |
name: ANY+; | |
ANY: .; | |
--Hello.kt | |
package antlrdemo | |
import com.huawei.antr.* | |
import org.antlr.v4.runtime.CommonTokenStream | |
import org.antlr.v4.runtime.CharStreams | |
fun main(args: Array<String>) { | |
val text = "Hello world!" | |
val source = CharStreams.fromString(text) | |
val lexer = HelloLexer(source) | |
val tokenStream = CommonTokenStream(lexer) | |
val parser = HelloParser(tokenStream) | |
val name = parser.main().name().text | |
println(name) | |
} | |
(三)问题 | |
(1)Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with UUID 59627784-3be5-417a-b9eb-8131a7286089 (expected aadb8d7e-aeef-4415-ad2b-8204d6cf042e or a legacy UUID). | |
ANTLR plugin版本和项目中maven依赖的antlr版本不一致 | |
https://www.cnblogs.com/dsj2016/p/9575712.html |
https://zhenglinj.github.io/technology/2017/08/05/antlr4-maven-examples/
ParseTreeProperty
https://github.com/tcoenraad/compiler-construction/blob/master/block-2/pp/block2/cc/antlr/Calculator.java
https://blog.csdn.net/qq_37771475/article/details/106546742
https://stuff.mit.edu/afs/athena/software/antlr_v4.5/code/listeners/TestLEvaluatorWithProps.java
重点关注如何使用ParseTreeProperty
升级到版本4.9.2,同时只生成listner模式的代码,解决了listener模式下不进入exitXXX函数的问题;
antlr4 -o D:/IdeaProject/IdeaProjects/antlrdemo\gen -listener -no-visitor -lib D:/IdeaProject/IdeaProjects/antlrdemo D:/IdeaProject/IdeaProjects/antlrdemo\Calc.g4
Why there's no grammar for PostgreSQL?
antlr/grammars-v4#1501
https://github.com/pgcodekeeper/pgcodekeeper/blob/master/apgdiff/antlr-src/SQLParser.g4
Grammar Structure
https://github.com/antlr/antlr4/blob/master/doc/grammars.md
import org.antlr.v4.runtime.ParserRuleContext
override fun exitEveryRule(ctx: ParserRuleContext ) {
println("test")
println(ctx.javaClass)
}
https://stackoverflow.com/questions/41180399/modify-expressions-generated-by-antlr
https://stackoverflow.com/questions/2228810/how-can-i-modify-the-text-of-tokens-in-a-commontokenstream-with-antlr
重写输入流
https://www.bookstack.cn/read/antlr4-short-course/rewriting-input-stream.md
https://www.antlr.org/api/Java/org/antlr/v4/runtime/TokenStreamRewriter.html
WS
: [ \t\r\n] + -> channel(HIDDEN)
;
将词法符号送入不同通道
https://blog.csdn.net/pourtheworld/article/details/108293630
https://blog.csdn.net/pourtheworld/article/details/108430250
An arithmetic expressions evaluator with Kotlin and ANTLR4
https://flavluc.github.io/blog/kotlin-antlr/
ANTLR4 Language Target, Runtime for Go
https://github.com/antlr/antlr4/blob/master/doc/go-target.md
https://abcdabcd987.com/notes-on-antlr4/
ANTLR 4 Runtime 提供了一个 ParseTreeProperty ,其实大致就是个 IdentityHashMap。你可以把 Context 当作 key 把相关的东西丢进去。