崔老师 实验三程序框架

编程入门 行业动态 更新时间:2024-10-26 04:24:25

崔老师  实验三程序<a href=https://www.elefans.com/category/jswz/34/1770644.html style=框架"/>

崔老师 实验三程序框架

 

思路1: 从上到下分析法,递归下降来检测语法是否正确 + 表达式求值

//#include<bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include<string.h>
#include<stdio.h>
#include<time.h>
#include<sstream>
#include<stack>using namespace std;bool IsOperator(char ch)
{if (ch == '+' || ch == '-' ||ch == '*' || ch == '/' ||ch == '(' || ch == ')' || ch == '=')return true;elsereturn false;}//运算符的优先关系
//'+', '-', '*', '/', '(', ')', '#'
char OprRelation[7][7] = {{'>', '>', '<', '<', '<', '>', '>'}, //'+'{'>', '>', '<', '<', '<', '>', '>'}, //'-'{'>', '>', '>', '>', '<', '>', '>'}, //'*'{'>', '>', '>', '>', '<', '>', '>'}, //'/'{'<', '<', '<', '<', '<', '=', ' '}, //'('{'>', '>', '>', '>', ' ', '>', '>'}, //')'{'<', '<', '<', '<', '<', ' ', '='}
};//'#'int ConvertToIndex(char opr)
{int index;switch (opr){case '+':index = 0;break;case '-':index = 1;break;case '*':index = 2;break;case '/':index = 3;break;case '(':index = 4;break;case ')':index = 5;break;case '=':index = 6;break;}return index;
}
/**这个是用来获取连个字符的优先级
*/
char Precede(char opr1, char opr2)
{int index1 = ConvertToIndex(opr1);int index2 = ConvertToIndex(opr2);return OprRelation[index1][index2];
}/**这个是用来对两个数进行求值
*/
double Operate(double opnd1, char op, double opnd2)
{double ret;switch(op){case '+':ret = opnd1 + opnd2;break;case '-':ret = opnd1 - opnd2;break;case '*':ret = opnd1 * opnd2;break;case '/':ret = opnd1 / opnd2;break;}return ret;
}//算符优先算法
double CaculateExpression(string exp)
{stack<char> optr; //只处理+ - # / ()运算stack<double> opnd;  //只处理0-9的整数运算char ch;int i = 0;optr.push('=');ch = exp[i++];bool flag = true;//如果##配对,表达式求值完成while (flag&&(ch != '=' || optr.top() != '=')){if (!IsOperator(ch)){string f,g;f=exp[i-1];g.append(f);while(!IsOperator(exp[i])){f=exp[i];g.append(f);++i;}double value = atof(g.c_str());//操作数入栈opnd.push(value);ch = exp[i++];}else{//比较栈顶操作符和新取得的操作符的优先关系switch (Precede(optr.top(), ch)){case '<'://栈顶优先权低optr.push(ch);ch = exp[i++];break;case '='://括号配对,栈顶括号弹出optr.pop();if(exp.length()==i){flag=false;}else{ch = exp[i++];}break;case '>'://栈顶优先权高,先弹出,计算,结果操作数入栈char op = optr.top();optr.pop();double num2 = opnd.top();//第二个操作数在前opnd.pop();double num1 = opnd.top();opnd.pop();double ret = Operate(num1, op, num2);opnd.push(ret);break;}}}//end of while//操作数栈的唯一元素即为计算结果return opnd.top();
}template <class Type> Type stringToNum(const string& str)
{istringstream iss(str);Type num;iss >> num;return num;
}int num=0;
int sum=0;ifstream infile("d.txt");//词法分析的结果或语法分析的输入
string str;//string变量进行字符识别
string sym; //指针
string sym1;
int temp=0;//int expressionAnalysis();//表达式分析
//int termAnaysis();//项分析
//int factorAnalysis();//因子分析
//int advance();
int E();
int E_plus();
int T();
int T_plus();
int F();int conterr=0;//记录错误
int lpnum=0;//记录左括号
int found;//提取字符串中指针的位置
string sym2;
int found1;
int flag=0;//记录往后移动一个指针SYM是否正确
string s;//用来保存要分析的字符串
struct _2tup
{string token;int id;
};int advance() //SYM的移动
{if(!getline(infile,str)) //从文件中提取字符{return 0;}
//    cout<<str<<endl;//    cout<<s<<endl;found=str.find(',',0);found1=str.find(')',0);
//    cout<<found1<<endl;if(found==-1) //当为error的时候,没有‘,’{conterr++;return 0;
//        cout<<"语法错误 识别字符错误"<<endl;
//        return -1;}if(temp==1)sym1=sym;sym=str.substr(1,found-1);sym2=str.substr(found+1,found1-found-1);if(sym2!="="){s+=str;}num=stringToNum<int>(sym2);
//    cout<<num<<endl;return 1;
}int F()
{if(sym=="28"){advance();}else{if(sym=="23"){advance();E();if(sym=="24"){advance();}else{printf("Error!\n");printf("缺少 ) \n");exit(1);}}else{printf("Error!\n");printf("缺少数字 \n");exit(1);}}return 0;}
int T_plus()
{if(sym=="13" || sym=="14") // 如果是 *  /{advance();T();E_plus();}
}
int T()
{F();T_plus();}int E_plus()
{if(sym=="11" || sym=="12")  // 如果是+ 或者 -{advance();T();E_plus();}
}int E()
{T();E_plus();}
string instr;//输入符号串
void yufafenxi()//语法分析
{
//    freopen("CON", "w", stdout);//结果在控制台上输出
//    flag=advance();
//    if(flag)-
//    {advance();E();advance();if(strcmp(str.data(),"#")!=0){printf("%s\n",s.data());printf("Error\n");}else{cout<<"OK:   "<<s<<endl;cout<<"计算结果为:"<<endl;cout<<CaculateExpression(instr)<<endl;}
//    }
//    if(flag!=-1 && !conterr)
//    {
//    cout<<"正确:"<<s<<endl;
//    }}int index;//当前输入符号读入字符的位置
char character;//全局变量字符,存放最新读入的字符
string token;//字符数组,存放已读入的字符序列
map<string, int> Symbol;//标识符集
map<string, int> Digit;//常数集
map<string, int> other;//other
map<string, int>::iterator ite;
const int len = 100;
string Reserve[len];//保留字表
map<string,int> type;
//void init_Reserve()  //构造保留字表的函数
//{
//    Reserve[1] = "begin";
//    Reserve[2] = "end";
//    Reserve[3] = "if";
//    Reserve[4] = "then";
//    Reserve[5] = "while";
//    Reserve[6] = "do";
//    Reserve[7] = "const";
//    Reserve[8] = "var";
//    Reserve[9] = "call";
//    Reserve[10] = "procedure";
//
//}
void getChar()  //读入一个字符
{character = instr[index++];
}
void getnbc()  //读入非空白字符
{while (character == ' '){getChar();}
}
void concat()  //连接字符串
{token = token + character;
}
bool letter()  //判断是否为字母
{if ((character >= 'A'&&character <= 'Z') || (character >= 'a'&&character <= 'z'))return true;return false;
}
bool digit()  //判断是否为数字
{if (character >= '0'&&character <= '9')return true;return false;
}
void retract() //回退字符的函数
{
//    character = ' ';index--;
}
int reserve()  //匹配保留字符
{for (int i = 0; i < len; i++)if (Reserve[i] == token)return i;return -1;
}
void symbol()
{ite = Symbol.find(token);if (ite != Symbol.end()){
//        return ite->first;return ;}else{Symbol[token] = Symbol.size();return ;
//        return token;}
}
void constant()
{ite = Digit.find(token);if (ite != Digit.end()){
//        return ite->first;return ;}else{Digit[token] = Digit.size();return ;
//        return token;}
}
void Three()
{ite = other.find(token);if (ite != other.end()){return ;
//        return ite->first;}else{other[token] = other.size();return ;
//        return token;}
}
void error()
{cout << token << "\t-->\t该单词不存在" << endl;
}
//词法分析函数,逐个识别单词
string LexAnalyze()
{token = "";getChar();getnbc();string val;
//    int num = -1;if(character>='a'&&character<='z' || character>='A'&&character<='Z'){while (letter() || digit())  //为字母或数字{concat();//追加到token末尾//            if(token=="odd")
//            {
//                getChar();
//                Three();
//                return ;
//            }getChar();//读取下一个字符}if(token=="odd"){getChar();Three();return 0;}retract();//回退一个字符{
//            val = symbol();//查看标识符表symbol();}}else if(character>='0' && character<='9'){int flag=0;while (character>='0' && character<='9' || character>='a' && character<='z' || character>='A' && character <='Z'){if(character>='0' && character<='9')concat();if(character>='a' && character<='z' || character>='A' && character <='Z'){concat();flag=1;}getChar();}if(flag==1){error();return 0;}retract();
//        val = constant();//查看常数表constant();}else if(character=='<'){concat();getChar();if (character == '='){concat();Three();
//返回<=符号}else if(character=='>'){concat();Three();}else{retract();Three();
//返回<符号}}else if(character=='>'){concat();getChar();if (character == '='){concat();Three();
//返回>=符号}else{retract();Three();
//返回>符号}}else if(character=='='){concat();getChar();if (character == '='){concat();Three();
//返回==符号}else{retract();Three();
//返回=符号}}else if(character==':'){concat();getChar();if (character == '='){concat();Three();}else{cout<<token<<endl;error();return 0;}}else if(character=='+'){concat();Three();}else if(character=='-'){concat();Three();}else if(character=='*'){concat();Three();}else if(character=='/'){concat();Three();}else if(character=='('){concat();Three();}else if(character==')'){concat();Three();}else if(character==','){concat();Three();}else if(character=='.'){concat();Three();}else if(character==';'){concat();Three();}else if(character=='#'){concat();return token;}else{error();return 0;}return token;}
void show_table()
{cout << "\n==================" << "标识符" << "==================" << endl;
//    cout << "标识符\t\t类别编码\t表中位置" << endl;for (ite=Symbol.begin(); ite != Symbol.end(); ite++){
//        int flag=0;
//        for (int i = 0; i < len; i++)
//        {
//               cout<<Reserve[i]<<endl;
//            if (Reserve[i] == ite->first)
//            {
//                cout<<ite->first<<"是保留字"<<"   ";
//                cout << "("<<ite->first <<","<<ite->second<<")" << endl;
//                flag=1;
//                break;
//            }
//
//
//        }
//        if(flag==0)
//            cout << "("<<ite->first <<","<<ite->second<<")" << endl;map<string,int>::iterator iter;iter=type.find(ite->first);if(iter!=type.end())cout << "("<<iter->second <<","<<ite->second<<")" << endl;elsecout << "("<<type.size() <<","<<ite->second<<")" << endl;}cout << "\n==================" << "常数表" << "==================" << endl;
//    cout << "常量值\t\t类别编码\t表中位置" << endl;for (ite = Digit.begin(); ite != Digit.end(); ite++){
//        cout << "("<<ite->first <<","<<ite->second<<")" << endl;map<string,int>::iterator iter;iter=type.find(ite->first);
//        if(iter!=type.end())
//            cout << "("<<iter->second <<","<<ite->second<<")" << endl;
//        elsecout << "("<<type.size()-1 <<","<<ite->second<<")" << endl;}cout << "\n==================" << "算符、界符" << "==================" << endl;for (ite = other.begin(); ite != other.end(); ite++){
//        cout << "("<<ite->first <<","<<ite->second<<")" << endl;map<string,int>::iterator iter;iter=type.find(ite->first);cout << "("<<iter->second <<","<<ite->second<<")" << endl;
//           cout<<iter->second<<endl;}
}int main()
{
//    clock_t start,finish;
//    double totaltime;
//    start=clock();type["begin"]=type.size();type["end"]=type.size();type["if"]=type.size();type["then"]=type.size();type["while"]=type.size();type["do"]=type.size();type["const"]=type.size();type["var"]=type.size();type["call"]=type.size();type["procedure"]=type.size();type["+"]=type.size();type["-"]=type.size();type["*"]=type.size();type["/"]=type.size();type["odd"]=type.size();type["="]=type.size();type["<>"]=type.size();type["<"]=type.size();type[">"]=type.size();type["<="]=type.size();type[">="]=type.size();type[":="]=type.size();type["("]=type.size();type[")"]=type.size();type[","]=type.size();type["."]=type.size();type[";"]=type.size();type["常数"]=type.size();type["标识符"]=type.size();//    init_Reserve();//保留字表初始化Symbol.clear();//标识符集初始化Digit.clear();//常数集初始化index = 0;character = ' ';token = "";cout << "请输入待词法分析的源程序代码:输入文件结束符结束输入\n" << endl;//源程序代码输入处理
//    instr="var a,b,c; a:=1; b:=2; c:=a+b";char ch[1000];string ins;
//    printf("%d\n",gets(ch));
//    printf("\n%d",&ch[1]);
//    cout<<gets(ch)<<endl;while(gets(ch)){FILE *fp;
//        char str[1000];map<string,int>::iterator iter;if((fp=fopen("d.txt","w"))==NULL){printf("error occur!\n");exit(1);}if((fp=fopen("d.txt","a"))==NULL){printf("error occur!\n");exit(1);}//        ins=ch;instr=ch;//        printf("*");index=0;while (index < instr.size()){char* str=(char*)LexAnalyze().data();
//                    fprintf(fp,"%s\n",str);
//            cout<<str<<endl;if(strcmp(str,"#")==0){
//                printf("*");fprintf(fp,"%s",str);}else{iter=type.find(str);if(iter!=type.end())//                if(index==instr.size())fprintf(fp,"(%d,%s)\n",iter->second,str);
//                else
//                    fprintf(fp,"(%d,%s)\n",iter->second,str);else{iter=Digit.find(str);if(iter!=Digit.end())//                if(index==instr.size())fprintf(fp,"(28,%s)\n",str);
//                else
//                    fprintf(fp,"(28,%s)\n",str);}}}fclose(fp);//        cout<<instr<<endl;memset(ch,0,sizeof(ch));}yufafenxi();//    cout<<instr<<endl;
//    string in;
//    while (cin >> in&&in != "@")
//    {
//        instr = instr + " " + in;
//    }//识别二元组初始化
//    while (index < instr.size())
//    {
//        LexAnalyze();
//    }//展示构造的各种词汇表show_table();//    finish=clock();
//    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
//    cout<<totaltime<<endl;
//    //system("pause");return 0;}

 

 

输入规范:

1)全程输入英文,注意不要使用中文输入法

2)表达式后面加上 =  

3)= 后面添加结束符 #

4)输入完成以后,回车,ctrl+Z,键入文件结束符

 

正确输入:

 

错误输入:

 

 

 

思路2:  梯度下降分析法      即检测语法,又算出来表达式的结果

 

 

 

输入规范:

1)全程输入英文,注意不要使用中文输入法

2)表达式后面加上 =  

