词法分析程序(原创!)

来源:互联网 发布:淘宝管控记录异常订单 编辑:程序博客网 时间:2024/04/30 10:11
/************************************************************************
                        词法分析程序

  功能:用于对输入的字符串进行分类(如果是无符号数,就对它记值;否则值为空)
类别码:1-无符号数,2-‘+’,3-‘-’,4-‘*’,5-‘/’,6-‘(’,7-‘)’        
***************************************************************************/
#include<iostream.h>
#include <windows.h>
#include <conio.h>

/*清屏函数定义*/
int clrscr()
{
  HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hndl, &csbi);
  DWORD written;
  DWORD N = csbi.dwSize.X * csbi.dwCursorPosition.Y +
            csbi.dwCursorPosition.X + 1;
  COORD curhome = {0,0};
  FillConsoleOutputCharacter(hndl, ' ', N, curhome, &written);
  csbi.srWindow.Bottom -= csbi.srWindow.Top;
  csbi.srWindow.Top = 0;
  SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
  SetConsoleCursorPosition(hndl, curhome);
  return 0;
}
void main(){
    char input[20];               //定义输入缓冲区
    char s,CurrentState;          //定义s用于接收判断是否继续,CurrentState判断当前状态
    unsigned int i,oldi,j;        //定义临时变量,用于数组操作
    do{
        cout<<"/t ---------------------------/n";
        cout<<"/t|       词法分析程序        |/n";
        cout<<"/t ---------------------------/n";
        cout<<"/t请输入:/n";
        cout<<"/t ";
        cin>>input;                  //屏幕提示输入
        i=0,oldi=0,CurrentState='0';
        cout<<"/tclassno/t/tvalue/n";//输出列属性
        do{
            switch(CurrentState){    //判断当前状态
            case '0':
                switch(input[i]){    //判断输入数值
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':   CurrentState='1';//改变状态,指向下一个字符
                    i++;
                    break;
                case '.':   CurrentState='3';
                    i++;
                    break;                    
                case '+':   if(i-oldi>0){    //判断如果符号前有数字,就输出
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<2<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '-':   if(i-oldi>0){    
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<3<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '*':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<4<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '/':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<5<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '(':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<6<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case ')':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<7<<endl;
                    CurrentState='0';
                    oldi=++i; break;                         
                default:    if(input[i]=='/0')    
                            {
                                if(i-oldi>0){     //判断如果符号前有数字,就输出
                                    cout<<"/t"<<1<<"/t/t";
                                    for(j=oldi;j<i;j++)
                                        cout<<input[j];
                                    cout<<endl;}
                                cout<<endl<<"/t Finished!"<<endl;
                                goto exit;
                            }else{                //非法字符判断
                                cout<<"/t ERROR!"<<endl; goto exit;
                            }   break;
                }            
                break;
            case '1':
                switch(input[i]){
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':   CurrentState='1';
                    i++;
                    break;
                case 'E':   CurrentState='4';
                    i++;
                    break;
                case '.':   CurrentState='2';
                    i++;
                    break;
                case '+':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<2<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '-':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<3<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '*':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<4<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '/':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<5<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '(':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<6<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case ')':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<7<<endl;
                    CurrentState='0';
                    oldi=++i; break;                        
                default:    if(input[i]=='/0')   //1、2、6状态可以是终态
                            {
                                if(i-oldi>0){
                                    cout<<"/t"<<1<<"/t/t";
                                    for(j=oldi;j<i;j++)
                                        cout<<input[j];
                                    cout<<endl;}
                                cout<<endl<<"/t Finished!"<<endl;
                                goto exit;
                            }else{
                                cout<<"/t ERROR!"<<endl; goto exit;
                            }   break;            
                }break;
            case '2':
                switch(input[i]){
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':   CurrentState='1';
                    i++;
                    break;
                case 'E':   CurrentState='4';
                    i++;
                    break;                    
                case '+':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<2<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '-':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<3<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '*':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<4<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '/':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<5<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '(':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<6<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case ')':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<7<<endl;
                    CurrentState='0';
                    oldi=++i; break;                        
                default:    if(input[i]=='/0')
                            {
                                if(i-oldi>0){
                                    cout<<"/t"<<1<<"/t/t";
                                    for(j=oldi;j<i;j++)
                                        cout<<input[j];
                                    cout<<endl;}
                                cout<<endl<<"/t Finished!"<<endl;
                                goto exit;
                            }else{
                                cout<<"/t ERROR!"<<endl; goto exit;
                            }   break;            
                }        
                break;
            case '3':
                switch(input[i]){
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':   CurrentState='2';
                    i++;
                    break;                        
                case '+':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<2<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '-':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<3<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '*':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<4<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '/':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<5<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '(':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<6<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case ')':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<7<<endl;
                    CurrentState='0';
                    oldi=++i; break;                         
                default:    if(input[i]=='/0')     //3、4、5不能是终态
                            {
                                cout<<"/t ERROR!"<<endl; goto exit;
                            }   break;
                }        
                break;
            case '4':
                switch(input[i]){
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':   CurrentState='6'; i++;break;
                case '+':   CurrentState='5'; i++;break;  
                case '-':   CurrentState='5'; i++;break;                        
                case '*':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<4<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '/':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<5<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '(':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<6<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case ')':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<7<<endl;
                    CurrentState='0';
                    oldi=++i; break;                        
                default:    if(input[i]=='/0')
                            {
                                cout<<"/t ERROR!"<<endl; goto exit;
                            }   break;
                }break;
            case '5':
                switch(input[i]){
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':   CurrentState='6';
                    i++;
                    break;                        
                case '+':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<2<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '-':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<3<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '*':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<4<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '/':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<5<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '(':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<6<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case ')':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<7<<endl;
                    CurrentState='0';
                    oldi=++i; break;                        
                default:    if(input[i]=='/0')
                            {
                                cout<<"/t ERROR!"<<endl; goto exit;
                            }   break;
                }break;
            case '6':
                switch(input[i]){
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':   CurrentState='6';
                    i++;
                    break;                        
                case '+':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<2<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '-':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<3<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '*':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<4<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '/':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<5<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case '(':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<6<<endl;
                    CurrentState='0';
                    oldi=++i; break;  
                case ')':   if(i-oldi>0){
                    cout<<"/t"<<1<<"/t/t";
                    for(j=oldi;j<i;j++)
                        cout<<input[j];
                    cout<<endl;}
                    cout<<"/t"<<7<<endl;
                    CurrentState='0';
                    oldi=++i; break;                                             
                default:    if(input[i]=='/0')
                            {
                                if(i-oldi>0){
                                    cout<<"/t"<<1<<"/t/t";
                                    for(j=oldi;j<i;j++)
                                        cout<<input[j];
                                    cout<<endl;}
                                cout<<endl<<"/t Finished!"<<endl;
                                goto exit;
                            }else{
                                cout<<"/t ERROR!"<<endl; goto exit;
                            }   break;
                }break;
            }
            }while(1);
            
exit:   cout<<"/n/t请输入e推出 任意键继续/n";
        cout<<"/t";
        cin>>s;
        if(s!='e')
            clrscr();
    }while(s!='e');
}

原创粉丝点击