角色向前看

发布于 2024-12-13 07:12:57 字数 527 浏览 4 评论 0原文

我将如何在 Java 中创建字符前瞻方法?我有一个文本文件(比如说 TextFile.txt),我需要它首先读取单个字符,识别它,如果它是特殊字符(如 { = + ; 等),则将其作为 Y 类型的标记返回。如果字符不是特殊的(如数字或字母),那么它应该向前查看是否有特殊字符或空格,当出现特殊字符或空格时,程序应该返回该字符集作为X 类型的令牌。如果程序读取数字,后跟另一个数字,然后是一个字母,它应该只返回 Z 类型的两个数字。

例如,在 TextFile.txt 中有以下文本:

{ test; 123test test5
test/-=test}

程序将读取文件,看到“{”,返回该标记为 Y,查看空白,忽略它,查看“t”,因为它不是特殊字符,向前查看并查看另一个字母,直到它到达“;”,并返回“test”作为标记键入 X。程序应读取“123”,将其返回为 Z,然后读取“test”并返回 X。

我了解程序应该如何工作,但我不知道如何使用前瞻字符读取器。我查看了 Scanner 类,但它没有 nextChar() 方法(这很有用)。

How would I go about making a character look-ahead method in Java? I have a text file (let's say TextFile.txt) and I need it to first read in a single character, recognise it, if it's a special character (like { = + ; etc.) return it as a token of type Y. If the character is not special (like a digit or letter), then it should look ahead to see if there is a special character or a white space, and when one does turn up, the program should then return that set of characters as a token of type X. If the program reads a digit, followed by another digit, then a letter, it should only return the two digits of type Z.

As an example, in TextFile.txt there is the following text:

{ test; 123test test5
test/-=test}

The program would read through the file, see a '{', return that token as Y, see the white space, ignore that, see the 't', because that's not a special character, look ahead and see another letter, until it reaches ';', and returns 'test' as a token of type X. The program should read '123', return that as Z, then read 'test' and return X.

I understand how the program should work, but I don't know what to use for the look ahead character reader. I looked at the Scanner class, but that doesn't have a method for nextChar() (which would have been useful).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

好菇凉咱不稀罕他 2024-12-20 07:12:57

我认为您实际上并不需要任何前瞻字符,只需将整个文本读入字符串并迭代其字符数组即可。检查每个字符的 isDigit 或 isLetter - 如果将其添加到字符串缓冲区,否则将缓冲区的内容刷新到包含所有标记的列表中。如果缓冲区为空并且字符是数字,请设置一个标志,并在遇到除数字之外的任何内容时立即刷新。我想应该是这样。

i think you don't really need any look ahead chars and could just read the entire text into a string and iterate over its char array. Check each char for isDigit or isLetter - if it is add to a string buffer, else flush contents of buffer into a list that holds all tokens. if the buffer is empty and the character is a digit, set a flag and flush as soon as you encounter anything except for a digit. that should do i guess.

白芷 2024-12-20 07:12:57

由于您只需要一个字符的前瞻,我建议使用 PushbackReader。您还可以使用 的阅读器支持标记,但在这个应用程序中我认为推回更容易理解。当前瞻固定(在本例中为一个字符)时,这两个选项都有效。

简而言之,您从流中读取一个字符并确定它是哪种标记类型(如果它是空格,则将其丢弃并重试)。在类似 StringBuilder 中累积令牌的该字符和后续字符。继续阅读,直到读到不属于该类型的字符为止。 将坏字符推回到流, 并返回令牌。下次请求令牌时,推回的字符将位于流的开头。

以这种方式处理流可以节省内存,因为前瞻是固定的。所需的空间不取决于流的长度(事实上,它适用于无限长的流)。

Since you only need one character of lookahead, I suggest using a PushbackReader. You could also use a reader that supports marking, but in this application I think a push-back is a easier to understand. Both of these options work when the lookahead is fixed (one character, in this case).

Simply put, you read a character from the stream and decide which token type it is (if it's whitespace, throw it away and try again). Accumulate this and subsequent characters of the token in something like a StringBuilder. Keep reading until you read a character that doesn't belong to that type. Push the bad character back to the stream, and return the token. The next time you request a token, the pushed-back character will be at the beginning of the stream.

Processing the stream in this way is memory-efficient, since the lookahead is fixed. The space required doesn't depend on the length of the stream (in fact, it will work with infinitely long streams).

却一份温柔 2024-12-20 07:12:57

您想要使用一种 InputStream 类型(在您的情况下可能是 FileInputStream),并使用 read() 方法从一个字符中提取字符一次。如果这些字符不是特殊字符/空格,请将它们保留在缓冲区中,直到找到特殊字符/空格,然后返回缓冲区的内容作为 X 类型的标记。

You want to use a type of InputStream (Probably a FileInputStream in your case) and use the read() method to pull characters off one at a time. If those characters are not special characters/whitespace, keep them in a buffer until you find a special character/whitespace, and then return the contents of the buffer as your token of type X.

东风软 2024-12-20 07:12:57

扫描仪没有 nextChar() 但您可以使用 Scanner.hasNext(String)。例如

scanner.hasNext("x")

将测试后面是否有字母x。对于更复杂的前瞻,您可以使用 Scanner.hasNext(Pattern)

Scanner has no nextChar() but you can use Scanner.hasNext(String). E.g.

scanner.hasNext("x")

will test if a letter x is following. For more complicated lookaheads you can use Scanner.hasNext(Pattern).

帥小哥 2024-12-20 07:12:57

您不需要任何特殊的前瞻功能。您只需将文件作为 FileReader 打开即可。然后,当您处理每个字符时,如果它不是特殊字符,则需要将这些字符累积在单独的 StringBuilder 中,直到到达特殊字符并可以确定当前标记的类型是什么。

you don't need any special lookahead functionality. you just need to open the file as a FileReader. then, as you process each character, if it is not a special character, you need to accumulate those characters in a separate StringBuilder until you reach a special character and can determine what the type of the current token is.

焚却相思 2024-12-20 07:12:57

你的问题不是很具有描述性......但我认为这可能会给你一个起点。将文本文件的内容拆分为字符数组。然后单独查看每个字符并执行测试/比较,

char[] cArray = textToRead.toCharArray();

for(int i = 0; i < cArray.length; i++){
    //perform your logic here
}

迭代每个字符并跟踪字符串或字符变量中所需的内容。

Your question is not very descriptive...but I think this might give you a starting point. Split up the contents of your text file into an array of characters. Then look at each character individually and perform your test/comparison

char[] cArray = textToRead.toCharArray();

for(int i = 0; i < cArray.length; i++){
    //perform your logic here
}

iterate through each character and keep track of what you need in String or character variables.

秋凉 2024-12-20 07:12:57

听起来像是语言解析。我建议使用 ANTLR。请查看 http://www. antlr.org/wiki/display/ANTLR3/五+分钟+介绍+to+ANTLR+3

Sounds like a language parsing. I'd advise to use ANTLR. Please have a look at http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文