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

📄 c.l

📁 C编译器,在VC6.0环境下开发
💻 L
字号:
/*
* C.lex
* Lexical Analyzer for C Compiler
*
* Copyright (c)  2001 by  Lin Ying and Ren Qi
* All Rights Reserved.
*
*/

%{

// include file
#include	"Header.h"
#include	"Debug.h"
#include	"Error.h"
#include    "convert.h"
#include	"Common.h"
#include	"SymTab.h"
#include	"stack.h"
#include	"Lex.h"

// for strcpy etc. string operation to string
#include	<string.h>

// used to contain cSTRING
extern	char	g_String[MAX_STR_LEN];

#define	LEX_INFO(x)		StrToDebug(x, "\nIn yylex() : ", "  --- LEX");

int	yylook();
int	yywrap();
int	yyback(int *p,int m);

%}

letter      [_a-zA-Z]
alnum       [_a-zA-Z0-9]
dec         [0-9]
hex         [0-9a-fA-F]
oct         [0-7]

%%	

"/*"	while ( 1 )
		{
			int i;

			i = input();

			// or i == EOF, see input() source code for when file
			// meet EOF return what?
			
			if ( !i )
			{
				yyerror( "End of file in comment\n" );
				user_exit(1);
			}

			if ( i == '*' )
				if ( ( i = input() )== '/' )
					break;
				else
					unput( i );
		}

"//"	while ( 1 )
		{
			int	currlineno = yylineno;

			if  ( !input() || currlineno != yylineno )
				break;
		}

\"([^\"])*\"	{
				// keep the "..." used in code gen
				// cSTRING db "..."
				strncpy(g_String, yytext, MAX_STR_LEN );
				return	cSTRING;
				}

"<"([^>])*">"	{
				// keep the <...> used in code gen
				strncpy(g_String, yytext, MAX_STR_LEN );
				return	pINCLDNAME;
				}

[1-9]+{dec}*	{
				yylval.num = stoi(yytext,10);
				return cINT;
				}

0{oct}*         {
				yylval.num = stoi(yytext,8);
				return cINT;
				}

0(x|X){hex}+    {
				yylval.num = stoi(yytext,16);
				return cINT;
				}

([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?	{
													strcpy(yylval.p_char,yytext);
													return cREAL;
													}

'([^']|'')'     {
				// keep '.'
				strcpy(yylval.p_char, yytext);
				return cCHAR;
				}

"("				return oLP;
")"				return oRP;
"{"				{
					LEX_INFO("new_compound_symtab()")
					new_compound_symtab();
					return oLC;
				}
"}"				return oRC;
"["				return oLB;
"]"				return oRB;

"+"				return oPLUS;
"-"				return oMINUS;
"*"				return oMUL;
"/"				return oDIV;
"%"				return oMOD;
"="				return oASSIGN;

"|"				return oBITOR;
"&"				return oBITAND;
"~"				return oBITNOT;
"^"				return oBITXOR;
"<<"			return oLFTSHT;
">>"			return oRITSHT;

"||"			return oOR;
"&&"			return oAND;
"!"				return oNOT;


"=="            return oEQUAL;
"!="			return oUNEQU;
"<"             return oLT;
">"             return oGT;
"<="            return oLE;
">="            return oGE;

"+="			return oPLUSASSIGN;
"-="			return oMINUSASSIGN;
"*="			return oMULASSIGN;
"/="			return oDIVASSIGN;
"%="			return oMODASSIGN;
"|="			return oBITORASSIGN;
"&="			return oBITANDASSIGN;
"^="			return oBITXORASSIGN;
"<<="			return oLFTSHTASSIGN;
">>="			return oRITSHTASSIGN;

","				return oCOMMA;
";"             return oSEMI;
":"				return oCOLON;
"'"				return oQUOTE;
"..."			return oDOTDOTDOT;
"?"				return oQUESTION;

"++"			return	oADDADD;
"--"			return	oSUBSUB;

"#include"		return	pINCLUDE;

{letter}{alnum}*    {
					strcpy(yylval.p_char,yytext);
					return id_or_keyword(yytext);
					}

%%

static	int		sg_Keytable_size = 0;

KEYENTRY Keytable[] =
{
	{"00000000",0 },
	{"auto",	kAUTO},
	{"break",	kBREAK},
	{"case",	kCASE},
	{"char",	kCHAR},
	{"const",	kCONST},
	{"continue",kCONTINUE},
	{"default",	kDEFAULT},
	{"do",		kDO},
	{"double",	kDOUBLE},
	{"else",	kELSE},
	{"extern",	kEXTERN},
	{"float",	kFLOAT},
	{"for",		kFOR},
	{"goto",	kGOTO},
	{"if",		kIF},
	{"int",		kINT},
	{"long",	kLONG},
	{"register",kREGISTER},
	{"return",	kRETURN},
	{"short",	kSHORT},
	{"signed",	kSIGNED},
	{"sizeof",	kSIZEOF},
	{"static",	kSTATIC},
	{"switch",	kSWITCH},
	{"typedef",	kTYPEDEF},
	{"unsigned",kUNSIGNED},
	{"void",	kVOID},
	{"volatile",kVOLATILE},
	{"while",	kWHILE},
	{"zzzzzzzz",LAST_ENTRY}
};

void	set_Keytablesize()
{
	// set  Keytable_size, call in Init_yacc()
	sg_Keytable_size = 0;

	while ( LAST_ENTRY != Keytable[sg_Keytable_size].key )
		sg_Keytable_size++;
}

int		id_or_keyword(char *lex)
{
	// alose return  typedef name, so find in current Symbol and father Symbol for typedef name
	int left = 0, right = sg_Keytable_size;
	int mid = (left+right)/2;
	
	if ( !sg_Keytable_size )
	{
		yyerror( "lex error : Keytable not init\n " );
		user_exit(1);
	}

    while (mid!=left && mid!=right)
	{
		int i;

        if ( ! (i = strcmp(Keytable[mid].name, lex) ) )
		// found
            return Keytable[mid].key;
		else if ( i < 0 )
			left = mid;
		else if ( i > 0 )
			right = mid;
		mid = (left+right)/2;
	}

	// search for typedef name
	// return not NULL means found
	if ( find_symtab_typedef( lex ) )
		return	idTYPEDEF;

	return yNAME;
}

yywrap()
{
	return	pop_include();
}


⌨️ 快捷键说明

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