📄 source.txt
字号:
///////////////////////////////
//compiler.cpp
//版权所有(C) 2002 飞翔鸟工作室
////////////////////////////////
# include <windows.h>
# include <stdio.h>
# include <string.h>
# include <commctrl.h>
# 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<j;i++) //j为一行的字符总数
{
ch=buffer[i]; //取一个字符
////////////////////////////////////
//{/////////////如果是字母///////}//
if(IsLetter(ch))
{
while(IsLetter(ch)||IsNumber(ch)||ch=='_'||ch=='.') //只要不是空格和括号
{
string[k++]=buffer[i++]; //取一个完整的字符串
ch=buffer[i];
}
if(IsKeyWord(string))
Out(KEYWORD,string);
if(IsMacro(string))
Out(MACRO,string);
if(!IsKeyWord(string)&&!IsMacro(string))
Out(FLAG,string);
for(k=0;k<=49;k++) //清空string,存入\0,字符串的结束
{
string[k]='\0';
}
k=0; //k回退
}
////////////////////////////////////
//{////////////如果是数字////////}//
if(IsNumber(ch))
{
while(IsNumber(ch)||ch=='.')
{
string[k++]=buffer[i++];
ch=buffer[i];
}
Out(NUMBER,string);
for(k=0;k<=49;k++) //清空string,存入\0,字符串的结束
{
string[k]='\0';
}
k=0; //k回退
}
//////////////////////////////////////////////
//{//////////////////如果是操作符//////////}//
if(IsOperation(ch))
{
char oldch;
oldch=string[k++]=buffer[i++];
ch=buffer[i]; //读下一个
///////单运算符/////////
if(!(IsOperation(ch)||IsLogic(ch)))
{
if(oldch=='+')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -