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. 【YashanDB知识库】statement级别的触发器在jdbc接口调用executeBatch时被多次触发

    问题现象 某客户使用jdbc接口向yashandb的表A插入数据. 表A上有一个语句级触发器,其内容为在触发时执行alter sequence操作:另外还有一个insert时的行级触发器,其内容为将每 ...

  2. 【YashanDB知识库】表收集统计信息默认阈值引起SQL执行效率差

    [问题分类]性能优化 [关键字]统计信息,阈值,执行计划 [问题描述]表新增87w数据自动收集统计信息任务没有启动导致SQL执行计划变差 [问题原因分析] CUS_REGISTER_READ 数据总量 ...

  3. Patlibc———更快捷的更换libc

    起初是为了简化做pwn题目时,来回更换libc的麻烦,为了简化命令,弄了一个小脚本,可以加入到/usr/local/bin中,当作一个快捷指令 这个写在了tools库(git clone https: ...

  4. JavaScript Bom和Dom的一般性详解

    一.JavaScript的组成 JavaScript的实现包括以下3个部分: ECMAScript(核心) 描述了JS的语法和基本对象. 文档对象模型 (DOM) 处理网页内容的方法和接口 浏览器对象 ...

  5. C++ string类型常用操作

    string类型操作 字符串切割 str.substr(索引,切割的个数)  ->  返回字符串 注意:第二个参数为切割的个数 string buf = "abcdefg"; ...

  6. docker安装及基本的镜像拉取

    docker 使用存储库安装 卸载它们以及相关的依赖项. yum remove docker \ docker-client \ docker-client-latest \ docker-commo ...

  7. ASP.NET Core OData 9的发布,放弃 .NET Framework

    Microsoft 于 2024 年 8 月 30 日宣布推出 ASP.NET Core OData 9 包. 这个新包将ASP.NET Core与.NET 8 OData库保持一致,改变了OData ...

  8. 利用3Dslice提取血管中心线

    1.首先进入官网下载你需要的版本.你也可以安装老版本,我已经用红色框框出来了. 2.开始安装,等个几十秒钟就ok了. 3.当然要实现提取中心线,还需要 VMTK 这个玩意, 打开应用,找到 insta ...

  9. Pytorch 实现 GAN 网络

    Pytorch 实现 GAN 网络 原理 GAN的基本原理其实非常简单,假设我们有两个网络,G(Generator)和D(Discriminator).它们的功能分别是: G 是一个生成网络,它接收一 ...

  10. .Net 中带有 ? 的运算符

    // 带 ? 的表达式 // 1. 三元表达式 // 2. ?? 双问号 // obj1 ?? obj2 如果 obj1 为 空(null) 返回 obj2 // Configure the HTTP ...