需要帮助启动用于C++的分析代码生成器
Need help starting a Parsing Code Generator for C++
所以我正在攻读我的CS学位,目前正在上一门编程语言概念课。第一个任务是创建一个程序,该程序生成语法正确的随机,但不必语义正确。这项任务是在检查了BNF gramner之后完成的。我什至从未见过创建解析树或使用解析树的程序。我从0到4000完成这项任务,老实说,我有点怀疑我的能力。我不是在寻找免费乘车,而是只是一些帮助,甚至启动该程序。我什至不确定主程序应该是什么样子或它将如何工作。C++解析程序的任何指南或参考都会很棒。或者 c++ 中解析器的一些示例源代码。
这是作业...
本练习的目的是为C++编程的子集编写语法生成器 将"随机"C++程序写入文件的语言。通过以随机语法编写这些 正确的程序,您将进一步发展对语法之间差异的理解 和语义。
考虑以下一组定义C++编程子集的产品 语言:
(prog) ::= "int main() { (stat_list) return 0; }">
(stat_list) ::= (统计) |(stat_list)(统计)
(统计) ::= (cmpd_stat) |(if_stat) |(iter_stat) |(assgn_stat) |(decl_stat)
以上仅在分配的 16 条生产规则中的少数几个。
问题 1.用 C、C++、C#、Java 或 Python(您的选择)编写一个以 根非终端,C++并使用 上面定义的产品。您应该遵循我们在课堂上看到的示例,我们扩展了这些示例 非终端递归,直到我们获得仅由终端令牌组成的句子。在 作品包含多个扩展(即右侧表达式)的情况, 您的程序应随机选择一个(最好具有基于以下条件的非均匀权重) 哪些扩展更有可能发生在C++)。您的程序应编写随机C++ 代码到输出文件。
我必须使用C++来完成作业,因为这是我在将入门课程带到CS课程后的唯一语言。
任何帮助将不胜感激。
这里的关键是
您应该遵循我们在课堂上看到的示例,其中我们递归扩展非终端,直到我们获得仅由终端标记组成的句子。
你确实在课堂上看到了这些例子,对吧?如果没有,下面是一个使用基于英语的自然语言的语法的示例。我使用的约定类似于您似乎正在使用的约定,其中终端(即实际输出)在引号("fox"
)内,而非终端的名称(短语类型)在括号中((noun phrase)
)。如果您从未上过英语语法课程,您可能需要看一下此词性列表。
规则
(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 行代码,唯一的数据结构是语法、字符串到字符串向量的映射,以及包含中间结果的两个字符串向量。
- C++ 制作一个随机数字/字母生成器,代码中有问题
- C++11 中的随机数:有没有一种简单的方法可以将生成器种子放在代码的一个位置,然后在不同的函数中使用它?
- 来自文本字符串或某种其他机制的代码类自动生成器
- 需要帮助启动用于C++的分析代码生成器
- 最佳实践:使用随机数生成器的测试代码
- 使用生成器和发行版扩展c++11随机的教程或示例代码
- 在C++代码生成器中模拟 C#"new"(隐藏虚拟方法)
- 生成独立C++代码的分析器生成器
- 是否有用于C++快速修复的消息代码生成器
- C++生成器:将二进制代码转换为AnsiString
- 如何仅在生成器或输入在CMake中发生更改时构建自动生成的代码
- 如何编写一个以C++代码为输入的C++代码生成器
- 如何在waf构建系统中使用自定义代码生成器
- 控制流图生成器的c++代码
- 任何使用T4和Powershell的MFC代码生成器
- Bazel头文件代码生成器
- 当我想要大于1000000的质数时,下面的质数生成器代码显示了一个错误
- 什么加密随机生成器C代码可以在WP8.1上使用
- 需要参考:自动代码生成器C++
- 数据绑定- XML数据绑定和c++代码生成器