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 ~]$ ...
随机推荐
- 【YashanDB知识库】kettle从DM8的number类型同步到YashanDB的varchar类型,存入是科学计数法形式的数据
[标题]kettle从DM8的number类型同步到YashanDB的varchar类型,存入是科学计数法形式的数据 [问题分类]数据导入导出 [关键字]数据同步,number类型,科学计数法 [问题 ...
- 【JS设计模式笔记】-观察者模式(即发布-订阅模式)(结构型)
发布-订阅模式的作用 比如常见的发送短信就是一个典型的发布-订阅模式,例如,小明.小红去售楼处购买房子,但是售楼处的工作人员告诉小明.小红当前楼盘已经售罄,新楼盘还没有开售,这个时候,小明.小红把自己 ...
- HTTP——响应数据格式
HTTP响应数据格式 状态码分类: 常见的状态响应码:
- 【QT性能优化】QT性能优化之QT6框架高性能统计图框架快速展示百万个数据点曲线图
QT性能优化之QT6框架高性能统计图框架快速展示百万个数据点曲线图 文章目录 百万个数据点的QT统计图运行效果 百万个数据点的QT统计图程序的源代码 QT统计图功能和效果展示 QT统计图模块整体结构 ...
- ES 去重/聚合查询
es 安装版本为 7.14.0 springboot 有默认的版本,防止冲突设置如下 <properties> <java.version>1.8</java.versi ...
- linux内核调试痛点之函数参数抓捕记
1.linux内核调试工具crash并不能直接显示函数参数,而这个对调试又非常重要 下面是工作中一个实际的问题,我们的进程hang在如下一个内核栈中了,通过栈回溯可知是打开了一个nfs3的网盘文件或者 ...
- Kubernetes的Pod调度:让你的应用像乘坐头等舱!
一.Kubernetes 中 Pod 调度的重要性 在 Kubernetes 的世界里,Pod 调度就像是一个繁忙的交通指挥官,负责把小车(也就是我们的 Pod)送到最合适的停车位(节点).调度不仅关 ...
- 16. VUE怎么阻止冒泡
给事件添加 stop 修饰符 ,比如 click.stop ; 补充: 阻止默认行为 prevent 修饰符 ,超链接的跳转,表单的默认提交 : once 修饰符 事件只触发一次 ps:事件修饰符可 ...
- .NET高级调试 - 3.7对象检查
简介 在大多数调试会话中,首先需要检查的项目就是分析应用程序的状态.在确认程序的问题是某种无效状态造成的,我们便需要分析程序是如何变成无效状态的.那么在分析过程中,需要为我们深入了解对象的各种审查方法 ...
- apisix~kafka-logger插件
作用 将http请求与响应的内容发到kafka的topic,以json的形式发送存储 配置相关 log_format为自定义配置字段,添加后,默认的请求响应消息将被覆盖 { "_meta&q ...