Understanding ANTLR Grammar Files
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的更多相关文章
- 开源解析器--ANTLR
序言 有的时候,我还真是怀疑过上本科时候学的那些原理课究竟是不是在浪费时间.比方学完操作系统原理之后我们并不能自己动手实现一个操作系统:学完数据库原理我们也不能弄出个像样的DBMS出来:相同,学完 ...
- 开源语法分析器--ANTLR
序言 有的时候,我还真是怀疑过上本科时候学的那些原理课究竟是不是在浪费时间.比方学完操作系统原理之后我们并不能自己动手实现一个操作系统:学完数据库原理我们也不能弄出个像样的DBMS出来:相同,学完 ...
- 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 ...
- Introduction to the Standard Directory Layout
Having a common directory layout would allow for users familiar with one Maven project to immediatel ...
- ANTLR3完全参考指南读书笔记[03]
前言 文中第4章内容有点多,有点枯燥,但不坚持一下,之前所做的工作就白做了. 再次确认一下总体目标: protege4编辑器中Class Definition中语法解析和错误提示: Java虚拟机规范 ...
- Android 4.1.1源码编译
适用环境: 系统:ubuntu-12.04.2-desktop-amd64 JDK:sun-java6-jdk Android源码:android_4.1.1_r1 虚拟机安装: VMware Wor ...
- Angle
1 What is Angle. The goal of ANGLE is to allow Windows users to seamlessly run WebGL and other OpenG ...
- 将replicated数据与元数据关联
本章介绍元数据的用法以及如何将replicated数据与元数据相关联. 12.1概述 将数据从一个表复制到另一个表时,重要的考虑因素是源表和目标表的列结构(元数据)是否相同. Oracle Golde ...
- Domato学习
A DOM fuzzer 转:https://github.com/google/domato Written and maintained by Ivan Fratric, ifratric@goo ...
- CentOS6.9上安装FreeSWITCH1.6.19
安装环境:操作系统:[zhi@Freeswitch ~]$ cat /etc/redhat-release CentOS release 6.9 (Final)[zhi@Freeswitch ~]$ ...
随机推荐
- Chrome扩展插件的开发--获取网页Cookies
Chrome扩展插件的开发--获取网页Cookies Chrome浏览器在浏览器类应用软件中一直居于榜首,很多人选择Chrome浏览器不仅仅是因为它的稳定,还有它丰富的可拓展性.那么有没有想自己开发一 ...
- vscode 安装历史版本
修改版本号为想要下载的版本即可 https://update.code.visualstudio.com/{版本}/win32-x64-archive/stable vscode 历史版本地址:Vis ...
- NDP 协议
Ref: http://ipv6.infosws.cn/20201009/40639.html IPv6系列基础篇(下)--邻居发现协议NDP IPv6之基础协议(3)讲NDP 里面NS, NA 报文 ...
- 携手华为云WeLink,合合信息旗下名片全能王推动人脉管理数智化升级
名片是商务场景中信息传递的重要载体.在无纸化办公日益兴盛的当下,数字名片逐渐被广大职场人士接受,成为商务交流的新方式.近期,合合信息旗下名片全能王与华为云WeLink联合研发,升级数字名片" ...
- ASP.NET Core – Work with Environment (Development, Staging, Production)
前言 这篇讲一讲发布和环境 (development, staging, production) 介绍 我的网站是 host 在 Azure 的 Virtual Machine, 跑 IIS, 没有使 ...
- 【赵渝强老师】阿里云大数据ACP认证之阿里大数据产品体系
阿里大数据产品体系是基于阿里云飞天平台上的数据处理服务.主要分为阿里云大数据基础产品和阿里云数加平台,其产品架构图如下所示: 一.阿里云大数据基础产品 1.云数据库--RDS(ApsaraDB for ...
- 架构与思维:漫谈高并发业务的CAS及ABA
1 高并发场景下的难题 1.1 典型支付场景 这是最经典的场景.支付过程,要先查询买家的账户余额,然后计算商品价格,最后对买家进行进行扣款,像这类的分布式操作, 如果是并发量低的情况下完全没有问题的, ...
- 关于Transformer中feed forward layer理解
今天记录一下个人关于Transformer中前馈神经网络(FFN)的一点理解. 第一点,我们知道,FFN之前是有一个多头注意力机制的模块.我们可以想想一下,如果这个模块直接输出,不经过FFN层会发生什 ...
- 活动预告 | 中国数据库联盟(ACDU)中国行定档深圳,一起揭秘数据库前沿技术
在当今数字化时代,数据库是各行各业中最核心的信息管理系统之一.随着技术的飞速发展,数据库领域也不断涌现出新的前沿技术和创新应用.数据库运维和开发人员需要紧跟前沿技术,才能保持竞争力,并实现更高效.更智 ...
- 从零开始学机器学习——构建一个推荐web应用
首先给大家介绍一个很好用的学习地址:https://cloudstudio.net/columns 今天,我们终于将分类器这一章节学习完活了,和回归一样,最后一章节用来构建web应用程序,我们会回顾之 ...