如何为编译器编写缓冲区? [英] how to write buffer for compiler?

查看:82
本文介绍了如何为编译器编写缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想第一次用C#编写编译器,但是我不知怎么失去了对其缓冲的处理!我的参考文献是编译器,原理,技术和工具,它说:

I want to write compiler with C# for the first time and I somehow lost what to do about its buffering! My reference is Compilers, Principles, Techniques and Tools, and it says:


由于时间长为了处理字符和
在大型源程序的
编译期间必须处理的大量字符,已经开发了专用的缓冲
技术来减少开销
是处理单个输入字符所必需的。一个重要的方案
涉及两个缓冲区被交替重载,每个缓冲区的
大小均等于N,并且N通常是磁盘块的大小,例如, 4096
个字节。使用一个系统读取命令,我们可以将N个字符读取到
缓冲区中,而不是每个字符使用一个系统调用。如果输入文件中剩余的字符少于
,则以eof表示的特殊字符
标记源文件的末尾,并且
与源程序的任何可能字符不同。

Because of the amount of time taken to process characters and the large number of characters that must be processed during the compilation of a large source program, specialized buffering techniques have been developed to reduce the amount of overhead required to process a single input character.An important scheme involves two buffers that are alternately reloaded,Each buffer is of the same size N, and N is usually the size of a disk block,e.g., 4096 bytes. Using one system read command we can read N characters into a buffer, rather than using one system call per character. If fewer than N characters remain in the input file, then a special character, represented by eof,marks the end of the source file and is different from any possible character of the source program.

本书还说过,我们在每个缓冲区的末尾放置 eof 来实现我们到达缓冲区的末尾。它有两个指针 forward lexemBegine 指向缓冲区中的词素!
我的问题是我不知道如何创建此缓冲区?我应该在sourceBuffer类中制作大小为N的数组或缓冲区,然后如何从StreamReader中读取文件并将源文件的N个字符放入数组中?
如果我改为从源文件中读取字符,会有什么问题?

and it also said in this book that we put eof at the end of each buffer to realize that we reach the end of buffer.and it has two pointers forward and lexemBegine that points to the lexeme in buffer! my problem is that I don't know how to create this buffer ? should I make array or buffer with the size N in sourceBuffer class and then how can I read file from StreamReader and put N characters of source file into array ? what is the problem if I read characters from source file instead?

推荐答案

似乎您引用的是

在像C#这样的现代编程语言中(甚至更多),(1986年的《编译器:原理,技术和工具》。)。正是在其I / O库中)已经实现了这种缓冲(以一种可靠,经过测试的高性能方式)。

In modern programming languages like C# (or more precisely in its I/O library) this kind of buffering is already implemented (in a robust, tested, high performance way).

只需使用 StreamReader 为您完成所有这些工作。然后,一个又一个地读取字符,直到找到完整的令牌,然后按照这本出色的书中的说明处理令牌。

Just use StreamReader which does all this work for you. Then just read character after character until you found a complete token, then process your tokens as described in this excellent book.

这篇关于如何为编译器编写缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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