如何编码FIRST& FOLLOW设置在编译器内部 [英] How to encode FIRST & FOLLOW sets inside a compiler

查看:214
本文介绍了如何编码FIRST& FOLLOW设置在编译器内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个编译器设计课程,我正在采取的,我目前在语法分析,我需要写一个解析器。

I am writing a compiler for a compiler design course that I am taking and I am currently at the Syntax Analysis where I need to write a parser.

我需要使用FIRST和FOLLOW集合来处理源文本中可能出现的任何错误。我已经为我的语法中的所有非终端预先计算了FIRST和FOLLOW集合,但我有麻烦决定我应该在我的程序中实际编码他们的位置。

I need to have the FIRST and FOLLOW sets to handle any errors that may appear in the source text. I have precalculated the FIRST and FOLLOW sets for all of the non-terminals in my grammar but I am having trouble deciding where I should actually encode them inside of my program.

我应该将它们放在地图中,其中键是非终端的名称吗?

Should I place them in a map where the key is the name of the non-terminal?

任何建议都是有用的

这篇文章可能有点不清楚,如果需要, 。

This post may seem a little unclear, I can clarify any points if needed.

推荐答案

如果要保留它们,则要将它们附加到它们代表的非终结符。你可能还需要一个反转,例如,一个映射从集合成员回到它们是第一个或后面的非终结符。

If you want to keep them, you want to attach them to the nonterminals they represent. You probably also want an inversion, e.g., a map from set members back to the nonterminals of which they are the FIRST or FOLLOW.

然后你的错误恢复例程可以使用前面的或更有可能是下一个输入令牌(这是导致您报告错误的令牌),以决定您可以插入到输入流中的内容。

Then you error recovery routine can use previous or more likely the "next" input token (that's the one which caused you report an error) to decide what you might insert into the input stream instead.

t实际存储这些。我使用一个GLR解析器,其解析表本质上是LALR解析表,并且简单地构建一个递归算法来爬过表,看看什么令牌可能允许解析器继续。间接地,我利用FIRST和FOLLOW,因为那些被用来构造解析表。

I don't actually store these. I use a GLR parser, whose parse tables are essentially LALR parse tables, and simply build a recursive algorithm to crawl over the tables to see what tokens might allow the parser to proceed. Indirectly, I'm taking advantage of FIRST and FOLLOW, since those were used to construct the parse tables.

如果你正在编译器设计课程,我推荐专注于解析后的问题。你可以吸收大量的时间来试图补丁源,以回应一个错误,你会学到的是,a)这是很难的,b)没有人会特别喜欢你提供给他们的选择。你可以花费能量在语法修复,直到你在蓝色的脸,但我会等到有人要求你做一个工作。同时,对于编译器类,我会让我的编译器简单地说,语法错误在第N行和中止。
非常好,但足以让你接触到更有趣的部分。

If you are taking a compiler design course, I'm recommend focusing on the post-parsing issues. You can sink tons of time into trying to "patch up" the source in response to an error, and all you'll learn is that a) this is hard, and b) nobody will particularly like the choice you offer them. You can spend energy on syntax repair until you're blue in the face but I'd wait until somebody asked you to do it for a job. In the meantime, for a compiler class, I'd let my compiler simply say, "Syntax error on line N" and abort. Crummy but good enough to let you get on with the more interesting part.

这篇关于如何编码FIRST& FOLLOW设置在编译器内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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