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

📄 my.l

📁 flex 词法分析器 识别token单元
💻 L
字号:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>


int currentRow = 1;
int currentCol = 1;
int count = 0;			//表示当前数组下标															
int size = 100;			//表示当前数组的长度


struct _tokenRecord{
	char * TokenType;
   	char * tokenText;
    	int row;
    	int col;
};

typedef struct _tokenRecord tokenRecord;
tokenRecord ** Token;


// 识别token并申请空间记录信息
void recognizeToken(char TokenType[], char tokenText[], int row, int col);
// 输出token信息
void outputToken();
// 释放申请的资源
void Free();

%}

Digit			[0-9]
Digits          	Digit+
Letter          	[a-zA-Z]
Integer         	[+-]?(0|[1-9]({Digit})*)
ThenKey         	then
ElseKey         	else
IfKey           	if
VarKey          	var
IntKey          	int
ArrayKey        	array
BeginKey        	begin
EndKey          	end
WhileKey        	while
DoKey           	do
ReadKey         	read
WriteKey        	write
OfKey           	of
FunKey          	function
CallKey         	call
DereKey         	"#"
PointerKey      	Pointer
MEMAllocKey     	malloc
MEMFreeKey      	free
Identifier      	{Letter}({Letter}|{Digit})*     
NewLine         	\n
Tab             	\t
Space           	[ ]
NotSym          	not
AssignSym       	"="
SemicolSym      	";"
ColonSym        	":"
CommaSym        	","
FullStopSym     	"."
GOp             	">"
GeOp            	">="
LeOp            	"<="
LessOp          	"<"
EqOp            	"=="
NeOp            	"!="
AddOp           	"+"
MinusOp         	"-"
MulOp           	"*"
DivOp           	"/"
AndOp           	and
OrOp            	or
XorOp           	xor
LBracket        	"("
RBracket        	")"
LSBracket       	"["
RSBracket       	"]"

%%

{Integer}       	{ recognizeToken("Integer",yytext,currentRow,currentCol); }
{NewLine}        	{ currentRow++; currentCol = 1; }
{Space}          	{ currentCol++; if(currentCol > 80){ currentRow++; currentRow = 1; } }
{Tab}            	{ currentCol += 6; if(currentCol > 80) { currentRow++; currentRow -= 80; } }
{NotSym}         	{ recognizeToken("NotSym",yytext,currentRow,currentCol); }
{AssignSym}      	{ recognizeToken("AssignSym",yytext,currentRow,currentCol); }
{SemicolSym}     	{ recognizeToken("SemicolSym",yytext,currentRow,currentCol); }
{ColonSym}       	{ recognizeToken("ColonSym",yytext,currentRow,currentCol); }
{CommaSym}       	{ recognizeToken("CommaSym",yytext,currentRow,currentCol); }
{FullStopSym}    	{ recognizeToken("FullStopSym",yytext,currentRow,currentCol); }
{GOp}            	{ recognizeToken("GOp",yytext,currentRow,currentCol); }
{GeOp}           	{ recognizeToken("GeOp",yytext,currentRow,currentCol); }
{LeOp}           	{ recognizeToken("LeOp",yytext,currentRow,currentCol); }
{LessOp}         	{ recognizeToken("LessOp",yytext,currentRow,currentCol); }
{EqOp}           	{ recognizeToken("EqOp",yytext,currentRow,currentCol); }
{NeOp}           	{ recognizeToken("NeOp",yytext,currentRow,currentCol); }
{AddOp}          	{ recognizeToken("AddOp",yytext,currentRow,currentCol); }
{MinusOp}        	{ recognizeToken("MinusOp",yytext,currentRow,currentCol); }
{MulOp}          	{ recognizeToken("MulOp",yytext,currentRow,currentCol); }
{DivOp}          	{ recognizeToken("DivOp",yytext,currentRow,currentCol); }
{AndOp}          	{ recognizeToken("AndOp",yytext,currentRow,currentCol); }
{OrOp}           	{ recognizeToken("OrOp",yytext,currentRow,currentCol); }
{XorOp}          	{ recognizeToken("XorOp",yytext,currentRow,currentCol); }
{LBracket}       	{ recognizeToken("LBracket",yytext,currentRow,currentCol); }
{RBracket}       	{ recognizeToken("RBracket",yytext,currentRow,currentCol); }
{LSBracket}      	{ recognizeToken("LSBracket",yytext,currentRow,currentCol); }
{RSBracket}      	{ recognizeToken("RSBracket",yytext,currentRow,currentCol); }
{ThenKey}        	{ recognizeToken("ThenKey",yytext,currentRow,currentCol); }
{ElseKey}        	{ recognizeToken("ElseKey",yytext,currentRow,currentCol); }
{IfKey}          	{ recognizeToken("IfKey",yytext,currentRow,currentCol); }
{VarKey}         	{ recognizeToken("VarKey",yytext,currentRow,currentCol); }
{IntKey}         	{ recognizeToken("IntKey",yytext,currentRow,currentCol); }
{ArrayKey}       	{ recognizeToken("ArrayKey",yytext,currentRow,currentCol); }
{BeginKey}       	{ recognizeToken("BeginKey",yytext,currentRow,currentCol); }
{EndKey}         	{ recognizeToken("EndKey",yytext,currentRow,currentCol); }
{WhileKey}       	{ recognizeToken("WhileKey",yytext,currentRow,currentCol); }
{OfKey}          	{ recognizeToken("OfKey",yytext,currentRow,currentCol); }
{DoKey}          	{ recognizeToken("DoKey",yytext,currentRow,currentCol); }
{ReadKey}        	{ recognizeToken("ReadKey",yytext,currentRow,currentCol); }
{WriteKey}       	{ recognizeToken("WriteKey",yytext,currentRow,currentCol); }
{FunKey}         	{ recognizeToken("FunKey",yytext,currentRow,currentCol); }
{CallKey}        	{ recognizeToken("CallKey",yytext,currentRow,currentCol); }
{DereKey}        	{ recognizeToken("DereKey",yytext,currentRow,currentCol); }
{PointerKey}     	{ recognizeToken("PointerKey",yytext,currentRow,currentCol); }
{MEMAllocKey}    	{ recognizeToken("MEMAllocKey",yytext,currentRow,currentCol); }
{MEMFreeKey}     	{ recognizeToken("MEMFreeKey",yytext,currentRow,currentCol); }
{Identifier}     	{ recognizeToken("Identifier",yytext,currentRow,currentCol); }

%%

int yywrap(void){
 	 return 1;
}

void recognizeToken(char TokenType[], char tokenText[], int row, int col) {
	// 分配空间
	// 还没给存储词法单元的数组分配空间(100)
	if(Token == NULL){
		Token = (tokenRecord **) malloc(sizeof(tokenRecord *) * size);							// 内存分配[指针大小*100(size)]空间,空间指针给Token
		count = 0;
	}	
	
	Token[count] = malloc(sizeof(tokenRecord));										// 同上, 区别就在于不同的(指针 内容) 概念
	
	if(Token[count] == NULL){
		perror("malloc error!\n");
		return;
	}
	
	// 申请空间,记录词法单元信息
	Token[count]->TokenType = (char*)malloc(strlen(TokenType));
	Token[count]->tokenText = (char*)malloc(strlen(tokenText));
	strcpy(Token[count]->TokenType, TokenType);
	strcpy(Token[count]->tokenText, tokenText);
	Token[count]->row = row;
	Token[count]->col = col;
	
	// 更新currentCol值, 加上该词法单元的长度
	currentCol += strlen(tokenText);
	// 数组下标进一
	count++;
	
	// 数组长度动态增长
	if(count > size){
		size *= 2;
		tokenRecord** tempToken;
		tempToken = (tokenRecord **)malloc(sizeof(tokenRecord*)*size);
		int i;
		for(i = 0; i < count; i++)											// 指针元素
			tempToken[i] = Token[i];
		Free(Token);
		Token = tempToken;												// Token  free
	}
}

void outputToken()
{
	int i;
	int j;
	int tokenType_Len;
	int tokenText_Len;
	
	// 循环输出存储的单元
	for(i = 0; i < count; i++)
	{
		tokenType_Len = strlen(Token[i]->TokenType);
		tokenText_Len = strlen(Token[i]->tokenText);
		
		// 输出词法单元信息
		// 类型
		printf("%s",Token[i]->TokenType);
		// 控制输出格式
		for(j = 0; j < 16-tokenType_Len; j++)     
			printf(" ");
		// 内容   
		printf("%s",Token[i]->tokenText);
		for(j = 0; j < 16-tokenText_Len; j++)
			printf(" ");

		// 行、列
		printf("%d\t%d\n",Token[i]->row,Token[i]->col);
	}
}

// 释放Token[]元素指针指向的内存空间及整个数组指向的空间
void Free() {
	int i;
	for(i = 0; i < count; i++)
		free(Token[i]);
	free(Token);
}

int main(int argc,char *argv[]){
	yyin = fopen(argv[1],"r");
	// 词法分析程序, scanner, 调用yywrap();
	yylex();
	printf("\nTYPE\t\tTEXT\t\tROW\tCOLUMN\n");
	outputToken();
	Free();
	fclose(yyin);
	return 0;
}

⌨️ 快捷键说明

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