3)= 后面添加结束符 #

4)输入完成以后,回车,ctrl+Z,键入文件结束符

 

//#include<bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include<string.h>
#include<stdio.h>
#include<time.h>
#include<sstream>
#include<stack>using namespace std;template <class Type> Type stringToNum(const string& str)
{istringstream iss(str);Type num;iss >> num;return num;
}int num=0;
int sum=0;ifstream infile("d.txt");//词法分析的结果或语法分析的输入
string str;//string变量进行字符识别
string sym; //指针
string sym1;
int temp=0;//int expressionAnalysis();//表达式分析
//int termAnaysis();//项分析
//int factorAnalysis();//因子分析
//int advance();
int E(int);
int E_plus(int);
int T(int);
int T_plus(int);
int F(int);
int global_value=0;int conterr=0;//记录错误
int lpnum=0;//记录左括号
int found;//提取字符串中指针的位置
string sym2;
int found1;
int flag=0;//记录往后移动一个指针SYM是否正确
string s;//用来保存要分析的字符串
struct _2tup
{string token;int id;
};int advance() //SYM的移动
{if(!getline(infile,str)) //从文件中提取字符{return 0;}
//    cout<<str<<endl;//    cout<<s<<endl;found=str.find(',',0);found1=str.find(')',0);
//    cout<<found1<<endl;if(found==-1) //当为error的时候,没有‘,’{conterr++;return 0;
//        cout<<"语法错误 识别字符错误"<<endl;
//        return -1;}if(temp==1)sym1=sym;sym=str.substr(1,found-1);sym2=str.substr(found+1,found1-found-1);if(sym2!="="){s+=str;}num=stringToNum<int>(sym2);
//    cout<<num<<endl;return 1;
}int F(int value=0)
{if(sym=="28"){value=num;advance();}else{if(sym=="23"){advance();value=E(value);if(sym=="24"){advance();}else{printf("Error!\n");printf("缺少 ) \n");exit(1);}}else{printf("Error!\n");printf("缺少数字 \n");exit(1);}}return value;}
int T_plus(int value=0)
{if(sym=="13" || sym=="14") // 如果是 *  /{if(sym=="13"){advance();value=value*T(value);}if(sym=="14"){advance();value=value/T(value);}value=E_plus(value);}return value;
}
int T(int value=0)
{value=F();
//    cout<<value<<endl;value=T_plus(value);return value;}int E_plus(int value)
{if(sym=="11" || sym=="12")  // 如果是+ 或者 -{if(sym=="11"){advance();value=value+T(value);}if(sym=="12"){advance();value=value-T();}E_plus(value);}return value;
}int E(int value)
{value=T(value);value=E_plus(value);return value;
}
string instr;//输入符号串
void yufafenxi()//语法分析
{
//    freopen("CON", "w", stdout);//结果在控制台上输出
//    flag=advance();
//    if(flag)-
//    {int value;advance();value=E(0);advance();if(strcmp(str.data(),"#")!=0){printf("%s\n",s.data());printf("Error\n");}else{cout<<"OK:   "<<s<<endl;cout<<"计算结果为:"<<endl;cout<<value<<endl;}
//    }
//    if(flag!=-1 && !conterr)
//    {
//    cout<<"正确:"<<s<<endl;
//    }}int index;//当前输入符号读入字符的位置
char character;//全局变量字符,存放最新读入的字符
string token;//字符数组,存放已读入的字符序列
map<string, int> Symbol;//标识符集
map<string, int> Digit;//常数集
map<string, int> other;//other
map<string, int>::iterator ite;
const int len = 100;
string Reserve[len];//保留字表
map<string,int> type;
//void init_Reserve()  //构造保留字表的函数
//{
//    Reserve[1] = "begin";
//    Reserve[2] = "end";
//    Reserve[3] = "if";
//    Reserve[4] = "then";
//    Reserve[5] = "while";
//    Reserve[6] = "do";
//    Reserve[7] = "const";
//    Reserve[8] = "var";
//    Reserve[9] = "call";
//    Reserve[10] = "procedure";
//
//}
void getChar()  //读入一个字符
{character = instr[index++];
}
void getnbc()  //读入非空白字符
{while (character == ' '){getChar();}
}
void concat()  //连接字符串
{token = token + character;
}
bool letter()  //判断是否为字母
{if ((character >= 'A'&&character <= 'Z') || (character >= 'a'&&character <= 'z'))return true;return false;
}
bool digit()  //判断是否为数字
{if (character >= '0'&&character <= '9')return true;return false;
}
void retract() //回退字符的函数
{
//    character = ' ';index--;
}
int reserve()  //匹配保留字符
{for (int i = 0; i < len; i++)if (Reserve[i] == token)return i;return -1;
}
void symbol()
{ite = Symbol.find(token);if (ite != Symbol.end()){
//        return ite->first;return ;}else{Symbol[token] = Symbol.size();return ;
//        return token;}
}
void constant()
{ite = Digit.find(token);if (ite != Digit.end()){
//        return ite->first;return ;}else{Digit[token] = Digit.size();return ;
//        return token;}
}
void Three()
{ite = other.find(token);if (ite != other.end()){return ;
//        return ite->first;}else{other[token] = other.size();return ;
//        return token;}
}
void error()
{cout << token << "\t-->\t该单词不存在" << endl;
}
//词法分析函数,逐个识别单词
string LexAnalyze()
{token = "";getChar();getnbc();string val;
//    int num = -1;if(character>='a'&&character<='z' || character>='A'&&character<='Z'){while (letter() || digit())  //为字母或数字{concat();//追加到token末尾//            if(token=="odd")
//            {
//                getChar();
//                Three();
//                return ;
//            }getChar();//读取下一个字符}if(token=="odd"){getChar();Three();return 0;}retract();//回退一个字符{
//            val = symbol();//查看标识符表symbol();}}else if(character>='0' && character<='9'){int flag=0;while (character>='0' && character<='9' || character>='a' && character<='z' || character>='A' && character <='Z'){if(character>='0' && character<='9')concat();if(character>='a' && character<='z' || character>='A' && character <='Z'){concat();flag=1;}getChar();}if(flag==1){error();return 0;}retract();
//        val = constant();//查看常数表constant();}else if(character=='<'){concat();getChar();if (character == '='){concat();Three();
//返回<=符号}else if(character=='>'){concat();Three();}else{retract();Three();
//返回<符号}}else if(character=='>'){concat();getChar();if (character == '='){concat();Three();
//返回>=符号}else{retract();Three();
//返回>符号}}else if(character=='='){concat();getChar();if (character == '='){concat();Three();
//返回==符号}else{retract();Three();
//返回=符号}}else if(character==':'){concat();getChar();if (character == '='){concat();Three();}else{cout<<token<<endl;error();return 0;}}else if(character=='+'){concat();Three();}else if(character=='-'){concat();Three();}else if(character=='*'){concat();Three();}else if(character=='/'){concat();Three();}else if(character=='('){concat();Three();}else if(character==')'){concat();Three();}else if(character==','){concat();Three();}else if(character=='.'){concat();Three();}else if(character==';'){concat();Three();}else if(character=='#'){concat();return token;}else{error();return 0;}return token;}
void show_table()
{cout << "\n==================" << "标识符" << "==================" << endl;
//    cout << "标识符\t\t类别编码\t表中位置" << endl;for (ite=Symbol.begin(); ite != Symbol.end(); ite++){
//        int flag=0;
//        for (int i = 0; i < len; i++)
//        {
//               cout<<Reserve[i]<<endl;
//            if (Reserve[i] == ite->first)
//            {
//                cout<<ite->first<<"是保留字"<<"   ";
//                cout << "("<<ite->first <<","<<ite->second<<")" << endl;
//                flag=1;
//                break;
//            }
//
//
//        }
//        if(flag==0)
//            cout << "("<<ite->first <<","<<ite->second<<")" << endl;map<string,int>::iterator iter;iter=type.find(ite->first);if(iter!=type.end())cout << "("<<iter->second <<","<<ite->second<<")" << endl;elsecout << "("<<type.size() <<","<<ite->second<<")" << endl;}cout << "\n==================" << "常数表" << "==================" << endl;
//    cout << "常量值\t\t类别编码\t表中位置" << endl;for (ite = Digit.begin(); ite != Digit.end(); ite++){
//        cout << "("<<ite->first <<","<<ite->second<<")" << endl;map<string,int>::iterator iter;iter=type.find(ite->first);
//        if(iter!=type.end())
//            cout << "("<<iter->second <<","<<ite->second<<")" << endl;
//        elsecout << "("<<type.size()-1 <<","<<ite->second<<")" << endl;}cout << "\n==================" << "算符、界符" << "==================" << endl;for (ite = other.begin(); ite != other.end(); ite++){
//        cout << "("<<ite->first <<","<<ite->second<<")" << endl;map<string,int>::iterator iter;iter=type.find(ite->first);cout << "("<<iter->second <<","<<ite->second<<")" << endl;
//           cout<<iter->second<<endl;}
}int main()
{
//    clock_t start,finish;
//    double totaltime;
//    start=clock();type["begin"]=type.size();type["end"]=type.size();type["if"]=type.size();type["then"]=type.size();type["while"]=type.size();type["do"]=type.size();type["const"]=type.size();type["var"]=type.size();type["call"]=type.size();type["procedure"]=type.size();type["+"]=type.size();type["-"]=type.size();type["*"]=type.size();type["/"]=type.size();type["odd"]=type.size();type["="]=type.size();type["<>"]=type.size();type["<"]=type.size();type[">"]=type.size();type["<="]=type.size();type[">="]=type.size();type[":="]=type.size();type["("]=type.size();type[")"]=type.size();type[","]=type.size();type["."]=type.size();type[";"]=type.size();type["常数"]=type.size();type["标识符"]=type.size();//    init_Reserve();//保留字表初始化Symbol.clear();//标识符集初始化Digit.clear();//常数集初始化index = 0;character = ' ';token = "";cout << "请输入待词法分析的源程序代码:输入文件结束符结束输入\n" << endl;//源程序代码输入处理
//    instr="var a,b,c; a:=1; b:=2; c:=a+b";char ch[1000];string ins;
//    printf("%d\n",gets(ch));
//    printf("\n%d",&ch[1]);
//    cout<<gets(ch)<<endl;while(gets(ch)){FILE *fp;
//        char str[1000];map<string,int>::iterator iter;if((fp=fopen("d.txt","w"))==NULL){printf("error occur!\n");exit(1);}if((fp=fopen("d.txt","a"))==NULL){printf("error occur!\n");exit(1);}//        ins=ch;instr=ch;//        printf("*");index=0;while (index < instr.size()){char* str=(char*)LexAnalyze().data();
//                    fprintf(fp,"%s\n",str);
//            cout<<str<<endl;if(strcmp(str,"#")==0){
//                printf("*");fprintf(fp,"%s",str);}else{iter=type.find(str);if(iter!=type.end())//                if(index==instr.size())fprintf(fp,"(%d,%s)\n",iter->second,str);
//                else
//                    fprintf(fp,"(%d,%s)\n",iter->second,str);else{iter=Digit.find(str);if(iter!=Digit.end())//                if(index==instr.size())fprintf(fp,"(28,%s)\n",str);
//                else
//                    fprintf(fp,"(28,%s)\n",str);}}}fclose(fp);//        cout<<instr<<endl;memset(ch,0,sizeof(ch));}yufafenxi();//    cout<<instr<<endl;
//    string in;
//    while (cin >> in&&in != "@")
//    {
//        instr = instr + " " + in;
//    }//识别二元组初始化
//    while (index < instr.size())
//    {
//        LexAnalyze();
//    }//展示构造的各种词汇表show_table();//    finish=clock();
//    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
//    cout<<totaltime<<endl;
//    //system("pause");return 0;}

 

 

正确输入:

 

 

 

错误输入:

 

 

 

以上程序均有一些bug,需要注意输入方式:

比如

 

 

 

思路3:  自底向上的分析方法,在给出运算符优先级表的情况下使用规约,解决表达式求值

 

 

更多推荐

崔老师 实验三程序框架

本文发布于:2024-03-13 03:07:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1733050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   老师   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!