返回顶部

收藏

词法分析

更多
#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
char ch;
char stra[256];
    struct baoliuzi
    {
        int no;
        char sx[20];
    }blz[17]={
        {1,"break"},
        {2,"char"},
        {3,"continue"},
        {4,"do"},
        {5,"double"},
        {6,"else"},
        {7,"extern"},
        {8,"float"},
        {9,"for"},
        {10,"int"},
        {11,"if"},
        {12,"long"},
        {13,"short"},
        {14,"static"},
        {15,"switch"},
        {16,"void"},
        {17,"while"}
    };

int IsLetter(char ch);
int IsDigit(char ch);
int Reserve(char str[]);
char Concat(char str[],char a);
void cifa();
void GetBC();
void qingling(char str[]);
void inPut();
void GetChar();

void main()
{
    GetChar();
    GetBC();
    while(ch!=' '&&ch!='\\n'&&ch!='\\t'){
    cifa();
    ch=getchar();
    GetBC();}
}

void cifa()
{
        char strToken[50]="";
    if(IsLetter(ch)){
        while(IsLetter(ch)||IsDigit(ch)){
            Concat(strToken,ch);
            GetChar();
        }
        Reserve(strToken);
        if(Reserve(strToken)){
            cout<<'<'<<Reserve(strToken)<<','<<strToken<<'>'<<endl;
            qingling(strToken);
        }
        else {
            cout<<'<'<<"70,"<<strToken<<'>'<<endl;
            qingling(strToken);
        }
    }
    else if(IsDigit(ch)){
        while(IsDigit(ch)){
            Concat(strToken,ch);
            GetChar();}
        cout<<'<'<<"80,"<<strToken<<'>'<<endl;
    }
    else
        switch(ch){
                 case '<' : GetChar();
                if(ch=='=') cout<<'<'<<"31,"<<"<="<<'>'<<endl;
                else if(ch=='>') cout<<'<'<<"32,"<<"<>"<<'>'<<endl;
                else cout<<'<'<<"30,"<<'<'<<'>'<<endl;
                break;
            case '>' : GetChar();
                if(ch=='=') {cout<<'<'<<"34,"<<">="<<'>'<<endl;break;}
                else {cout<<'<'<<"33,"<<'>'<<'>'<<endl;break;}
            case '=' :cout<<'<'<<"35,"<<'='<<'>'<<endl;break;
            case '(' :cout<<'<'<<"36,"<<'('<<'>'<<endl;break;
            case ')' :cout<<'<'<<"37,"<<')'<<'>'<<endl;break;
            case '*' :GetChar();
                if(ch=='*') {cout<<'<'<<"38,"<<"**"<<'>'<<endl;break;}
                else {cout<<'<'<<"39,"<<'*'<<'>'<<endl;break;}
            case ':' : GetChar();
                if(ch=='=') {cout<<'<'<<"40,"<<":="<<'>'<<endl;break;}
                else break;
            case '+' : cout<<'<'<<"41,"<<'+'<<'>'<<endl;break;
            case '-' : cout<<'<'<<"42,"<<'-'<<'>'<<endl;break;
            case '?' : cout<<'<'<<"43,"<<'?'<<'>'<<endl;break;
            case ',' : cout<<'<'<<"44,"<<','<<'>'<<endl;break;
            case ';' : cout<<'<'<<"45,"<<';'<<'>'<<endl;break;
            case '\\n' : break;
            default : cout<<'<'<<"0,"<<ch<<'>'<<endl;break;
                }
}

void GetBC()
{
    while(ch==' '||ch=='\\n'||ch=='\\t')
        GetChar();
}
int IsLetter(char ch)
{
    if((ch<=90)&&(ch>=65)||(ch<=122)&&(ch>=97))
        return 1;
    else return 0;
}

int IsDigit(char ch)
{
        if((ch<=57)&&(ch>=48))
            return 1;
        else return 0;
}

int Reserve(char str[])
{
    int i;
    for(i=0;i<17;i++){
          if(strcmp(str,blz[i].sx)==0)
            return blz[i].no;
    }
    return 0;
}

char Concat(char str[],char a)
{
    int i=0;
    i=strlen(str);
    str[i]=a;
    str[i+1]='\\0';
    return *str;
}

void qingling(char str[])
{
    int i,j;
    i=strlen(str);
    for(j=0;j<i;j++)
        str[i]='\\0';
}

void inPut()
{
    int i;
    for(i=0;ch!='$';i++)
    {stra[i]=ch;
    ch=getchar();}
}

void GetChar()
{
    int i=1;
    ch=stra[i];
    i++;
}
//该片段来自于http://outofmemory.cn

标签:c++,算法

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. bystander 发表 2013-04-16 00:42:58 模板优先级队列及堆排序(C++实现)
  2. dianlujitao 发表 2014-10-17 13:52:22 POJ 2388 Who’s in the Middle
  3. abyssss 发表 2014-05-20 03:23:39 数据结构 最小堆 数组实现
  4. dianlujitao 发表 2014-10-17 13:56:48 POJ 1611 The Suspects
  5. surgesoft 发表 2014-10-28 08:01:58 LeetCode OJ: Restore IP Addresses
  6. espace 发表 2015-07-18 17:43:14 Two Sum
  7. bystander 发表 2013-05-15 10:37:24 倒水问题求解(C++)
  8. dianlujitao 发表 2014-10-17 14:11:26 POJ 1328 Radar Installation
  9. bystander 发表 2013-04-01 10:12:37 [藏]关于B树的一篇文章
  10. lvfuyu 发表 2015-04-12 08:53:30 [hihocoder]矩阵快速幂
  11. lvfuyu 发表 2015-04-18 09:13:32 [hihocoder]二分查找
  12. leaver 发表 2013-05-27 02:55:49 求整数1-N范围和为N的所有组合

发表评论