令牌和文件流
我研究对文件流进行操作的程序(用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
流是指随着时间的推移而可用的数据元素序列。流中的数据元素可以是任何类型:字符、字符串、专用数据结构等。文本文件可以简单地解释为字符流。
在编程语言的上下文中,单词令牌通常指的是字符流中的一个或多个相关字符的序列。令牌在字符流之上提供了一定程度的抽象,并且通常将其本身分组到流中以进行进一步处理。
编程语言编译器中的通常处理顺序从词法分析器将字符流转换为标记流开始,然后将其传递给解析器。令牌的典型表示由类型指示符和令牌的内容组成。
下面是一个示例:考虑 C++ 程序的这个片段:
词法分析器将该程序分组为包含 10 个标记的流:
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:
A lexer analyzer groups this program into a stream of ten tokens:
代币可以被认为是“占位符”。令牌表示特定实体(字符串),通常是两个等效实体之间的某种中间步骤。例如,标记通常用在解析器中,它们表示给定语言的语法。
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.