www.pudn.com > zhp2006041602.rar > Fenxi.cpp
/////////////////////////////// //compiler.cpp //版权所有(C) 2002 飞翔鸟工作室 //////////////////////////////// # include# include # include # include # define MaxBuff 256 //菜单ID定义 #define IDM_MAINMENU 100 #define IDM_QUIT 101 #define IDM_ABOUT 102 #define IDD_ABOUT 200 //标识符 # define IDE_DIREDIT 1 # define IDB_LOOKBTN 2 # define IDB_PLAYBTN 3 # define IDE_SOURCEDIT 4 # define IDB_BORDERBTN 5 //句柄 static HWND hwndDirEdit; static HWND hwndLookBtn; static HWND hwndPlayBtn; static HWND hwndListView; static HWND hwndSourcEdit; static HWND hwndSourcList; static HWND hwndBorderBtn; //宏定义 #define ERRORSTRING 299 #define KEYWORD 300 #define NUMBER 301 #define BORDER 302 #define LOGIC 303 #define SPECIAL 304 #define OPERATION 305 #define STRING 306 #define MACRO 307 #define PLUS 308 #define SBB 309 #define DEVIDE 310 #define MULI 311 #define ADDEQU 312 #define SBBEQU 313 #define MULEQU 314 #define DEVEQU 315 #define DOUBLEYH 316 #define LEFTBIG 317 #define RIGHTBIG 318 #define DOUHAO 319 #define FENHAO 320 #define LEFTSMAL 321 #define RIGHTSMAL 322 #define LEFTMID 323 #define RIGHTMID 324 #define UNEQUL 325 #define NOT 326 #define JINGHAO 327 #define DOT 328 #define MAOHAO 329 #define WENHAO 330 #define PENCENT 331 #define XOR 332 #define BYTEAND 333 #define ANDEQU 334 #define ADDRESS 335 #define BYTEOR 336 #define AND 337 #define OR 338 #define BIG 339 #define SMALL 340 #define EQUAL 341 #define BIGEQU 342 #define SMALLEQU 343 #define LEFTMOVE 344 #define RIGHTMOVE 345 #define VALUE 346 #define ADDADD 347 #define SBBSBB 348 #define DYHAO 349 #define ZHUSHI 350 #define FLAG 351 #define DBP 352 //全局变量 char filename[256]; char fileTitle[256]; int iIndex; HINSTANCE hInstance; char string[50]; //关键字表定义 char keyword[31][10]={"auto","break","case","char", "continue","default","do","double", "else","extern","enum","extern","float","for", "goto","if","int","long", "register","return", "short","sizeof","static", "struct","switch","typedef","union", "unsigned","void","volatile","while"}; char Macro[5][10]={"include","define","ifdef","ifndef","endif"}; //全局函数 bool ShowOpenDlg(HWND hwnd); void AdjustWindow(HWND hwnd); void Scanner(FILE* fp); bool IsLetter(char ch); bool IsNumber(char ch); bool IsOperation(char ch); bool IsBorder(char ch); bool IsLogic(char ch); bool IsSpecial(char ch); bool IsKeyWord(char* ch); bool IsMacro(char* ch); void Out(int Identifier,char* str); //帮助对话框 LRESULT CALLBACK AboutProc(HWND,UINT,WPARAM,LPARAM); //窗口函数 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); ////////////////////////////////////////////////////////////////////////// //函数名:WinMain //功能:Windows 主函数 //参数说明:hInstance 当前实例句柄,由操作系统分配,hPrevInstance 前一个实例句柄,一般不使用 // lpcmdLine 命令行字符串,nCmdShow 窗口显示方式 /////////////////////////////////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpcmdLine,int nCmdShow) { static TCHAR szAppName[]=TEXT("Compiler"); static TCHAR szClassName[]=TEXT("CompileClass"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style =CS_HREDRAW|CS_VREDRAW; wndclass.lpfnWndProc =WndProc; wndclass.cbClsExtra =0; wndclass.cbWndExtra =0; wndclass.hInstance =hInstance; wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor =LoadCursor(NULL,IDC_ARROW); wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName =MAKEINTRESOURCE(IDM_MAINMENU); wndclass.lpszClassName =szClassName; //注册窗口类,如果失败则报警并退出 if(!RegisterClass(&wndclass)) { MessageBox(NULL,TEXT("This program requires Windows NT!"), szAppName,MB_ICONERROR); return 0; } //创建窗口 hwnd=CreateWindow( szClassName, TEXT("C语言词法分析器(Win32ApI版) 飞翔鸟工作室"),//窗口标题 WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX, //窗口风格 130, //窗口在屏幕的x坐标 50, //窗口在屏幕的y坐标 495, //窗口宽 450, //窗口高 NULL, //窗口的父窗口,无 NULL, //窗口菜单,无 hInstance,//窗口实例 NULL); //一般为NULL hInstance=hInstance; ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); //如果捕捉到消息则将其发往消息处理函数 while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } ////////////////////////////////////////////////////////////////////////// //函数名:WndProc //功能:窗口函数 //参数说明:hwnd 父窗口句柄,message 窗口消息,wParam 消息参数,lParam 消息参数 /////////////////////////////////////////////////////////////////////////// LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { RECT rect; GetClientRect(hwnd,&rect); FILE* fp; int cLineNumber; char szBuff[256]; switch(message) { case WM_CREATE: hwndListView=CreateWindow(WC_LISTVIEW,NULL,WS_CHILD|LVS_REPORT|LVS_EDITLABELS|WS_VISIBLE, 5,46,100,20,hwnd,NULL, hInstance,NULL); hwndDirEdit=CreateWindow("EDIT",NULL, WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|ES_LEFT|ES_READONLY, 0,0,0,0,hwnd,(HMENU)IDE_DIREDIT, hInstance,NULL); hwndLookBtn=CreateWindow(TEXT("button"),TEXT("打 开"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 265,3,70,25, hwnd,(HMENU)IDB_LOOKBTN,hInstance,NULL); hwndPlayBtn=CreateWindow(TEXT("button"),TEXT("词法分析"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 385,3,70,25, hwnd,(HMENU)IDB_PLAYBTN,hInstance,NULL); hwndSourcList=CreateWindow(WC_LISTVIEW,NULL,WS_CHILD|LVS_REPORT|LVS_EDITLABELS|WS_VISIBLE, 5,46,100,20,hwnd,NULL, hInstance,NULL); hwndBorderBtn=CreateWindow(TEXT("button"),TEXT(" "), WS_CHILD|WS_VISIBLE, 267,31,15,370, hwnd,(HMENU)IDB_BORDERBTN,hInstance,NULL); //获得当前文件 GetCurrentDirectory(256,filename); strcat(filename,"\\"); strcat(filename,"b.c"); SetWindowText(hwndDirEdit,filename); //初始化显示 //送往窗口显示 cLineNumber=0; if((fp=fopen(filename,"r"))!=NULL) { while(!feof(fp)) { int i=0,j; char ch; for(j=0;j<256;j++) //清空szBuff szBuff[j]='\0'; while((ch=fgetc(fp))!='\n'&&ch!=EOF) { szBuff[i++]=ch; } LV_ITEM lviS; lviS.mask = LVIF_TEXT; lviS.iItem =cLineNumber; lviS.iSubItem = 0; lviS.pszText = szBuff; ListView_InsertItem(hwndSourcList,&lviS); ListView_SetItemText(hwndListView,cLineNumber,1," "); cLineNumber++; } fclose(fp); } return 0; case WM_SIZE: AdjustWindow(hwnd); return 0; case WM_SETFOCUS: SetFocus(hwndDirEdit); return 0; case WM_COMMAND: switch(wParam) //处理控件消息 { case IDM_QUIT: PostQuitMessage(0); break; case IDM_ABOUT: DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUT), hwnd, (DLGPROC)AboutProc); break; case IDB_LOOKBTN: ListView_DeleteAllItems(hwndSourcList); //清除列表框中的原内容 ListView_DeleteAllItems(hwndListView); if(ShowOpenDlg(hwnd)==true) { SetWindowText(hwndDirEdit,filename); } //送往窗口显示 cLineNumber=0; fp=fopen(filename,"r"); if(fp==NULL) { MessageBox(hwnd,"打开文件错误","Error",NULL); return 0L; } else { while(!feof(fp)) { int i=0,j; char ch; for(j=0;j<256;j++) //清空szBuff szBuff[j]='\0'; while((ch=fgetc(fp))!='\n'&&ch!=EOF) { szBuff[i++]=ch; } LV_ITEM lviS; lviS.mask = LVIF_TEXT; lviS.iItem =cLineNumber; lviS.iSubItem = 0; lviS.pszText = szBuff; ListView_InsertItem(hwndSourcList,&lviS); ListView_SetItemText(hwndListView,cLineNumber,1," "); cLineNumber++; } fclose(fp); } break; case IDB_PLAYBTN: MessageBox(NULL,"Begin to play......"," ",MB_OK); //清空列表框,调用Scanner函数开始扫描*.c文件 ListView_DeleteAllItems(hwndListView); iIndex=0; fp=fopen(filename,"r"); if(fp==NULL) { MessageBox(hwnd,"打开文件错误","Error",NULL); return 0L; } Scanner(fp); //调用scanner函数开始扫描文件 fclose(fp); break; } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); } /////////////////////////////////////////////////////////////////////////// //函数名:AboutProc //功能:显示帮助对话框 //参数说明:hwnd 父窗口句柄,message 窗口消息,wParam 消息参数,lParam 消息参数 //////////////////////////////////////////////////////////////////////////// LRESULT CALLBACK AboutProc(HWND hwnd,//窗口句柄 UINT message,//窗口消息 WPARAM wParam,//消息参数 LPARAM lParam)//消息参数 { switch(message) { case WM_INITDIALOG: return 0; case WM_COMMAND: { switch(wParam) { case IDOK: EndDialog(hwnd,0); break; } } break; case WM_CLOSE: EndDialog(hwnd,0); break; } return 0; } ///////////////////////////////////// //函数名:ShowOpenDlg //功能:显示打开文件对话框 //参数说明:hwnd 父窗口句柄 ///////////////////////////////////// bool ShowOpenDlg(HWND hwnd) { char szFile[256]; char szFileTitle[256]; char defExtention[10]=".asm"; szFile[0]='\0'; OPENFILENAME ofn; ZeroMemory(&ofn, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = "C源程序(*.C)\0*.c\0"; ofn.nFilterIndex = 1; ofn.lpstrFileTitle =szFileTitle; ofn.nMaxFileTitle = 256; ofn.lpstrInitialDir = NULL; ofn.lpstrDefExt=defExtention; ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; if (GetOpenFileName(&ofn)==TRUE) { strcpy(filename,szFile); strcpy(fileTitle,szFileTitle); return true; } else return false; } /////////////////////////// //函数名:AdjustWindow //功能:调整窗口内控件大小 //参数说明:hwnd 父窗口句柄 /////////////////////////// void AdjustWindow(HWND hwnd) { RECT rect; LV_COLUMN lvc; LV_COLUMN lvcS; GetClientRect(hwnd,&rect); MoveWindow(hwndDirEdit,5,3,200,25,1); MoveWindow(hwndListView,283,30,199,rect.bottom-35,1); MoveWindow(hwndSourcList,3,30,263,rect.bottom-35,1); lvcS.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvcS.iSubItem = 0; lvcS.pszText = " 源 程 序"; lvcS.cx =264; lvcS.fmt = LVCFMT_LEFT; ListView_InsertColumn(hwndSourcList,0,&lvcS); lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvc.iSubItem = 0; lvc.pszText = " 字 符 串"; lvc.cx =104; lvc.fmt = LVCFMT_LEFT; ListView_InsertColumn(hwndListView,0,&lvc); lvc.iSubItem = 1; lvc.pszText = " 类 型"; lvc.cx =96; lvc.fmt = LVCFMT_LEFT; ListView_InsertColumn(hwndListView,1,&lvc); } //////////////////////// //函数名:Scanner //功能:扫描*.c文件 //参数说明:fp 文件指针 //////////////////////// void Scanner(FILE* fp) { char buffer[256]; //接收每一行的字符 char ch; //接收一个字符 int i; //数组控制变量 int j; //记录一行的字符数 int k; //字符串内控制变量 for(k=0;k<=49;k++) //string内存入\0,字符串的结束 { string[k]='\0'; } ///////////////////////////////////////////////////////////////////////////// ////////////////////////扫描整个文件,直到文件结束//////////////////////////// ///////////////////////////////////////////////////////////////////////////// while(!feof(fp)) { i=0; j=0; k=0; while((ch=fgetc(fp))!='\n'&&ch!=EOF) //接收一行字符 { buffer[i++]=ch; //取一行内容,存入buffer中 j++; } //////////////////////////////////////////////////// ///////////////////处理一行中的字符///////////////// //////////////////////////////////////////////////// for(i=0;i ,==///////////// if(IsLogic(ch)) { char oldch; char ooldch; ooldch=buffer[i-1]; oldch=string[k++]=buffer[i++]; ch=buffer[i]; if(!IsLogic(ch)) { if(oldch=='>') Out(BIG,string); if(oldch=='<') Out(SMALL,string); if(oldch=='=') //判断是否是算术赋值符 if(!(IsOperation(ooldch))) Out(VALUE,string); i--; //单回退 } /////////////双目运算//////////// if(IsLogic(ch)) { string[k++]=ch; //读进 if((oldch=='>')&&(ch=='=')) { Out(BIGEQU,string); } if((oldch=='>')&&(ch=='>')) { Out(RIGHTMOVE,string); } if((oldch=='<')&&(ch=='=')) { Out(SMALLEQU,string); } if((oldch=='<')&&(ch=='<')) { Out(LEFTMOVE,string); } if((oldch=='=')&&(ch=='=')) { Out(EQUAL,string); } }//////位移运算//// }////逻辑运算符//// for(k=0;k<49;k++) //清准备接收下一个字符串 string[k]='\0'; k=0; }////处理一行//// }/////扫描文件// } /////////////////////// //函数名:IsLetter //功能:判断是否为字母 //参数说明:ch 字母 /////////////////////// bool IsLetter(char ch) { if((ch>=65&&ch<=90)||(ch>=97&&ch<=122)) return true; else return false; } /////////////////////// //函数名:IsNumber //功能:判断是否为数字 //参数说明:ch 字母 /////////////////////// bool IsNumber(char ch) { if(ch>=48&&ch<=57) return true; else return false; } ///////////////////////// //函数名:IsOperation //功能:判断是否为操作符 //参数说明:ch 字母 ///////////////////////// bool IsOperation(char ch) { if(ch==42||ch==43||ch==45||ch==47) return true; else return false; } /////////////////////// //函数名:IsBorder //功能:判断是否为界符 //参数说明:ch 字母 ////////////////////// bool IsBorder(char ch) { if(ch==34||ch==39||ch==40||ch==41||ch==91||ch==93||ch==59||ch==123||ch==125||ch==44) return true; else return false; } //////////////////////////// //函数名:IsLogic //功能:判断是否为逻辑运算符 //参数说明:ch 字母 //////////////////////////// bool IsLogic(char ch) { if(ch==60||ch==61||ch==62) return true; else return false; } ///////////////////////////// //函数名:IsSpecial //功能:判断是否为特殊运算符 //参数说明:ch 字母 ///////////////////////////// bool IsSpecial(char ch) { if(ch==33||ch==35||ch==37||ch==38||ch==58||ch==63||ch==46||ch==94||ch==124) return true; else return false; } ////////////////////////// //函数名:IsKeyword //功能:判断是否为保留字 //参数说明:ch 字符串 ////////////////////////// bool IsKeyWord(char* ch) { int i; bool flag; flag=false; for(i=0;i<=31;i++) if(!strcmp(ch,keyword[i])) { flag=true; break; } if(flag) return true; else return false; } ////////////////////////// //函数名:IsMacro //功能:判断是否为宏定义 //参数说明:ch 字符串指针 /////////////////////////// bool IsMacro(char* ch) { int i; bool flag; flag=false; for(i=0;i<=4;i++) if(!strcmp(ch,Macro[i])) { flag=true; break; } if(flag) return true; else return false; } //////////////////////////////////////////////////////// //函数名:Out //功能:输出字符串 //参数说明:Identifier,字符串类型,str,待输出字符串 //////////////////////////////////////////////////////// void Out(int Identifier,char *str) { LV_ITEM lvi; lvi.mask = LVIF_TEXT; lvi.iItem = iIndex; lvi.iSubItem = 0; lvi.pszText = str; ListView_InsertItem(hwndListView,&lvi); switch(Identifier) //根据不同类型,在类型栏中输出对应文本 { case KEYWORD: ListView_SetItemText(hwndListView,iIndex,1,"关键字"); break; case MACRO: ListView_SetItemText(hwndListView,iIndex,1,"宏定义"); break; case BORDER: ListView_SetItemText(hwndListView,iIndex,1,"界符"); break; case NUMBER: ListView_SetItemText(hwndListView,iIndex,1,"数值"); break; case LOGIC: ListView_SetItemText(hwndListView,iIndex,1,"逻辑运算符"); break; case OPERATION: ListView_SetItemText(hwndListView,iIndex,1,"算术运算符"); case STRING: ListView_SetItemText(hwndListView,iIndex,1,"字符串"); break; case PLUS: ListView_SetItemText(hwndListView,iIndex,1,"加操作符"); break; case SBB: ListView_SetItemText(hwndListView,iIndex,1,"减操作符"); break; case MULI: ListView_SetItemText(hwndListView,iIndex,1,"星号"); break; case DEVIDE: ListView_SetItemText(hwndListView,iIndex,1,"整除操作符"); break; case ERRORSTRING: ListView_SetItemText(hwndListView,iIndex,1,"错误类型,请改正!"); break; case DOUBLEYH: ListView_SetItemText(hwndListView,iIndex,1,"双引号"); break; case LEFTBIG: ListView_SetItemText(hwndListView,iIndex,1,"左大扩号"); break; case RIGHTBIG: ListView_SetItemText(hwndListView,iIndex,1,"右大扩号"); break; case LEFTMID: ListView_SetItemText(hwndListView,iIndex,1,"左中扩号"); break; case RIGHTMID: ListView_SetItemText(hwndListView,iIndex,1,"右中扩号"); break; case LEFTSMAL: ListView_SetItemText(hwndListView,iIndex,1,"左小扩号"); break; case RIGHTSMAL: ListView_SetItemText(hwndListView,iIndex,1,"右小扩号"); break; case DOUHAO: ListView_SetItemText(hwndListView,iIndex,1,"逗号"); break; case FENHAO: ListView_SetItemText(hwndListView,iIndex,1,"分号"); break; case ADDEQU: ListView_SetItemText(hwndListView,iIndex,1,"加法赋值运算符"); break; case SBBEQU: ListView_SetItemText(hwndListView,iIndex,1,"减法赋值运算符"); break; case MULEQU: ListView_SetItemText(hwndListView,iIndex,1,"乘法赋值运算符"); break; case DEVEQU: ListView_SetItemText(hwndListView,iIndex,1,"除法赋值运算符"); break; case AND: ListView_SetItemText(hwndListView,iIndex,1,"与运算符"); break; case OR: ListView_SetItemText(hwndListView,iIndex,1,"或运算符"); break; case NOT: ListView_SetItemText(hwndListView,iIndex,1,"取反运算符"); break; case UNEQUL: ListView_SetItemText(hwndListView,iIndex,1,"逻辑运算符-不等"); break; case BYTEOR: ListView_SetItemText(hwndListView,iIndex,1,"位或运算符"); break; case BYTEAND: ListView_SetItemText(hwndListView,iIndex,1,"位与运算符"); break; case PENCENT: ListView_SetItemText(hwndListView,iIndex,1,"百分号"); break; case XOR: ListView_SetItemText(hwndListView,iIndex,1,"位异或运算符"); break; case MAOHAO: ListView_SetItemText(hwndListView,iIndex,1,"冒号"); break; case WENHAO: ListView_SetItemText(hwndListView,iIndex,1,"问号"); break; case JINGHAO: ListView_SetItemText(hwndListView,iIndex,1,"井号"); break; case DOT: ListView_SetItemText(hwndListView,iIndex,1,"结构成员运算符"); break; case BIG: ListView_SetItemText(hwndListView,iIndex,1,"大于号"); break; case SMALL: ListView_SetItemText(hwndListView,iIndex,1,"小于号"); break; case BIGEQU: ListView_SetItemText(hwndListView,iIndex,1,"大于等于"); break; case SMALLEQU: ListView_SetItemText(hwndListView,iIndex,1,"小于等于"); break; case EQUAL: ListView_SetItemText(hwndListView,iIndex,1,"逻辑等于号"); break; case LEFTMOVE: ListView_SetItemText(hwndListView,iIndex,1,"左移"); break; case RIGHTMOVE: ListView_SetItemText(hwndListView,iIndex,1,"右移"); break; case VALUE: ListView_SetItemText(hwndListView,iIndex,1,"赋值运算符"); break; case ADDADD: ListView_SetItemText(hwndListView,iIndex,1,"自加运算符"); break; case SBBSBB: ListView_SetItemText(hwndListView,iIndex,1,"自减运算符"); break; case DYHAO: ListView_SetItemText(hwndListView,iIndex,1,"单引号"); break; case ZHUSHI: ListView_SetItemText(hwndListView,iIndex,1,"注释符号"); break; case FLAG: ListView_SetItemText(hwndListView,iIndex,1,"标识符"); break; case ADDRESS: ListView_SetItemText(hwndListView,iIndex,1,"特殊操作符&"); break; case DBP: ListView_SetItemText(hwndListView,iIndex,1,"二维指针"); break; } iIndex++; }