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. IIS Reverse Proxy 反向代理

    前言 反向代理是这样的: 2 台 web server, A server, B server A server 是 public 的, 有 domain, 有 SSL (作为 B server 的代 ...

  2. nRF24L01芯片驱动记录

    nRF24L01芯片驱动记录 ​ 学习完了usb,了解了部分元器件的功能以及用途后,打算在端午假期用一天的时间完成一个小目标,不过实际上是花了一天半才成功实现,现将驱动nRF24L01芯片的整个过程记 ...

  3. QT QML实用效果之实现页面切换效果

    简介 本文介绍了如何使用QT QML和JavaScript实现页面动态加载和切换的效果. 文章目录 效果 JavaScript代码:butianyun.js文件 QML代码:主页面 页面A QML代码 ...

  4. 【赵渝强老师】MySQL高可用架构:MHA

    MHA(Master HA)是一款开源的 MySQL 的高可用程序,它为 MySQL 主从复制架构提供了 automating master failover 功能.MHA 在监控到 master 节 ...

  5. AntDesign-Vue Table 查询与分页

    前言 之前的增删改查小 Demo 已经快要进行到最后一步了,这节的任务是将请求数据的方式改为 分页,并且增加 分页条件查询 的功能. 页面布局 <a-table :data-source=&qu ...

  6. 深入理解Redis锁与Backoff重试机制在Go中的实现

    目录 Redis锁的深入实现 Backoff重试策略的深入探讨 结合Redis锁与Backoff策略的高级应用 具体实现 结论 在构建分布式系统时,确保数据的一致性和操作的原子性是至关重要的.Redi ...

  7. C# 的浮点类型 float double 和十进制类型 decimal

    // 浮点型数据 float double(双精度) // float f = 1.1; // ps:写小数的时候只要后面没有加上 f/F 默认是double类型 // 正确的定义 double d ...

  8. 何为ORM框架?和ADO.NET 对比

    百度:ORM(Object Relational Mapping)框架采用元数据来描述对象与关系映射的细节,元数据一般采用XML格式,并且存放在专门的对象一映射文件中.简单理解为一种框架的格式 . 只 ...

  9. 云原生爱好者周刊:在 PaaS 平台上托管 WebAssembly 应用

    云原生一周动态要闻: Knative v1.1 发布 Nocalhost v0.6.12 发布 CircleCI 的企业功能现在免费了 SolarWinds 修复了一个 Serv-U 漏洞 Nvidi ...

  10. 认识JVM

    类加载器 运行时数据区 执行引擎 执行引擎的任务就是将字节码指令解释/编译为对应平台上的本地机器指令 JVM架构图