资源简介
RJ010703编译原理第二次课程设计 语法分析器
另有README.DOC 以及 Abstract-tree.doc
画出抽象语法树,保证100分

代码片段和文件信息
#include
#include
#include
#include
#include “Scaner.h“
/*
*authorized by Chiang 2007303154
*/
class Node { /*This class define a node in the tree*/
public:
Node * leftChild; //left child pointer
Node * rightChild; //righr child pointer
int op; //store operator
int value; //store value of left op right
Node() { //default constructor
leftChild = NULL;
rightChild = NULL;
op = NULL;
value = 0;
}
};
class Analysis{ /*This class analysis the express and caculate the result ouput the result and the tree to the file output.txt*/
private:
ofstream out; //out is a output stream
Node * root; //root of the tree
public:
Analysis() {
Node temp;
out.open(“output.txt“);//create a output stream
root = &temp;
}
/*free all nodes in the tree through destructor*/
~Analysis() {
freeNode(root);
}
/*This function is to free all the nodes recursively*/
void freeNode(Node * & p) {
if (p != NULL) {
Node * t = p;
p = NULL;
if (t->leftChild == NULL && t->rightChild == NULL) {
free(p);
}
freeNode(t->leftChild);
freeNode(t->rightChild);
}
}
/*This public method simply invokes the inner buildTree method with two arguments*/
void buildTree() {
int result = buildTree(root pairs);
out << “/*authorized by Chiang 2007303154*/“ < out << “The results is: “ << result << endl;
out << “The Abstract-Syntax-Tree has been represented below:“ << endl << endl;
cout << “The rusult and the syntax tree have been written in the file ‘output.txt‘ “ << endl;
}
void printTree() {
printTree(root);
}
/*This fucntion output the tree detail into the file output.txt*/
private :
void printTree(Node * p) {
if (p != NULL) {
out << “节点: “ << p << endl;
out << “结果: “ << p->value << endl;
out << “运算符: “ ;
switch (p->op) {
case ADD: {
out << “+“ << endl;
break;
}
case MINUS: {
out << “-“ << endl;
break;
}
case MULT: {
out << “*“ << endl;
break;
}
default :
out << “没有运算符!“ << endl;
}
out << “左节点: “ ;
if (p->leftChild) {
out << p->leftChild << endl;
}
else {
out << “没有左节点“ << endl;
}
out << “右节点: “ ;
if (p->rightChild) {
out << p->rightChild << endl;
}
else {
out << “没有右节点“ << endl;
}
out << endl;
printTree(p->leftChild);
printTree(p->rightChild);
}
}
private:
/*This is a private function it builds a tree through invoking it recursively*/
int buildTree(Node * & np deque & pairs) {
np = new Node();
if (0 == pairs.size()) {
return 0;
}
else if (pairs.size() == 1) {
if (pairs[0]->type == INT) {//when the type of token is int
np->value = pairs[0]->value;
return np->value;
}
else {//when it i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 45056 2010-04-29 21:28 2007303154_蒋浩昊\abstract_tree.doc
文件 6245 2010-04-29 21:00 2007303154_蒋浩昊\Analysis.cpp
文件 3425 2010-04-29 20:59 2007303154_蒋浩昊\Analysis.dsp
文件 734 2010-04-29 21:00 2007303154_蒋浩昊\Analysis.dsw
文件 41984 2010-04-29 21:00 2007303154_蒋浩昊\Analysis.ncb
文件 53760 2010-04-29 21:00 2007303154_蒋浩昊\Analysis.opt
文件 756 2010-04-29 20:59 2007303154_蒋浩昊\Analysis.plg
文件 594027 2010-04-29 20:59 2007303154_蒋浩昊\Debug\Analysis.exe
文件 837396 2010-04-29 20:59 2007303154_蒋浩昊\Debug\Analysis.ilk
文件 372389 2010-04-29 20:59 2007303154_蒋浩昊\Debug\Analysis.obj
I.A.... 2641168 2010-04-29 20:15 2007303154_蒋浩昊\Debug\Analysis.pch
文件 1164288 2010-04-29 20:59 2007303154_蒋浩昊\Debug\Analysis.pdb
文件 91136 2010-04-29 20:59 2007303154_蒋浩昊\Debug\vc60.idb
文件 126976 2010-04-29 20:59 2007303154_蒋浩昊\Debug\vc60.pdb
文件 15 2007-05-23 22:09 2007303154_蒋浩昊\input.txt
文件 560 2010-04-29 20:05 2007303154_蒋浩昊\macro.h
文件 534 2010-04-29 20:59 2007303154_蒋浩昊\output.txt
文件 187 2007-05-23 12:07 2007303154_蒋浩昊\Pair.h
文件 21504 2010-04-29 21:03 2007303154_蒋浩昊\readme.doc
文件 4765 2010-04-29 19:56 2007303154_蒋浩昊\Scaner.h
I.A.... 16802 2010-04-29 21:25 2007303154_蒋浩昊\绘图.gif
目录 0 2010-04-29 20:59 2007303154_蒋浩昊\Debug
目录 0 2010-04-29 21:31 2007303154_蒋浩昊
----------- --------- ---------- ----- ----
6023707 23
- 上一篇:Dark GDK
- 下一篇:camera li
nk 接口说明
相关资源
- 编译原理实验工具及参考源码(lex&
- 类pascal语言编译器(编译原理实验)
- 编译原理课程设计:词法语法编译器
- 中科院 编译原理 习题及解答
- 编译原理四元式和逆波兰式
- 《编译原理》清华大学版中的pl0扩充
- PL/0功能扩充break功能
- 编译原理LR(0)语法分析
- 见过的最好 英语语法讲义
- 编译原理中间代码生成程序
- 编译原理:LR分析程序
- 编译原理实验:词法分析,语法分析
- 吉林大学编译原理课件
- 编译原理龙书答案
- 编译原理 第三章课后习题答案
- 易语言变量和数组的编译原理
- Kuka库卡机器人编程语法进阶
- 编译原理语法分析器、词法分析器
- 山东大学编译原理PL/0语言 compiler实验
- arcpy语法教程入门以及ArcGIS的脚本构建
- FOR循环语句的翻译程序设计简单优先
- NFA的确定化NFA->DFA完整可运行代码
- pl/0编译器 语法分析
- 哈工大威海编译原理实验报告和源代
- 哈工大威海-编译原理实验报告和源码
- Ultraedit环境下配置verilog语法高亮的字
- 编译原理课设c编译器
- 赋值语句翻译四元式
- 河北工业大学编译原理实验代码及实
- TINY+编译器 改编自原版TINY
评论
共有 条评论