ANTLR 获得第一批生产 [英] ANTLR get first production

查看:27
本文介绍了ANTLR 获得第一批生产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ANTLR4,特别是在他们的存储库中可用的 C 语法(语法).语法似乎没有初始规则,所以我想知道如何获得它.事实上,一旦初始化解析器,我就附加了我的侦听器,但是由于我试图解析具有不同代码指令的两个文件,因此出现语法错误:

I'm using ANTLR4 and, in particular, the C grammar available in their repo (grammar). It seems that the grammar hasn't an initial rule, so I was wondering how it's possible to get it. In fact, once initialized the parser, I attach my listener, but I obtain syntax errors since I'm trying to parse two files with different code instructions:

int a;

int foo() { return 0; }

在我的示例中,我使用 "parser.primaryExpression();" 调用解析器,这是g4"文件的第一个产品.是否可以避免调用第一个产品并由 ANTLR 自动获取它?

In my example I call the parser with "parser.primaryExpression();" which is the first production of the "g4" file. Is it possible to avoid to call the first production and get it automatically by ANTLR instead?

推荐答案

除了@GRosenberg 的回答:

In addition to @GRosenberg's answer:

此外,规则枚举(在生成的解析器中)包含每个规则的条目,它们按照它们在语法中出现的顺序,并且第一条规则的值为 0.但是,仅仅因为它是语法中的第一条规则并不意味着它是主要的入口点.只有语法作者知道真正的条目是什么,有时您甚至可能只想使用子规则进行解析,这使得这个决定更加困难.

Also the rule enum (in the generated parser) contains entries for each rule in the order they appear in the grammar and the first rule has the value 0. However, just because it's the first rule in the grammar doesn't mean that it is the main entry point. Only the grammar author knows what the real entry is and sometimes you might even want to parse only with a subrule, which makes this decision even harder.

这篇关于ANTLR 获得第一批生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