Are you confused by all the different sections of an Antlr grammar file, wondering what each one does ? Well lets take another look at them, this time using Antlr Studio.

Believe it or not but Antlr grammar files are built to be as close to your java source files as possible. What, you don't see any similarities between your java files and antlr grammars? Well, let
me show you...

Grammar

Everything you put in the header section of the grammar file is placed right on
top of all the java files generated by Antlr. Think of this at the absolute top of your java files, you generally use this section to put the package definition. You can even place some imports that are common in all the generated files.

The action section below that is unique for every grammar you specify in the file.
This is placed right before the actual class specification. For example here we want to import the ArrayList and MyClass for the CalcParser only.

Then we have the grammar specification, which even looks like a class declaration.

Below that is the options section , where you can specify the options for the
grammar. You can press Ctrl+Space in Antlr Studio to see which options are available.

The tokens section is used to specify 'imaginary' tokens which are not declared
in the lexer. These are generally used in TreeParsers to specify 'imaginary nodes.

Another action section. This one puts its stuff 'inside' the class definition.
You generally use it to define some custom methods for your parser.

RULES

A rule definition inside an antlr grammar corresponds to a method definition in the generated java file.

As
you can see , here again we can do everything with a rule , that can be done with a function.We can specify arguments for the rule ,as shown above (int a), even a return value and the exceptions thrown by the rule.

The options section allows us to specify some rule specific options.

We can specify custom exception handlers in the exception section.

Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States, other countries, or
both.

Understanding ANTLR Grammar Files的更多相关文章

  1. 开源解析器--ANTLR

      序言 有的时候,我还真是怀疑过上本科时候学的那些原理课究竟是不是在浪费时间.比方学完操作系统原理之后我们并不能自己动手实现一个操作系统:学完数据库原理我们也不能弄出个像样的DBMS出来:相同,学完 ...

  2. 开源语法分析器--ANTLR

      序言 有的时候,我还真是怀疑过上本科时候学的那些原理课究竟是不是在浪费时间.比方学完操作系统原理之后我们并不能自己动手实现一个操作系统:学完数据库原理我们也不能弄出个像样的DBMS出来:相同,学完 ...

  3. What you should know about .so files

    In its early days, the Android OS was pretty much supporting only one CPU architecture: ARMv5.Do you ...

  4. Introduction to the Standard Directory Layout

    Having a common directory layout would allow for users familiar with one Maven project to immediatel ...

  5. ANTLR3完全参考指南读书笔记[03]

    前言 文中第4章内容有点多,有点枯燥,但不坚持一下,之前所做的工作就白做了. 再次确认一下总体目标: protege4编辑器中Class Definition中语法解析和错误提示: Java虚拟机规范 ...

  6. Android 4.1.1源码编译

    适用环境: 系统:ubuntu-12.04.2-desktop-amd64 JDK:sun-java6-jdk Android源码:android_4.1.1_r1 虚拟机安装: VMware Wor ...

  7. Angle

    1 What is Angle. The goal of ANGLE is to allow Windows users to seamlessly run WebGL and other OpenG ...

  8. 将replicated数据与元数据关联

    本章介绍元数据的用法以及如何将replicated数据与元数据相关联. 12.1概述 将数据从一个表复制到另一个表时,重要的考虑因素是源表和目标表的列结构(元数据)是否相同. Oracle Golde ...

  9. Domato学习

    A DOM fuzzer 转:https://github.com/google/domato Written and maintained by Ivan Fratric, ifratric@goo ...

  10. CentOS6.9上安装FreeSWITCH1.6.19

    安装环境:操作系统:[zhi@Freeswitch ~]$ cat /etc/redhat-release CentOS release 6.9 (Final)[zhi@Freeswitch ~]$ ...

随机推荐

  1. Android 获取当前获取焦点的组件

    在Activity中,使用this.getCurrentFocus(),获取当前焦点所在的View, 再判断是否是EditText(可调整成其他组件),看个人需要再做特定的逻辑处理 String co ...

  2. 事件触发器TRIGGER

    我所理解的事件 MYSQL触发器 就好比是 JavaScript 中的 Object.defineProperty,通过观察某些行为,然后做些事情 创建触发器 DELIMITER $$ CREATE ...

  3. ArgoWorkflow教程(四)---Workflow & 日志归档

    上一篇我们分析了argo-workflow 中的 artifact,包括 artifact-repository 配置以及 Workflow 中如何使用 artifact.本篇主要分析流水线 GC 以 ...

  4. SpringBoot系列:使用原生JDBC实现对数据库的增删改查

    application.yml spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306 ...

  5. std::make_shared

    std::make_shared 是 C++11 引入的一个标准库函数,用于创建一个 std::shared_ptr,并在堆上分配所需的对象. 它的功能是将对象的创建和 shared_ptr 的初始化 ...

  6. 数据库运维实操优质文章文档分享(含Oracle、MySQL等) | 2024年8月刊

    本文为大家整理了墨天轮数据社区2024年8月发布的优质技术文章/文档,主题涵盖Oracle.MySQL.PostgreSQL等主流数据库系统以及国产数据库的技术实操,从基础的安装配置到复杂的故障排查, ...

  7. 在实例化对象的时候new关键字具体做了哪些操作?

    a 创建了一个空对象 {}b 通过原型链把空对象和构造函数连接起来__proto__ = prototype c 构造函数的this指向新对象,并执行函数体 d 判断构造函数的返回值,返回对象就使用该 ...

  8. Android复习(二)应用资源 --> 动画

    没什么好总结的 复制自 https://developer.android.google.cn/guide/topics/resources/animation-resource 有需要的可以查看官方 ...

  9. 蜘点云原生之 KubeSphere 落地实践过程

    作者:池晓东,蜘点商业网络服务有限公司技术总监,从事软件开发设计 10 多年,喜欢研究各类新技术,分享技术. 来源:本文由 11 月 25 日广州站 meetup 中讲师池晓东整理,整理于该活动中池老 ...

  10. Kubernetes 对接 GlusterFS 磁盘扩容实战

    前言 知识点 定级:入门级 使用 Heketi Topology 扩容磁盘 使用 Heketi CLI 扩容磁盘 实战服务器配置 (架构 1:1 复刻小规模生产环境,配置略有不同) 主机名 IP CP ...