ANTLR4中是否有相当于“片段"标记的解析器? [英] Is there a parser equivalent of 'fragment' marking in ANTLR4?

查看:83
本文介绍了ANTLR4中是否有相当于“片段"标记的解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉ANTLR4内联解析器规则?

Is there a way to tell ANTLR4 to inline the parser rule?

具有这样的功能似乎是合理的.在阅读了有关ANTLR的书(权威ANTLR 4参考" )之后,我没有发现这种可能性,但是四年来可能已经引入了一些变化 由于该书已发行,所以我想最好在这里问一下.

It seems reasonable to have such feature. After reading the book on ANTLR ("The Definitive ANTLR 4 Reference") I haven't found such possibility, but changes might've been introduced in the 4 years since the book was released, so I guess it is better to ask here.

请考虑以下语法:

file: ( item | class_decl )*;
class_decl: 'class' class_name '{' type_decl* data_decl* code_decl* '}';
type_decl: 'typedef' ('bool'|'int'|'real') type_name;
const_decl: 'const' type_name const_name;
var_decl: 'var' type_name var_name;
...
fragment item: type_decl | data_decl | code_decl;
fragment data_decl: const_decl | var_decl;
fragment code_decl: function_decl | procedure_decl;
fragment class_name: ID;
fragment type_name: ID;
fragment const_name: ID;
fragment var_name: ID;

标记为碎片的规则用于清晰/文档化和可重用性,但是从语法的角度来看,它是实际上,var_declfileclass_decl的实际直接元素,我希望将其反映在解析器创建的上下文的内容中.为itemdata_decl等创建的所有中间上下文都是多余的,不必要地占用了空间并使访问者绑定到语法的组织结构而不是其实际含义.

The rules marked as fragment are there for clarity/documentation and reusability, however from syntax point of view it is f.e. really a var_decl that is actual direct element of file or class_decl and I'd like to have it reflected in content of contexts created by the parser. All the intermediate contexts created for item, data_decl etc. are superfluous, needlessly take space and make it so visitor is bound to organizational structure of the grammar instead of its actual meaning.

总而言之-我希望ANTLR在生成解析器之前将上述语法转换为以下语法:

To sum up - I'd expect ANTLR to turn the above grammar into the following before generation of a parser:

file: ( type_decl | const_decl | var_decl | function_decl | procedure_decl | class_decl )*;
class_decl: 'class' ID '{' type_decl* ( const_decl | var_decl )* ( function_decl | procedure_decl )* '}';
type_decl: 'typedef' ('bool'|'int'|'real') ID;
const_decl: 'const' ID ID;
var_decl: 'var' ID ID;
...

推荐答案

不,解析器规则中没有这样的东西.您可能会在ANTLR的Github存储库中提出这样的问题/RFE: https://github.com/antlr/antlr4/issues

No, there is no such thing in parser rules. You could raise an issue/RFE in ANTLRs Github repo for such a thing: https://github.com/antlr/antlr4/issues

这篇关于ANTLR4中是否有相当于“片段"标记的解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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