使用Lex/Yacc识别汉字标识符

发布于 2024-09-08 01:30:26 字数 29 浏览 2 评论 0原文

如何使用Lex/Yacc识别中文字符标识符?

How can I use Lex/Yacc to recognize identifiers in Chinese characters?

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

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

发布评论

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

评论(2

烂人 2024-09-15 01:30:26

我认为你的意思是 Lex(词法分析器生成器)。 Yacc 是解析器生成器。

根据 Unicode 中汉字的完整范围是多少?,大多数 CJH 字符都属于 3400-9FFF范围。

根据 http://dinosaur.compilertools.net/lex/index.html

任意字符。几乎匹配
任意字符、运算符字符
。是所有字符的类
除了换行符。转义为八进制是
尽管不可移植,但可能:

<前><代码> [\40-\176]

匹配所有可打印字符
ASCII 字符集,从八进制 40 开始
(空白)转换为八进制 176(波形符)。

所以我假设你需要的是类似 [\32000-\117777] 的东西。

I think you mean Lex (the lexer generator). Yacc is the parser generator.

According to What's the complete range for Chinese characters in Unicode?, most CJH characters fall in the 3400-9FFF range.

According to http://dinosaur.compilertools.net/lex/index.html

Arbitrary character. To match almost
any character, the operator character
. is the class of all characters
except newline. Escaping into octal is
possible although non-portable:

                             [\40-\176]

matches all printable characters in
the ASCII character set, from octal 40
(blank) to octal 176 (tilde).

So I would assume what you need is something like [\32000-\117777].

预谋 2024-09-15 01:30:26

Yacc 不关心中文字符,但 lex 关心中文字符:它负责分析输入字节(和字符)以识别标记。然而,汉字一般都是多字节的。有些程序 lex 可能支持此功能,但它们不是lex。已经讨论过好几次了。

进一步阅读:

标准词法分词器 lex (或 flex)不接受多字节字符,因此对于许多现代语言来说是不切实际的。本文档描述了从描述 UTF-8 多字节字符的正则表达式到单字节正则表达式的映射。

Yacc does not care about Chinese characters, but lex does: it is responsible for analyzing the input bytes (and characters) to recognize tokens. However, Chinese characters generally are multibyte. There are programs like lex which may support this, but they're not lex. It has been discussed several times.

Further reading:

The standard lexical tokenizer, lex (or flex), does not accept multi-byte characters, and is thusly impractical for many modern languages. This document describes a mapping from regular expressions describing UTF-8 multi-byte characters to a regular expressions of single bytes.

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