返回顶部

收藏

词法分析

更多
#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-25 21:20:53二叉树深度优先遍历说明和c++的实现 by 玉开Sir
  2. 2014-02-05 13:32:07C++算法之图的结构 by walker30
  3. 2014-02-16 18:20:02C++解决八皇后问题 by aiheng1988
  4. 2014-03-05 16:11:07C++挑选一组数中最大的N个数 by niutao.linux
  5. 2014-05-13 19:31:43排序算法 by 蟋蟀哥
  6. 2014-05-14 18:05:23递归方法实现全排列 by 童学芬
  7. 2014-05-22 12:36:50单链表的各种操作 by 千万不要郁闷
  8. 2014-05-28 14:39:00C++算法之线性队列 by 小项
  9. 2014-06-30 20:50:04线性表 by lucasli
  10. 2014-07-10 10:49:57快速排序 [C遮言泛型版] by sxgkwei
  11. 2014-08-08 12:12:47sudoku求解数独 by aiheng1988

发表评论