?? lexyy.c
字號:
# include "stdio.h"
# define U(x) x
# define NLSTATE yyprevious=YYNEWLINE
# define BEGIN yybgin = yysvec + 1 +
# define INITIAL 0
# define YYLERR yysvec
# define YYSTATE (yyestate-yysvec-1)
# define YYOPTIM 1
# define YYLMAX 200
# define output(c) putc(c,yyout)
# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
# define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;}
# define yymore() (yymorfg=1)
# define ECHO fprintf(yyout, "%s",yytext)
# define REJECT { nstr = yyreject(); goto yyfussy;}
int yyleng; extern char yytext[];
int yymorfg;
extern char *yysptr, yysbuf[];
int yytchar;
FILE *yyin = {stdin}, *yyout = {stdout};
extern int yylineno;
struct yysvf {
struct yywork *yystoff;
struct yysvf *yyother;
int *yystops;};
struct yysvf *yyestate;
extern struct yysvf yysvec[], *yybgin;
// 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);
# define YYNEWLINE 10
yylex(){
int nstr; extern int yyprevious;
while((nstr = yylook()) >= 0)
yyfussy: switch(nstr){
case 0:
if(yywrap()) return(0); break;
case 1:
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 );
}
break;
case 2:
while ( 1 )
{
int currlineno = yylineno;
if ( !input() || currlineno != yylineno )
break;
}
break;
case 3:
{
// keep the "..." used in code gen
// cSTRING db "..."
strncpy(g_String, yytext, MAX_STR_LEN );
return cSTRING;
}
break;
case 4:
{
// keep the <...> used in code gen
strncpy(g_String, yytext, MAX_STR_LEN );
return pINCLDNAME;
}
break;
case 5:
{
yylval.num = stoi(yytext,10);
return cINT;
}
break;
case 6:
{
yylval.num = stoi(yytext,8);
return cINT;
}
break;
case 7:
{
yylval.num = stoi(yytext,16);
return cINT;
}
break;
case 8:
{
strcpy(yylval.p_char,yytext);
return cREAL;
}
break;
case 9:
{
// keep '.'
strcpy(yylval.p_char, yytext);
return cCHAR;
}
break;
case 10:
return oLP;
break;
case 11:
return oRP;
break;
case 12:
{
LEX_INFO("new_compound_symtab()")
new_compound_symtab();
return oLC;
}
break;
case 13:
return oRC;
break;
case 14:
return oLB;
break;
case 15:
return oRB;
break;
case 16:
return oPLUS;
break;
case 17:
return oMINUS;
break;
case 18:
return oMUL;
break;
case 19:
return oDIV;
break;
case 20:
return oMOD;
break;
case 21:
return oASSIGN;
break;
case 22:
return oBITOR;
break;
case 23:
return oBITAND;
break;
case 24:
return oBITNOT;
break;
case 25:
return oBITXOR;
break;
case 26:
return oLFTSHT;
break;
case 27:
return oRITSHT;
break;
case 28:
return oOR;
break;
case 29:
return oAND;
break;
case 30:
return oNOT;
break;
case 31:
return oEQUAL;
break;
case 32:
return oUNEQU;
break;
case 33:
return oLT;
break;
case 34:
return oGT;
break;
case 35:
return oLE;
break;
case 36:
return oGE;
break;
case 37:
return oPLUSASSIGN;
break;
case 38:
return oMINUSASSIGN;
break;
case 39:
return oMULASSIGN;
break;
case 40:
return oDIVASSIGN;
break;
case 41:
return oMODASSIGN;
break;
case 42:
return oBITORASSIGN;
break;
case 43:
return oBITANDASSIGN;
break;
case 44:
return oBITXORASSIGN;
break;
case 45:
return oLFTSHTASSIGN;
break;
case 46:
return oRITSHTASSIGN;
break;
case 47:
return oCOMMA;
break;
case 48:
return oSEMI;
break;
case 49:
return oCOLON;
break;
case 50:
return oQUOTE;
break;
case 51:
return oDOTDOTDOT;
break;
case 52:
return oQUESTION;
break;
case 53:
return oADDADD;
break;
case 54:
return oSUBSUB;
break;
case 55:
return pINCLUDE;
break;
case 56:
{
strcpy(yylval.p_char,yytext);
return id_or_keyword(yytext);
}
break;
case -1:
break;
default:
fprintf(yyout,"bad switch yylook %d",nstr);
} return(0); }
/* end of yylex */
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();
}
int yyvstop[] = {
0,
30,
0,
20,
0,
23,
0,
50,
0,
10,
0,
11,
0,
18,
0,
16,
0,
47,
0,
17,
0,
19,
0,
6,
0,
5,
0,
49,
0,
48,
0,
33,
0,
21,
0,
34,
0,
52,
0,
56,
0,
14,
0,
15,
0,
25,
0,
12,
0,
22,
0,
13,
0,
24,
0,
32,
0,
3,
0,
41,
0,
29,
0,
43,
0,
39,
0,
53,
0,
37,
0,
54,
0,
38,
0,
8,
0,
1,
0,
2,
0,
40,
0,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -