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

📄 regother.cpp

📁 C语言词法分析器 实现简单的词法分析
💻 CPP
字号:

#include <windows.h>
#include "commendef.h"

static char ValidChar[] = "()[]{};:,?#";

STACK_NODE* g_pStackTop = 0;

int IsValidOtherChar(char ch)
{
	int i,len;
	len = strlen(ValidChar);
	for(i = 0; i < len; i++)
		if (ch == ValidChar[i]) return i;
	return -1;
}

bool RecogniseOtherChar(char** pStr,RECOG_RESULT* pResult)
{
	char ch;
	int  no;
	STACK_NODE* pNewNode;

	pResult->WordType = WORD_OTHER;
	pResult->Right = true;	
	ch = **pStr;
	no = IsValidOtherChar(ch);
	if (no < 0)		pResult->Right = false;
	else
	{		
		pResult->Value.OtherSymbol[0] = ch;
		pResult->Value.OtherSymbol[1] = 0;
		if (no / 2 <= 2) // ()[]{} 三种括号需要配对
		{
			if (no % 2 == 0)  // 左括号
			{
				if (g_pStackTop != NULL && g_pStackTop->sign == ch)
					(g_pStackTop->num)++;
				else
				{
					pNewNode = new STACK_NODE;
					pNewNode->sign = ch;
					pNewNode->num  = 1;
					pNewNode->next = g_pStackTop;
					g_pStackTop = pNewNode;
				}
			}
			else   // 右括号
			{   
				if (g_pStackTop == NULL)  //先出现右括号					 	
					pResult->Right = false; 									
				else
				{
					int no1,no2;
					no1 = IsValidOtherChar(g_pStackTop->sign) / 2;
					no2 = IsValidOtherChar(ch) / 2;

					if (no1 != no2)     // 括号不匹配
						pResult->Right = false;
					else if (--(g_pStackTop->num) == 0)      // 括号匹配,弹出栈顶符号
					{
						pNewNode = g_pStackTop->next;
						delete g_pStackTop;
						g_pStackTop = pNewNode;
					}
				}				
			}
		}		
	}
	(*pStr)++;
	
	return pResult->Right;
}

⌨️ 快捷键说明

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