日常练习代码

根据给出的正则式,构建分词程序,完成相应的分词任务,并返回所有单词以及单词类别。
正则式:
<关键字> int |for| while | do | return | break | continue
<运算符> + | - | * | / |==| < | <= |!= | > | >=
<界符> , | ; | ( | ) | {|}
<标识符> letter (letter | digit)*
<整型常数> digit digit*
<小数> digit digit* . digit digit*
代码:

#include <iostream>
#include<stdio.h>
using namespace std;
#define MAX 6
char ch =' ';
string key[MAX]={"int","for","while","do","return","break"};

int Iskey(string c)    //关键字判断
{
   int i;
   for(i=0;i<MAX;i++)
   {
       if(key[i] == c)
        return 1;
   }
   return 0;
}
int IsLetter(char c)   //判断是否为字母
{
    if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A')))
        return 1;
    else
        return 0;
}
int IsDigit(char c)    //判断是否为数字
{
    if(c>='0'&&c<='9')
        return 1;
    else
        return 0;
}
void analyse(FILE *fpin)  //词法分析
{
    string arr="";
    while((ch=fgetc(fpin))!=EOF)
    {
        arr="";
        if(ch==' '||ch=='\t'||ch=='\n'){
        }
        else if(IsLetter(ch))
        {
            while(IsLetter(ch)||IsDigit(ch))
            {
                if((ch<='Z')&&(ch>='A'))
                    ch=ch+32;
                arr=arr+ch;
                ch=fgetc(fpin);
            }
            fseek(fpin,-1L,SEEK_CUR);
            if(Iskey(arr))
            {
                cout<<arr<<"\t$关键字"<<endl;
            }
            else cout<<arr<<"\t$标识符"<<endl;
        }

        else if(IsDigit(ch))
        {
            int decimal = 0;
            while(IsDigit(ch)||(ch=='.'&&IsDigit(fgetc(fpin))))
            {
                if(ch=='.')
                {
                    decimal = 1;
                }
                arr=arr+ch;
                ch=fgetc(fpin);
            }
            fseek(fpin,-1L,SEEK_CUR);
            if(decimal == 1){
                cout<<arr<<"\t$小数"<<endl;
            }
            else
                cout<<arr<<"\t$整型常数"<<endl;
        }
        else switch(ch)
        {
            case'+' :
            case'-' :
            case'*' :
            case'/' :cout<<ch<<"\t$运算符"<<endl;break;
            case'(' :
            case')' :
            case';' :
            case',' :
            case'{' :
            case'}' :cout<<ch<<"\t$界符"<<endl;break;
            case'=' :
            {
                ch=fgetc(fpin);
                if(ch=='=')
                    cout<<"=="<<"\t$运算符"<<endl;
                else if(ch=='!')
                {
                    cout<<"!="<<"\t$运算符"<<endl;
                }
                else
                {
                    cout<<"="<<"\t$运算符"<<endl;;
                    fseek(fpin,-1L,SEEK_CUR);
                }
            }
            break;
            case'>' :
            {
                ch=fgetc(fpin);
                if(ch=='=')
                    cout<<">="<<"\t$运算符"<<endl;
                else
                {
                    cout<<">"<<"\t$运算符"<<endl;
                    fseek(fpin,-1L,SEEK_CUR);
                }
            }
            break;
            case'<' :
            {
                ch=fgetc(fpin);
                if(ch=='=')
                    cout<<"<="<<"\t$运算符"<<endl;
                else
                {
                    cout<<"<"<<"\t$运算符"<<endl;
                    fseek(fpin,-1L,SEEK_CUR);
                }
            }
            break;
            default :
                cout<<ch<<"\t$无法识别字符"<<endl;
        }
    }
}
int main()
{
    char in_fn[30];
    FILE *fpin;
    cout<<"请输入源文件名(包括路径和后缀名):";
    for(;;)
    {
        cin>>in_fn;
        if((fpin=fopen(in_fn,"r"))!=NULL)
            break;
        else
            cout<<"文件路径错误!请输入源文件名(包括路径和后缀名):";
    }
    cout<<"\n********************分析如下********************"<<endl;
    analyse(fpin);
    fclose(fpin);
    return 0;
}

运行结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值