如何使用 Flex 和 YACC 实现 #include 构造?
在解析过程中,如果我遇到包含标记,我想指示 YACC 打开指定为输入的文件并开始解析它。一旦解析完成,我想指示 YACC 返回到文件并直接在包含表达式之后继续解析。我会将包含深度级别限制为 1。
During parsing, if I encounter a include token I want to instruct YACC to open the file specified as input and to begin parsing this. Once this parsing is finished, I want to instruct YACC to return to the file and continue parsing directly after the include expression. I will restrict the include depth level to one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Flex 手册介绍了如何使用 yypush_buffer_state() 和 yypop_buffer_state() 来执行此操作。
这里是有关使用多个输入缓冲区的官方手册。有一些示例代码。
The flex manual covers how to do this using yypush_buffer_state() and yypop_buffer_state().
Here is the section of the official manual on using multiple input buffers. There is some sample code.
在处理器的词汇阶段和句法阶段之间进行通信是正常的。
因此,在解析器中识别 include 指令的语法(或者,为了使事情变得更容易,只需在词法分析器中识别它)并在词法级别进行切换。
例如,这是一种简单的语言,可以识别包含
ab
或cd
或.file
的标准输入行。当它看到.someString
时,它会将someString
作为包含文件打开,然后返回读取标准输入。当我们运行它时...
It's normal to communicate between the lexical and syntactic phases of your processor.
So, recognize the syntax for an include directive in your parser (or, to make things easier, just recognize it in the lexer) and do the switching at the lexical level.
For example, here is a simple language that recognizes standard input lines containing
ab
orcd
or.file
. When it sees.someString
it openssomeString
as an include file and then goes back to reading standard input.And when we run it...