令牌和文件流

发布于 2024-12-23 03:24:03 字数 101 浏览 1 评论 0原文

我研究对文件流进行操作的程序(用 C++ 编写)的源代码。但时不时地我会偶然发现一种叫做tokens的东西。您能向我解释一下代币的作用以及它们为什么有用吗?谢谢。

I study source codes of programs, (written in c++) that operate on file streams. But every now and then i stumble upon something called tokens. Can you please explain to me, what tokens do and why are they useful? Thank you.

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

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

发布评论

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

评论(2

临风闻羌笛 2024-12-30 03:24:03

是指随着时间的推移而可用的数据元素序列。流中的数据元素可以是任何类型:字符、字符串、专用数据结构等。文本文件可以简单地解释为字符流。

在编程语言的上下文中,单词令牌通常指的是字符流中的一个或多个相关字符的序列。令牌在字符流之上提供了一定程度的抽象,并且通常将其本身分组到流中以进行进一步处理。

编程语言编译器中的通常处理顺序从词法分析器将字符流转换为标记流开始,然后将其传递给解析器。令牌的典型表示由类型指示符和令牌的内容组成。

下面是一个示例:考虑 C++ 程序的这个片段:

class MyClass {
public:
    string name;
};

词法分析器将该程序分组为包含 10 个标记的流:

'class' (keyword)
'MyClass' (identifier)
'{' (opening brace)
'public' (keyword)
':' (colon)
'string' (identifier)
'name' (identifier)
';' (semicolon)
'}' (closing brace)
';' (semicolon)

Stream refers to a sequence of data elements made available over time. Data elements in streams could be of any kind: characters, strings, special-purpose data structures, etc. A text file could be trivially interpreted as a stream of characters.

In the context of programming languages, the word token usually refers to a sequence of one or more related characters from a stream of characters. Tokens offer a level of abstraction on top of character streams, and are often themselves grouped in streams for further processing.

The usual processing sequence in compilers of programming languages starts with lexical analyzers converting streams of characters into streams of tokens, which are then passed on to parsers. Typical representation of a token consists of a type indicator and the content of the token.

Here is an example: consider this fragment of C++ program:

class MyClass {
public:
    string name;
};

A lexer analyzer groups this program into a stream of ten tokens:

'class' (keyword)
'MyClass' (identifier)
'{' (opening brace)
'public' (keyword)
':' (colon)
'string' (identifier)
'name' (identifier)
';' (semicolon)
'}' (closing brace)
';' (semicolon)
碍人泪离人颜 2024-12-30 03:24:03

代币可以被认为是“占位符”。令牌表示特定实体(字符串),通常是两个等效实体之间的某种中间步骤。例如,标记通常用在解析器中,它们表示给定语言的语法。

Tokens can be thought of as "place holders". Tokens represent a specific entity (string), and are usually some sort of intermediary step between two equivalents. For example, tokens are often used in parsers where they represent a given language's syntax.

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