⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanner.cpp

📁 编译原理词法分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// scanner.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;							// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

HWND hWndEdit;
HWND hWndCode;
HWND hWndPreDeal;
HWND hWndAnalysis;

char* SourceCode;
char* ComputerCode;
char* PreDeal;
char Result[10000];

bool isOpen = false;
bool isPreDeal = false;

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
char* 				FileRead(HWND hWnd,HWND BOXNAME,char* str);
void                preDeal(HWND BOXNAME);
void				analysis(HWND BOXNAME);
int					isKeyWord(char buf[256]);
void				PushIntoResult(char buf[256]);
int					isSignal(char buf[2]);
bool				isDouble(char a,char b);



int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;
	
	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_SCANNER, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);
	
	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}
	
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SCANNER);
	
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	
	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	
	wcex.cbSize = sizeof(WNDCLASSEX); 
	
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_SCANNER);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)GetStockObject(GRAY_BRUSH);
	wcex.lpszMenuName	= (LPCSTR)IDC_SCANNER;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
	
	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	
	hInst = hInstance; // Store instance handle in our global variable
	
	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
		100, 100, 860, 440, NULL, NULL, hInstance, NULL);
	
	if (!hWnd)
	{
		return FALSE;
	}
	
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	hWndCode = ::CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",ES_READONLY|ES_WANTRETURN|WS_HSCROLL|WS_VSCROLL|WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL,10,50,200,320,hWnd,(HMENU)(1),hInst,NULL);
	hWndEdit = ::CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",ES_READONLY|ES_WANTRETURN|WS_HSCROLL|WS_VSCROLL|WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL,220,50,200,320,hWnd,(HMENU)(1),hInst,NULL);
	hWndPreDeal = ::CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",ES_READONLY|ES_WANTRETURN|WS_HSCROLL|WS_VSCROLL|WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL,430,50,200,320,hWnd,(HMENU)(1),hInst,NULL);
	hWndAnalysis = ::CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",ES_READONLY|ES_WANTRETURN|WS_HSCROLL|WS_VSCROLL|WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL,640,50,200,320,hWnd,(HMENU)(1),hInst,NULL);
	
	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	
	switch (message) 
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		case IDM_OPEN:
			SourceCode = FileRead(hWnd,hWndEdit,SourceCode);
			isOpen = true;
			isPreDeal = false;
			break;
		case IDM_CODE:
			FileRead(hWnd,hWndCode,ComputerCode);
			break;
		case IDM_PREDEAL:
			if(isOpen == true)
			{
				preDeal(hWndPreDeal);
				isPreDeal = true;
			}
			else
			{
				::MessageBox(hWnd,"请先打开原程序!","错误!",NULL);
			}
			break;
		case IDM_ANALYSIS:
			if(isOpen == true && isPreDeal == true)
			{
				analysis(hWndAnalysis);break;
			}
			if(isOpen == false)
			{
				::MessageBox(hWnd,"请先打开原程序!","错误!",NULL);break;
			}
			if(isPreDeal == false)
			{
				::MessageBox(hWnd,"请先进行预编译!","错误!",NULL);
			}
			
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			RECT rt;
			GetClientRect(hWnd, &rt);
			//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			::SetTextColor(hdc,RGB(0,204,255));
			::SetBkMode(hdc,TRANSPARENT);
			::TextOut(hdc,70,20,"机内表示符",10);
			::TextOut(hdc,290,20,"源程序",6);
			::TextOut(hdc,500,20,"预处理",6);
			::TextOut(hdc,720,20,"结果",4);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		return TRUE;
		
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
    return FALSE;
}

