在C#中,如果词法标记以字母或下划线开头,则识别为标识符或关键字;如果词法标记以数字开头,则识别为数字。
此上下文中的字母不限于[A-Za-z];它可以是char.IsLetter识别的任何Unicode字母。
为了识别数字文字,[0-9]之外的任何字符是否被类似地识别为数字?
发布于 2019-04-17 17:43:30
答案是否定的,但它比这更复杂。
如果你看看the language specification,你就会明白我的意思:
integer_literal
: decimal_integer_literal
| hexadecimal_integer_literal
;
decimal_integer_literal
: decimal_digit+ integer_type_suffix?
;
decimal_digit
: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
;
integer_type_suffix
: 'U' | 'u' | 'L' | 'l' | 'UL' | 'Ul' | 'uL' | 'ul' | 'LU' | 'Lu' | 'lU' | 'lu'
;
hexadecimal_integer_literal
: '0x' hex_digit+ integer_type_suffix?
| '0X' hex_digit+ integer_type_suffix?
;
hex_digit
: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
| 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f';正如您所看到的,+或-不是整数文字规范的一部分,因此对于此规则的开头,它始终需要一个数字( 0-9表示十进制整数,0后跟x表示十六进制整数)。
规则解析比仅仅检查第一个字符来确定要遵循哪个规则要复杂得多。阅读ANTLR前瞻性方法论(LL(*))会让你学到很多。简单地说,整个规则必须解析,否则它将无法解析该规则。这不仅仅是基于规则的开始。
https://stackoverflow.com/questions/55724411
复制相似问题