返回顶部

收藏

词法分析

更多
#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. 2013-07-17 19:11:32c++动态规划算法计算网络的最长路线和最短路线 by lucasli
  2. 2014-02-12 11:56:53C++算法之数星星 by lucasli
  3. 2014-02-21 12:23:12C++实例解析哈夫曼树 by lucasli
  4. 2014-03-30 15:29:42C++算法之链表排序 by 跳跳虎
  5. 2014-04-24 17:42:39C++算法之链表重合 by lucasli
  6. 2014-05-20 10:42:08双向循环列表 by Kevin.
  7. 2014-05-27 14:13:38网络中两点最短路径 Dijkstra 算法 by 千万不要郁闷
  8. 2014-06-08 10:40:28数字的全排列 by sxgkwei
  9. 2014-06-24 14:17:58C++ 算法之单词统计 by 小项
  10. 2014-07-06 21:23:00死锁检测算法 by 童学芬
  11. 2014-07-20 12:34:50傻瓜递归 by 童学芬

发表评论