【问题标题】:Need help starting a Parsing Code Generator for C++ [closed]需要帮助启动 C++ 解析代码生成器 [关闭]
【发布时间】:2017-06-08 17:52:02
【问题描述】:

所以我正在攻读计算机科学学位,目前正在学习编程语言概念课程。第一个任务是创建一个随机生成语法正确但不一定语义正确的程序。这项任务是在检查 BNF 语法后完成的。我什至从未见过创建或使用解析树的程序。我的任务是从 0 到 4000,老实说,我有点怀疑我的能力。我不是在寻找免费搭车,而是在启动程序时提供一些帮助。我什至不确定主程序应该是什么样子或者它是如何工作的。 C++ 解析程序的任何指南或参考资料都会很棒。或者一些 C++ 解析器的示例源代码。

这是任务...

本练习的目的是为 C++ 编程的一个子集编写一个语法生成器 将“随机”C++ 程序写入文件的语言。通过在语法上写这些随机 正确的程序,您将进一步加深对语法差异的理解 和语义。

考虑以下一组定义 C++ 编程子集的产生式 语言:

(prog) ::= "int main() { (stat_list) return 0; }"

(stat_list) ::= (stat) | (stat_list) (stat)

(stat) ::= (cmpd_stat) | (if_stat) | (iter_stat) | (assgn_stat) | (decl_stat)

以上仅在 16 条产生式规则中的几条进行赋值。

问题 1.​ 用 C、C++、C#、Java 或 Python(您的选择)编写一个以 root 非终结符并使用 上面定义的产品。您应该按照我们在课堂上看到的示例进行扩展 递归非终结符,直到我们得到一个仅由终结符组成的句子。在里面 产生式包含多个展开式(即右侧表达式)的情况, 你的程序应该随机选择一个(最好是基于非均匀加权 哪些扩展更可能出现在 C++ 中)。你的程序应该写随机的 C++ 代码到输出文件。

我必须使用 C++ 来完成作业,因为这是我参加 CS 入门课程后唯一的语言。

任何帮助将不胜感激。

【问题讨论】:

  • 编写一个“Lexer”类,将输入字符串转换为标记。为每个语法规则编写一个带有函数的“解析器”类。使用递归的魔力。
  • 在此之前没有数据结构类。不,我没有错过任何讲座,我们要讨论的主题是语法和语义。这就是为什么我注意到感觉就像是 0-4000。这个练习现在已经超出了我的想象,这就是我寻求帮助的原因。我在网上研究了 C++ 中的解析树示例,但运气不佳。希望人们不会因为寻求帮助而投反对票。
  • "有人能帮帮我吗?"您提供的问题 1 陈述您需要的帮助;它清楚地描述了您应该能够实施的解决方案。讲师甚至说他在课堂上向您展示了如何递归地扩展非终结符(这是问题解决方案的精髓);这是对标记字符串的操作,实际上非常简单。所以,要么他是一个糟糕的老师,没有给你很好的展示,要么它没有留在你的脑海里。无论哪种方式,你都必须得到这个,否则你会错过解析的全部意义。去寻求他的帮助来理解这个想法。
  • .... 通常在教授此类内容时,会向您展示如何使用语法规则手动扩展非终结符,直到结果与输入字符串匹配。而且您倾向于在纸上记录扩展如何填写以进行匹配。我想你是在课堂上做的,而且是自己在纸上做的。他要求您自动化该过程,但在您有选择时选择随机终端令牌,而不是匹配特定输入。

标签: c++ parsing tree generator


【解决方案1】:

这里的关键是

你应该按照我们在课堂上看到的例子,递归地扩展非终结符,直到我们得到一个只包含终结符的句子。

你确实在课堂上看到过这些例子,对吧?如果没有,这里有一个示例,它使用基于英语的某种自然语言的语法。我使用类似于您似乎正在使用的约定,其中终端(即实际输出)在引号内("fox"),而非终端(短语类型)的名称在括号中((noun phrase) )。如果您从未上过英语语法课程,您可能想看看这个parts of speech list

规则

(sentence) ::= (noun phrase) (verb phrase)
(noun phrase) ::= (pronoun) | (determiner) (noun)
(verb phrase) ::= (intransitive verb) | (transitive verb) (noun phrase)

(determiner) ::= "a" | "the"
(pronoun)    ::= "I" | "you"
(noun)       ::= "bug" | "cloud"
(intransitive verb) ::= "thought" | "procrastinated"
(transitive verb)   ::= "followed" | "liked"

我省略了形容词、副词、单数/复数一致和一堆其他有趣的东西。但是我们可以生成几句话。

我们开始

(sentence)

只能替换为

(noun phrase) (verb phrase)

有两种可能的(noun phrase) 替换。抛硬币是反面,所以我们替换为(determiner) (noun)

(determiner) (noun) (verb phrase)

有两种可能的(determiner) 替换。抛硬币又是反面,所以我们用“the”代替

"the" (noun) (verb phrase)

好的,我要压缩这个。在每一步,我们都会替换第一个剩余的非终结符(带括号的名称,记住)。我们可以在两列中写出整个推导(这就是它的名称):到目前为止我们所拥有的,以及我们将要应用的替换(总是针对第一个未扩展的非终结符)。所以每一行都以上一行的替换结果开始:

(sentence)                                     | (sentence) ::= (noun phrase) (verb phrase)
(noun phrase) (verb phrase)                    | (noun phrase) ::= (determiner) (noun) 
(determiner) (noun) (verb phrase)              | (determiner) ::= "the"   
"the" (noun) (verb phrase)                     | (noun) ::= "cloud"       
"the" "cloud" (verb phrase)                    | (verb phrase) ::= (transitive verb) (noun phrase)
"the" "cloud" (transitive verb) (noun phrase)  | (transitive verb) ::= "followed" 
"the" "cloud" "followed" (noun phrase)         | (noun phrase) ::= (determiner) (noun)
"the" "cloud" "followed" (determiner) (noun)   | (determiner) ::= "the"   
"the" "cloud" "followed" "the" (noun)          | (noun) ::= "bug"
"the" "cloud" "followed" "the" "bug"

这是另一个:

(sentence)                                 | (sentence) ::= (noun phrase) (verb phrase)
(noun phrase) (verb phrase)                | (noun phrase) ::= (determiner) (noun)
(determiner) (noun) (verb phrase)          | (determiner) ::= "a"
"a" (noun) (verb phrase)                   | (noun) ::= "bug"
"a" "bug" (verb phrase)                    | (verb phrase) ::= (transitive verb) (noun phrase)
"a" "bug" (transitive verb) (noun phrase)  | (transitive verb) ::= "liked" 
"a" "bug" "liked" (noun phrase)            | (noun phrase) ::= (pronoun)
"a" "bug" "liked" (pronoun)                | (pronoun) ::= "you"
"a" "bug" "liked" "you" 

(大约有 20 行代码,唯一的数据结构是语法、字符串到字符串向量向量的映射,以及包含中间结果的两个字符串向量。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多