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 ~]$ ...
随机推荐
- Java连接MySQL示范
通过驱动连接mysql数据库,驱动得跟源码在一块,如图 先分析这句 3306 是本地端口 students是要连接的数据库的名字 账号和密码,之前已经设置 接下来分析这个数据库内容: 这个要查的表是 ...
- Pipeline流水线通过git拉取Jenkinsfile报错 error: RPC failed; result=22, HTTP code = 404
Pipeline流水线通过git拉取Jenkinsfile报错 error: RPC failed; result=22, HTTP code = 404 在学习共享库时使用通过git拉取jenkin ...
- 超轻量级、支持插件的 .NET 网络通信框架
前言 给大家推荐一个轻量级的.支持插件的综合网络通信库:TouchSocket. TouchSocket 的基础通信功能包括 TCP.UDP.SSL.RPC 和 HTTP.其中,HTTP 服务器支持 ...
- Angular 18+ 高级教程 – 学以致用
前言 读这么多原理,到底为了什么?真实项目中真的会用得到吗? 你正在疑惑 "知识的力量" 吗? 本篇会给一个非常非常好的案例,让你感悟 -- 知识如何用于实战. 记住,我的目的是让 ...
- RxJS 系列 – Error Handling Operators
前言 前几篇介绍过了 Creation Operators Filter Operators Join Creation Operators 这篇继续介绍 Error Handling Operato ...
- CSS – Sass & SCSS
前言 CSS 代码多了就不好管理了, 这是它语法先天的不足. Sass 就是加强它语法的, Sass 为 CSS 引入了一些 JS 语言的特性, 比如 variable, function, para ...
- Angular 学习笔记 (Typescript 版本更新)
记入每一期我会用到的功能 : v4.3 1. getter setter 的类型可以不一样咯, 这个困扰很久了. (interface, object, class 都可以这样用) class T ...
- WiFi基础(四):WiFi工作原理及WiFi接入过程
liwen01 2024.09.16 前言 802.11 无线 WiFi 网有三类帧:数据帧.管理帧.控制帧.与有线网相比,无线 WiFi 网会复杂很多.大部分应用软件开发对 WiFi 的控制帧和管理 ...
- SSD-KD:天翼云&清华出品,最新无原始数据的蒸馏研究 | CVPR'24
无数据知识蒸馏能够利用大型教师网络所学到的知识,来增强较小型学生网络的训练,而无需访问原始训练数据,从而避免在实际应用中的隐私.安全和专有风险.在这方面的研究中,现有的方法通常遵循一种反演蒸馏的范式, ...
- Qt中当程序结束时线程的退出
在Qt程序结束时应该如何退出正在运行的任务子线程? 因个人经验和能力有限,本文仅供参考,有错误或者考虑不完善的地方请各位批评指正. 一.正常情况下如何创建和退出线程 1.继承QThread,重写run ...