The Definitive ANTLR 4 Reference

原文:https://github.com/joaoBordalo/feup-COMP/blob/master/The Definitive ANTLR 4 Reference.pdf

ANTLR V4介绍

  • 新的解析技术:Adaptive LL() or ALL()
  • 简化对expression的语法表达
  • 自动将左递归规则重写为等价的非左递归规则
  • parse-tree walker: listener and visitor pattern
  • 相比v3的一些重要改进:
    • embedding actions作为高级应用,更推荐Listern和Visitor
    • 不需要设计tree grammer,使用自动生成的parse tree and tree walker
    • ALL() better than LL()

ANTLR v4 is the result of a minor detour (twenty-five years) I took in graduate
school. I guess I’m going to have to change my motto slightly.
Why program by hand in five days what you can spend twenty-five years of your
life automating?
ANTLR v4 is exactly what I want in a parser generator, so I can finally get
back to the problem I was originally trying to solve in the 1980s. Now, if I
could just remember what that was.

Chapter 1. Meet ANTLR

Chapter 2. The Big Picture

  • Language的定义:a language is a set of valid sentences, a sentence is made up of phrases, and a phrase is made up of subphrases and vocabulary symbols
  • Interpreter的定义:计算或者执行sentences的程序
  • Translator的定义:
  • parser就像只有一个入口,一个出口,地上写有单词的迷宫
  • ambiguous如何处理
  • ANTLR的基本数据结构和重要类有哪些?作用是什么?
    • CharStream->Lexer->Token(TokenStream)->Parser->ParseTree
    • ParseTree: RuleNode + TerminalNode
    • ANTLR实现了很多RuleNode的子类,比如StatContext, AssignContext...
  • 如何遍历ParseTree?
    • 使用ParseTreeWalker
    • Listener and Visitor
    • Listener如何工作?
      • ParseTreeListener提供enter和exit方法。使用DFS遍历parseTree时,在对应的节点会触发对enter和exit方法的调用
    • Visitor如何工作?

posted @ 2022-02-15 00:33  rxh1999  阅读(416)  评论(0)    收藏  举报