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知识库】使用select * 创建的物化视图无法进行查询重写

    问题现象 使用如下语句准备测试数据: alter system set query_rewrite_enabled=force scope=both; drop table test; create ...

  2. LLM

    LangChain 是啥,能干啥? AutoChain 又是啥 近年来的LLM模型 LLM 都是基于transformers 结构的,具体又分为 Encoder-only(Autoencoding), ...

  3. 一文看懂global, nonlocal, local变量

    Python中,变量是根据程序运行顺序进行的,比如函数外的变量,在函数内是可见的,但是可用,不可赋值.那么如何实现赋值呢? 利用global关键字进行. 在函数内,如果出现和函数外的变量同名变量,而且 ...

  4. 【笔记】前端人脸检测之clmtrackr.js的使用

    clmtrackr.js使用示例代码 html代码: <div class="video-con"> <video id="video" pl ...

  5. Google – Cloud Translation API

    前言 通常网站内容翻译,我们都不推荐使用 Google Translate.但网站中一些不那么重要的内容确实可以用 Google Translate.比如 Customer Reviews. 这篇是续 ...

  6. JavaScript – 理解 Function, Object, Prototype, This, Class, Mixins

    前言 JavaScript (简称 JS) 有几个概念 Object, Prototype, This, Function, Class 是比较难理解的 (相对其它语言 C# / Java 而已),这 ...

  7. thinkphp在原字段上面进行加减操作

    经常有需要对某个数据表的计数字段进行加减操作,我们来看下在ThinkPHP中的具体使用办法. 最简单的,使用下面方法对score自加,第二个参数也可以不要,默认加1:   M('User')-> ...

  8. 技术分享 | 徐轶韬:从MySQL5.7升级到MySQL 8.0

    在6月20日举办的[墨天轮数据库沙龙-MySQL 5.7 停服影响与应对方案]中,甲骨文MySQL解决方案首席工程师徐轶韬分享了<从MySQL5.7升级到MySQL 8.0>主题演讲,本文 ...

  9. 轨道控制器 threejs

    就是一个可以360转动的相机,通过不断的改变相机的参数 然后渲染达到效果. // 引入相机控件  -- 轨道控制器 // console.log('OrbitControls',OrbitContro ...

  10. Curve 分布式存储在 KubeSphere 中的实践

    Curve 介绍 Curve 是网易开发的现代存储系统,目前支持文件存储 (CurveFS) 和块存储 (CurveBS).现在它作为一个沙盒项目托管在 CNCF. Curve 是一个高性能.轻量级操 ...