“碎片"是什么意思在ANTLR中是什么意思? [英] What does "fragment" mean in ANTLR?
本文介绍了“碎片"是什么意思在ANTLR中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
fragment 在 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".
这篇关于“碎片"是什么意思在ANTLR中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文