编译原理语法分析编程

来源:百度知道 编辑:UC知道 时间:2024/05/22 05:19:37
写一个计算器程序,要求从文件中读入表达式,如在data.txt中有下列表达式:
1+1;
4+2*54;
5/(6-2*3);
要求能检查表达式是否正确,如果正确得出正确结果。

#include <iostream>
#include <string>
#include <fstream>
#include <queue>
#include <string.h>
#include <stdio.h>

using namespace std;

enum Datatype { RESERVE_WORD=1,IDENTIFIER=2,DIGIT=3,OPERATOR=4,SEPRATOR=5 };

struct OutputStruct
{
public:
Datatype type;
string value;
};

string operate[]={"sin","cos","pow"};
string KeyWord[]={"main","int","if","char","cout"};
const int MAX_SIZE=255;
char BUFF[MAX_SIZE]; //buffer to contain a char line.
ifstream inFile;
ofstream outFileStream;
queue<OutputStruct> tt;

bool IsKeyWord(string& cs)
{
for(int i=0;i<5;++i)
if(cs==KeyWord[i])
return true; //Exist
return false;
}

void ReadLineAndAnalyze()
{
int