如何在JavaCC中使用反斜杠转义字符为新行? [英] How to use backslash escape char for new line in JavaCC?

查看:163
本文介绍了如何在JavaCC中使用反斜杠转义字符为新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务来创建一个词法分析器,除了一点之外我有一切工作。
我需要创建一个接受一个新行的字符串,并且该字符串用双引号分隔。
字符串在分隔符中接受任何数字,字母,一些指定的标点符号,反斜杠和双引号。
我似乎无法弄清楚如何逃避一个新的行字符。
有某种方式可以像新行和标签一样转义字符吗?

I have an assignment to create a lexical analyser and I've got everything working except for one bit. I need to create a string that will accept a new line, and the string is delimited by double quotes. The string accepts any number, letter, some specified punctuation, backslashes and double quotes within the delimiters. I can't seem to figure out how to escape a new line character. Is there a certain way of escaping characters like new line and tab?

这里有一些可能有助于

< STRING : ( < QUOTE> (< QUOTE > | < BACKSLASH > | < ID > | < NUM > | " " )* <QUOTE>) >
< #QUOTE : "\"" >
< #BACKSLASH : "\\" >

所以我的字符串应该允许一个报价,然后任何以下字符,如反斜杠,空格,数字等,然后跟着另一个引号
换行符像\
提前感谢

So my string should allow for a quote, then any of the following characters like a backslash, a whitespace, a number etc, and then followed by another quote. The newline char like "\n" is what's not working. Thanks in advance!

推荐答案

对于字符串文字,JavaCC借用Java的语法因此,包含回车的单字符文字会以\r的形式进行转义,并将包含换行符的单字符文字转义为 \\\

For string literals, JavaCC borrows the syntax of Java. So, a single-character literal comprising a carriage return is escaped as "\r", and a single-character literal comprising a line feed is escaped as "\n".

然而,处理的字符串值只是一个字符;它不是转义本身。所以,假设你定义换行标记:

However, the processed string value is just a single character; it is not the escape itself. So, suppose you define a token for line feed:

< LF : "\n" >

令牌< LF> 的匹配将成为单行代码字符定义另一个令牌,单个字符被有效地替代。所以,假设你有更高层次的定义:

A match of the token <LF> will be a single line-feed character. When substituting the token in the definition of another token, the single character is effectively substituted. So, suppose you have the higher-level definition:

< STRING : "\"" ( <LF> ) "\"" >

令牌匹配< STRING> 将是三个字符:引号,后跟换行,后跟引号。你似乎想要的是让转义序列被识别:

A match of the token <STRING> will be three characters: a quotation mark, followed by a line feed, followed by a quotation mark. What you seem to want instead is for the escape sequence to be recognized:

< STRING : "\"" ( "\\n" ) "\"" >

现在匹配令牌< STRING> 将是四个字符:引号,后跟表示换行符的转义序列,后跟引号。

Now a match of the token <STRING> will be four characters: a quotation mark, followed by an escape sequence representing a line feed, followed by a quotation mark.

在您当前的定义中,我看到其他经常逃逸的元字符(如引号和反斜杠)也被字面识别,而不是转义序列。

In your current definition, I see that other often-escaped metacharacters like quotation mark and backslash are also being recognized literally, rather than as escape sequences.

这篇关于如何在JavaCC中使用反斜杠转义字符为新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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