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

📄 c--.l

📁 简单实现C--语言的编译器
💻 L
字号:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int nline =1;

struct id_entry{
	char * id;
	struct id_entry *next; /* next entry on hash chain */
};

struct id_entry *id_hash_table[1024];

int hash(char * name){  /* char name->int */
	int h = *name++;  /* *name++ ?? */
	while(*name)  h = (h << 4) + *name++;
	return h;
}

char *insert_id(char *name){   /* 返回 name */
	int h = hash(name) & 1023; /*  表头 */
	struct id_entry *p=id_hash_table[h];
	while(p && (strcmp(p->id,name) != 0))
		p=p->next;
	if(p)  return p->id;     //had exist name
	p=(struct id_entry *)malloc(sizeof *p);
	p->id = (char *)malloc(sizeof(strlen(name)+1));
	strcpy(p->id,name);
	p->next= id_hash_table[h];
	id_hash_table[h] = p ;  /* 插入在表头 */
	return p->id;
}
	
%}

/* 正规定义 */
delim		[ \t]
ws		{delim}+
letter		[A-Za-z]
digit		[0-9]
id		{letter}({letter}|{digit})*
number		{digit}+(\.(digit)+)?(E[+\-]?{digit}+)?
comment	(\#[^\n]*)|(\/\*(((\*)*[^\*\/]+(\/)*)*|(\*)*|(\/)*)\*\/)|(\/\/[^\n]*)
sentence	\"([^\"])*\"
chartype	(\'([^\n]|\n)\')|\'\'
special         [\@\~\\\$\`] .


%%
"\n"        { nline++;}
{comment}   {  ; }
{ws}        {  ; }
{number}    {yylval._ident = insert_id(yytext); return NUM;}
{sentence}  {yylval._ident = yytext; return STRING_LITERAL;}
{chartype}  {yylval._op = yytext[0]; return  CHAR_LITERAL;}
"__stdcall" {return STDCALL;}
"__cdecl"   {return CDECL;}
"if"        {return IF;}
"int"       {return INT;}
"void"      {return VOID;}
"char"      {return CHAR;}
"else"      {return ELSE;}
"while"     {return WHILE;}
"return"    {return RETURN;}
{id}        {yylval._ident = insert_id(yytext); return ID;}
"<="        {return LE;}
">="        {return GE;}
"<"         {return LT;}
">"         {return GT;}
"!="        {return NE;}
"=="        {return EQ;}
.           {return yytext[0];}


%% 



void error(char *m)
{
     fprintf(stderr,"%s\n",m);  
     exit(1);         /*非正常终止*/
}



int yywrap(){
	return 1;
}

	

⌨️ 快捷键说明

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