简体   繁体   English

ANTLR4-需要对此字符串文字的解释

[英]ANTLR4 - Need an explanation on this String Literals

On my assignment, I have this description for the String Lexer: 在我的作业中,我对String Lexer有以下描述:

"String literals consist zero or more characters enclosed by double quotes ("). “字符串文字包含零个或多个用双引号(“)引起的字符。 Use escape sequences (listed below) to represent special characters within a string. 使用转义序列(下面列出)来表示字符串中的特殊字符。 It is a compile-time error for a new line or EOF character to appear inside a string literal. 新行或EOF字符出现在字符串文字内是编译时错误。

All the supported escape sequences are as follows: 所有受支持的转义序列如下:

\\b backspace \\ b退格键

\\f formfeed \\ f换页

\\r carriage return \\ r回车

\\n newline \\ n换行符

\\t horizontal tab \\ t水平制表符

\\" double quote \\“双引号

\\ backslash \\反斜杠

The following are valid examples of string literals: 以下是字符串文字的有效示例:

"This is a string containing tab \\t" “这是一个包含制表符\\ t的字符串”

"He asked me: \\"Where is John?\\"" “他问我:“约翰在哪里?”

A string literal has a type of string." 字符串文字具有字符串的类型。”

And this is my String lexer: 这是我的String lexer:

STRINGLIT: '"'(('\\'('b'|'t'|'n'|'f'|'r'|'\"'|'\\'))|~('\n'))*'"';

Can anybody check for my lexer if it meets the requirement or not? 有人可以检查我的词法分析器是否符合要求? If it's not, please tell me your correction, I don't really understand the requirement and ANTLR4. 如果不是,请告诉我您的更正,我不太了解这个要求和ANTLR4。

With ANTLR4, instead of writing \\\\ ('b' | 't' | 'n') , you can write \\\\ [btn] . 使用ANTLR4,您可以写\\\\ [btn]而不是写\\\\ ('b' | 't' | 'n') Also, as J Earls mentioned in a comment, you'll want to include the quote in your negated set, as well as the \\r and the literal \\ . 另外,正如J Earls在评论中提到的那样,您希望将引号包括在求反集中以及\\r和文字\\

This ought to do the trick: 这应该可以解决问题:

STRINGLIT
 : '"' ( '\\' [btnfr"'\\] | ~[\r\n\\"] )* '"'
 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM