如何处理递归下降解析器中无法中断语法错误的问题

发布于 2024-09-28 05:09:37 字数 544 浏览 9 评论 0原文

我目前正在上系统软件开发课程。我们正在为虚构机器的汇编语言编写两遍汇编程序。我们已经实现了标记生成器,以及抽象表示该程序所需的所有类 - 剩下的就是解析标记(除了在稍后阶段实现代码生成器之外)。这是我遇到的一个主要问题。我选择将其实现为递归下降解析器,因为这是我目前拥有的唯一技术经验......但我们不允许在语法错误时停止汇编。例如,如果用户给出了语法无效的加载字指令,我们将用 NOP 替换它。如果用户给出了不好的标签,我们就简单地忽略它。如果用户在一行中放置未知字符,我们将丢弃它们。

一方面,这听起来很容易 - 然而,实现它会导致我打破(我所理解的)递归下降解析器的重要规则之一。我的每个函数在调用另一个函数之前都会提取多个标记,因为我需要考虑所有可能的可修复语法错误。鉴于我无法停止汇编,并且我必须拥有有关当前上下文的足够信息才能智能地确定用户打算做什么,因此我必须在一个函数中处理很多事情。

这将程序从真正的递归下降解析器转变为半有限状态机。我觉得我做得很糟糕,但我不知道还能如何实现这一点。有人有什么建议/想法吗?

顺便说一句 - 我不允许使用 ANTLR 等工具或任何其他解析器生成器。

谢谢。

I'm currently in a class on systems software development. We are writing the two-pass assembler for the assembly language of a fictional machine. We've implemented the tokenizer, and all of the classes that we need to abstractedly represent this program - all that is left (besides implementing the code generator in a later phase) is to parse the tokens. Here is where I'm having a major issue. I'm choosing to implement this as a recursive descent parser, since that's the only technique I currently have experience with...but we are not allowed to stop assembly on syntax errors. For instance, if the user gives a load word instruction with invalid syntax, we are to replace it with a NOP. If the user gives a bad label, we are to simply ignore it. If the user places unknown characters in a line, we discard them.

On the one hand, it sounds easy - however, implementing this causes me to break (what I understand to be) one of the important rules of a recursive descent parser. Each of my functions pulls multiple tokens before calling another function, since I need to account for all of the possible fixable syntax errors. Given that I can't stop assembly, and I must have enough information about my current context to intelligently determine what the user was intending to do, I have to handle a lot within one function.

This turns the program from a true recursive descent parser into more of a semi-finite-state-machine. I feel like I'm doing this badly, but I'm not sure how else to implement this. Does anyone have any suggestions/ideas?

BTW - I'm not allowed to use tools like ANTLR, or any other parser generator.

Thanks.

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

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

发布评论

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

评论(1

不羁少年 2024-10-05 05:09:37

我的建议是不要尝试。糟糕的语法错误恢复是递归下降解析器所固有的。如果不允许您使用解析器生成器,那么体面的语法错误恢复可能超出了您的作业范围。 (与你的导师确认...)

My suggestion would be don't try. Poor syntax error recovery is inherent in recursive descent parsers. If you are not allowed to use a parser generator, then decent syntax error recovery is probably beyond the scope of your homework. (Check with your instructor ...)

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