char* FileRead(HWND hWnd,HWND BOXNAME,char* str)
{
	OPENFILENAME ofn;
	
	char cTemp[256] = {0};
	
	ZeroMemory(&ofn, sizeof(ofn));
	
	ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hWnd;
	ofn.nMaxFile = 256;
	ofn.nMaxFileTitle = 256;
	ofn.lpstrFile = cTemp;
    ofn.lpstrFilter = "文本文档 (*.txt)\0*.txt\0All files\0*.*\0\0";
    ofn.lpstrDefExt = "txt";
	ofn.Flags = OFN_FILEMUSTEXIST;
	
	if ( ::GetOpenFileName(&ofn) == false )
	{
		return str;
	}
	
	
	FILE * fp_in;
	char c;
	int TotalNum = 0;
	int ReturnNum =0;
	int i = 0;
	fp_in = fopen(ofn.lpstrFile, "r");
	
	while(!feof(fp_in))
	{
		c = fgetc(fp_in);
		TotalNum++;
		if(c == '\n')
		{
			ReturnNum++;
		}
	}
	
	str = (char*)calloc(TotalNum+ReturnNum,sizeof(char));
	rewind(fp_in);
	
	while(!feof(fp_in))
	{
		c = fgetc(fp_in);
		if(c == '\n')
		{
			str[i] = '\r';
			i++;
			str[i] = '\n';
		}
		else
		{
			str[i] = c;
		}
		
		i++;
	}
	str[TotalNum+ReturnNum-1] = '\0';
	
	SetWindowText(BOXNAME,str);
	fclose(fp_in);
	return str;
}

void preDeal(HWND BOXNAME)
{
	int length,i,j;
	length = strlen(SourceCode);
	PreDeal = (char*)calloc(length,sizeof(char));
	int first = -1;
	int last = -1;
	
	//去处注释
	for(i=0;i<length;i++)
	{
		if(SourceCode[i] == '/' && SourceCode[i+1] == '*')
		{
			first = i;
		}
		if(SourceCode[i] == '*' && SourceCode[i+1] == '/' && first!=-1)
		{
			last = i+1;
			for(int j = first;j<=last;j++)
			{
				SourceCode[j] = ' ';
			}
			
			first = -1;
			last = -1;
		}
	}
	
	//去除多余的回车
	first = 0;
	for(i=0;i<length;i++)
	{
		if(SourceCode[i] == '\r' && SourceCode[i+1] == '\n')
		{
			if(first == 0)
			{
				first = 1;
				i++;
			}
			else
			{
				SourceCode[i] = ' ';
				i++;
				SourceCode[i] = ' ';
			}
		}
		else
		{
			first = 0;
		}
	}
	
	//去处多余的空格
	int begin = 0;
	first = 0;
	j = 0;
	for(i=0;i<length;i++)
	{
		if(begin ==0)
		{
			if(SourceCode[i] == ' ' || SourceCode[i] == '\r' || SourceCode[i] == '\n')
			{
			}
			else
			{
				begin = 1;
				PreDeal[j] = SourceCode[i];
				j++;
			}
		}
		else
		{
			if(SourceCode[i] == ' ')
			{
				if(first == 0)
				{
					first++;
					if(SourceCode[i-1] != '\n')
					{
						PreDeal[j] = SourceCode[i];
						j++;										
					}
				}
				else
				{
					first++;
				}
			}
			else
			{
				first = 0;
				if(SourceCode[i] != '\t')
				{
					PreDeal[j] = SourceCode[i];
					j++;													
				}
			}
		}
		
	}
	
	SetWindowText(BOXNAME,PreDeal);
}

void analysis(HWND BOXNAME)
{
	char buf[256];
	char number[2];
	int length;
	int j = 0;
	length = strlen(PreDeal);
	int code;
	
	for(int k = 0;k < 256;k++)
	{
		Result[k] = '\0';
	}

	for(int i = 0;i < length;i++)
	{
		if(isspace(PreDeal[i]))
		{
		}
		else
		{
			if(isalpha(PreDeal[i]))
			{
				j = 0;
				for(int k = 0;k < 256;k++)
				{
					buf[k] = '\0';
				}
				buf[j] = PreDeal[i];
				j++;
				
				i++;
				while(isalpha(PreDeal[i]))
				{
					buf[j] = PreDeal[i];
					j++;
					i++;
				}
				
				if(isdigit(PreDeal[i]))
				{
					buf[j] = PreDeal[i];
					j++;
					
					i++;
					while(isalnum(PreDeal[i]))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -