ANTLR首次生产 [英] ANTLR get first production

查看:58
本文介绍了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天全站免登陆