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 ~]$ ...
随机推荐
- mysql 8.0.18 根据.ibd文件和建库SQL恢复数据
前提:执行建库SQL,(包括建表的SQL) 1. 在mysql 的data文件夹中,找到需要恢复的DB名称,清除其文件夹下的所有文件,将待恢复的.ibd文件复制到此文件夹内 2. 执行SQL,然后查询 ...
- Java连接MySQL示范
通过驱动连接mysql数据库,驱动得跟源码在一块,如图 先分析这句 3306 是本地端口 students是要连接的数据库的名字 账号和密码,之前已经设置 接下来分析这个数据库内容: 这个要查的表是 ...
- GitHub Copilot 典型使用场景实践
大家好,我是Edison. 近期我们一直在使用GitHub Copilot协助开发编码工作,总结了一些实际场景的用法,可能在目前网络中很多的博客中都没有提及到,本文一一分享给你. 简介:你的结对编程伙 ...
- react-pdf预览在线PDF的使用
1.在react项目中安装react-pdf依赖包 建议安装8.0.2版本的react-pdf,如果安装更高版本的可能出现一些浏览器的兼容性问题: npm install react-pdf@8.0. ...
- Qml 实现星级评分组件 已发布
[写在前面] 在现代应用程序中,星级评分是一个常见的用户界面元素,它允许用户对产品.服务或内容进行评价. 想必大家在用各种带有评分的软件中看到过这个组件: 本文将指导你如何使用 Qml 创建一个简单而 ...
- httpclient调用接口
有时候会将参数(返回结果)压缩(解压),加密(解密) 将json参数通过GZip压缩 Base64加密 1 public static String gzipAndEncryption(String ...
- Java项目笔记(一)
一.springboot控制台打印sql日志 ---------.mapper为你启动类扫描的mapper路径 logging.level.---------.mapper = debug 二.前端传 ...
- OpenAI 发布适用于 .NET 库的稳定版本
OpenAI 在 6 月发布测试版后发布了其官方 .NET 库的稳定版本.它以 NuGet 包的形式提供,支持 GPT-4o 和 GPT-4o mini 等最新模型,以及完整的 OpenAI REST ...
- USB 控制写传输、控制读传输、无数据控制传输都是在什么场景下?
在 USB 通信中,控制传输(Control Transfer)是一个非常常见且重要的传输类型,主要用于配置设备.查询设备状态以及发送和接收命令.控制传输有三种主要形式:控制写传输(Control W ...
- DDD之领域服务与应用服务
领域服务: 聚合中的实体没有业务逻辑代码,只有对象的创建,对象的初始化,状态管理等个体相关的代码: 对于聚合内的业务逻辑,我们编写领域服务Domain service 而对于聚合协作以及聚合与外部系统 ...