Skip to content

Instantly share code, notes, and snippets.

@virtualsafety
Last active May 10, 2022 02:09
Show Gist options
  • Save virtualsafety/9d27a62ab615c9425d830fb35a4d5b76 to your computer and use it in GitHub Desktop.
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
@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

https://github.com/antlr/antlr4/blob/master/doc/listeners.md
See the book for more information on listeners and to learn how to use visitors. (The biggest difference between the listener and visitor mechanisms is that listener methods are called independently by an ANTLR-provided walker object, whereas visitor methods must walk their children with explicit visit calls. Forgetting to invoke visitor methods on a node’s children, means those subtrees don’t get visited.)

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

https://abcdabcd987.com/notes-on-antlr4/
ANTLR 4 Runtime 提供了一个 ParseTreeProperty ,其实大致就是个 IdentityHashMap。你可以把 Context 当作 key 把相关的东西丢进去。

@virtualsafety
Copy link
Author

virtualsafety commented Jul 15, 2021

升级到版本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

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

import org.antlr.v4.runtime.ParserRuleContext
override fun exitEveryRule(ctx: ParserRuleContext ) {
println("test")
println(ctx.javaClass)
}

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

@virtualsafety
Copy link
Author

An arithmetic expressions evaluator with Kotlin and ANTLR4
https://flavluc.github.io/blog/kotlin-antlr/

@virtualsafety
Copy link
Author

ANTLR4 Language Target, Runtime for Go
https://github.com/antlr/antlr4/blob/master/doc/go-target.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment