词法分析程序

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <queue>
#include <cctype>
using namespace std;

map<string,int>Dict;
queue<int>Err;
ofstream out;   //write to file
ifstream in;    //read from file

void init()
{
    Dict["+"]=3;Dict["-"]=4;Dict["*"]=5;
    Dict["/"]=6;Dict["%"]=7;Dict["<"]=8;
    Dict["<="]=9;Dict[">"]=10;Dict[">="]=11;
    Dict["=="]=12;Dict["!="]=13;Dict["&&"]=14;
    Dict["||"]=15;Dict["="]=16;Dict["("]=17;
    Dict[")"]=18;Dict["["]=19;Dict["]"]=20;
    Dict["{"]=21;Dict["}"]=22;Dict[";"]=23;
    Dict[","]=24;Dict["void"]=25;Dict["int"]=26;
    Dict["float"]=27;Dict["char"]=28;Dict["if"]=29;
    Dict["else"]=30;Dict["while"]=31;Dict["do"]=32;
    Dict["return"]=33;
}

void block_commend()  //deal block commend
{
    string str;
    while(getline(cin,str))
    {
        int t=str.length()-1;
        if(str[t]=='/'&&str[t-1]=='*')
            return;
    }
}

int word(const string &s,int n,int line)  //read a word
{
    string tmp;
    tmp+=s[n];
    int j;
    for(j=n+1;j<s.length();j++)
    {
        if((isalpha(s[n])||isdigit(s[n]))&&(isalpha(s[j])||isdigit(s[j])))
            tmp+=s[j];
        else break;
    }
    map<string,int>::iterator idx=Dict.find(tmp);
    if(idx!=Dict.end())
    {
        out<<"<"<<idx->second<<",->,";
    }
    else
    {
        if(tmp.length()==1)
        {
            if(isalpha(tmp[0]))
                out<<"<1,"<<tmp<<">,";
            else
                out<<"<2,"<<tmp<<">,";
        }
        else
        {
            bool is_num=true;
            for(int k=0;k<tmp.length();k++)
            {
                if(!isdigit(tmp[k]))
                {
                    is_num=false;
                    break;
                }
            }
            if(is_num)
                out<<"<2,"<<tmp<<">,";
            else
            {
                if(isdigit(tmp[0])&&isalpha(tmp[1]))
                {
                    out<<"LexicalError,";
                    Err.push(line);
                }
                else out<<"<1,"<<tmp<<">,";
            }
        }
    }
    return j-1;
}

int main()
{
    init();
    string str;
    int line=0;
    out.open("file.txt");
    while(getline(cin,str)&&str[0]!='#')
    {
        if(str.length()==0)continue;  //empty line
        line++;
        bool enter=true;
        for(int i=0;i<str.length();i++)
        {
            if(str[0]=='/'&&str[1]=='/') //commend line
            {
                out<<endl;
                enter=false;  //do not need to output enter
                if(line!=0)--line;
                break;
            }
            else if(str[0]=='/'&&str[1]=='*') //commend block
            {
                block_commend();
                out<<endl; //do not need to output enter
                enter=false;
                if(line!=0)--line;
                break;
            }
            else if(str[i]==' '||str[i]=='\t') //jump space and tab.. Notice the fucking Tab !!!!!!!!!!!
                continue;
            else i=word(str,i,line); //deal with signal word
        }
        if(enter)
            out<<endl;
    }
    in.open("file.txt");
    str.clear();
    getline(in,str);
    while(in)  //read file
    {
        cout<<str<<endl;
        getline(in,str);
    }
    if(!Err.empty())cout<<"LexicalError(s) on line(s) ";
    while(!Err.empty())   //output errors
    {
        cout<<Err.front()<<",";
        Err.pop();
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<cctype> #include<cstring> #include<cmath> using namespace std; int w=0; //尾数累加器 int p=0; //指数累加器 int j=0; //十进制小数位数计数器 int e=1; //用来记录十进制数的符号,当指数为正时为1,为负时为-1 int i=0; //用来标志元素位置 int d=0; //用来表示每个数值型元素对应的数值 const int N=40;//用来确定输入识别符的最大长度 char data[N];//存放输入的识别符 bool is_digit; //标志是否是数字 string CJ1;//确定是整形还是实型 double CJ2;//记数值 //函数声明 void check(char c);//检查首字母是否是数字的函数 void deal_integer(char c);//处理识别符的整数部分 void deal_point(char c);//用来处理小数部分 void deal_index(char c);//用来处理指数部分 void s_next();// 确定实型 void z_next();//确定整型 void last();// 计算 CJ2 void error();//程序中错误处理程序 void deal();//处理函数主体 int main(){ //主函数 cout<<"please input your data,and its maximum length is "<<N<<":"<<endl;//等待用户输入识别符 cin>>data; deal();//处理函数主体 last();// 计算 CJ2 system("pause"); return 0; } void check(char c) //判断输入的首字母是否是数字 { is_digit=isdigit(c); while(is_digit!=true){//输入的首字母不是数字时 cout<<"\nError! Try again.."<<endl;//要求重新输入 cin>>data; check(data[0]); } } void deal_integer(char c){//处理识别符的整数部分 d=(int)c-48; w=w*10+d; i++; if(isdigit(data[i])!=0)//下一个仍是数值时,调用程序本身 deal_integer(data[i]); } void deal_point(char c){//用来处理小数部分 int temp=i; if(isdigit(c)!=0)//是数值字符时 deal_integer(c); else { error(); //错误处理程序 deal();//处理函数主体 } j=i-temp;//记录十进制小数位数 } void deal_index(char c){//用来处理指数部分 if(c=='-') {e=-1;i++;}//是'-'号时 else {if(c=='+') i++;//是'+' 号时 else { if(isdigit(c)==false) //非数值字符时 { error();//错误处理程序 deal();//处理函数主体 } else { d=(int)c-48;//把输入字符转换为整型 goto pro2;} } } if(isdigit(data[i])!=0) pro1: d=(int)(data[i])-48; pro2: p=p*10+d; i++; if(isdigit(data[i])!=0)//是数值字符时 goto pro1; else if(data[i]!='\0'){//非结束标志 error();//错误处理程序 deal();//处理函数主体 } else s_next(); // 确定实型 } void s_next(){// 确定实型 i--;//退一个字符 CJ1="实型"; } void z_next(){//确定整型 i--;//退一个字符 CJ1="整型"; } void last(){// 计算 CJ2 CJ2=w*pow((double)10,e*p-j); cout<<CJ1<<": "<<CJ2<<endl;//输出 } void error(){//程序中错误处理程序 cout<<"\nError! Try again.."<<endl;//重新输入数据 cin>>data; p=0;w=0;j=0; //所有全局变量重新初始化 e=1;i=0;d=0; //exit(0); } void deal(){ check(data[0]);//判断输入的首字母是否是数字 deal_integer(data[i]);//处理识别符的整数部分 if(data[i]=='.') { deal_point(data[++i]);//用来处理小数部分 if(data[i]=='e'||data[i]=='E')//如果是e或E时 deal_index(data[++i]);//用来处理指数部分 else if(data[i]!='\0') { error();//错误处理程序 deal();//处理函数主体 } else s_next();// 确定实型 } else { if(data[i]=='e'||data[i]=='E')//如果是e或E时 { deal_index(data[++i]);//用来处理指数部分 //CJ1="整型"; } else if(data[i]!='\0'){ //非结束标志 error();//错误处理程序 deal();//处理函数主体 } else z_next();//确定整型 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值