什么是“碎片"?在ANTLR中是什么意思? [英] What does "fragment" mean in ANTLR?

查看:169
本文介绍了什么是“碎片"?在ANTLR中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ANTLR中,片段是什么意思?

What does fragment mean in ANTLR?

我看过两个规则:

fragment DIGIT : '0'..'9';

DIGIT : '0'..'9';

有什么区别?

推荐答案

一个片段有点类似于内联函数:它使语法更易读且更易于维护.

A fragment is somewhat akin to an inline function: It makes the grammar more readable and easier to maintain.

一个片段永远不会被视为一个标记,它只是用来简化语法.

A fragment will never be counted as a token, it only serves to simplify a grammar.

考虑:

NUMBER: DIGITS | OCTAL_DIGITS | HEX_DIGITS;
fragment DIGITS: '1'..'9' '0'..'9'*;
fragment OCTAL_DIGITS: '0' '0'..'7'+;
fragment HEX_DIGITS: '0x' ('0'..'9' | 'a'..'f' | 'A'..'F')+;

在此示例中,匹配NUMBER总是将NUMBER返回到词法分析器,无论它是否匹配"1234","0xab12"或"0777".

In this example, matching a NUMBER will always return a NUMBER to the lexer, regardless of if it matched "1234", "0xab12", or "0777".

请参阅项目3

这篇关于什么是“碎片"?在ANTLR中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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