?? lexer.l.bak
字號:
%{
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.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)
{
int h = *name++;
while (*name) h = h << 4 + *name++;
return h;
}
char * insert_id(char *name)
{ int h = hash(name) & 1024;
struct id_entry *p=id_hash_table[h];
while (p && (strcmp(p->id, name) != 0)) p = p->next;
if (p) return p->id;
p = (struct id_entry *)malloc(sizeof *p);
p->id = (char *)malloc(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}+
leter [A-Za-z]
digit [0-9]
id {leter}({leter}|{digit})*
number {digit}+(\.{digit}+)?(E[+\-]?{digit}+)?
comment (\#[^\n]*)|(\/\*([^*]|\n|(\*+([^*/]|\n)))*\*+\/)
sentence \"(.)*\"
chartype (\'([^\n]|\n)\')|\'\'
special [\@\~\\\$\`]
pun \(|\)|\{|\}|<=|>=|=|\+|\-|\;|\*|\,
%%
"\n" { nline++;}
{comment} {/*對預處理和注釋部分沒有動作和返回值*/}
{ws} {/*對空白串沒有動作和返回值*/}
{number} { /*yylval._ident = insert_id(yytext);*/fprintf(yyout,"NUM %s\n",yytext);}
{sentence} {fprintf(yyout,"STRING %s\n",yytext);}
{chartype} {fprintf(yyout,"CONST_CHAR %s\n",yytext);}
"__stdcall" {fprintf(yyout,"STDCALL %s\n",yytext);}
"__cdecl" {fprintf(yyout,"CDECL %s\n",yytext);}
"if" {fprintf(yyout,"KEYWORD %s\n",yytext);}
"int" {fprintf(yyout,"KEYWORD %s\n",yytext);}
"void" {fprintf(yyout,"KEYWORD %s\n",yytext);}
"char" {fprintf(yyout,"KEYWORD %s\n",yytext);}
"else" {fprintf(yyout,"KEYWORD %s\n",yytext);}
"while" {fprintf(yyout,"KEYWORD %s\n",yytext);}
"return" {fprintf(yyout,"KEYWORD %s\n",yytext);}
{pun} {fprintf(yyout,"PUN %s\n",yytext);}
{id} { /*yylval._ident = insert_id(yytext); */fprintf(yyout,"ID %s\n",yytext);}
. { /*return yytext[0];*/ }
%%
error(char *m)
{
fprintf(stderr,"%s\n",m);
exit(1); /*非正常終止*/
}
int main(int argc,char *argv[]){
if(argc==1){
information(); //當命令行不帶參數時,輸出提示信息
}
else{
if(argc==3){
if((yyin=fopen(argv[1],"r"))==NULL){
printf("can't open %s\n",argv[1]);
exit(0);
}
if((yyout=fopen(argv[2],"w"))==NULL){
printf("can't open %s\n",argv[2]);
exit(0);
}
yylex();
return 0;
}
else
printf("Common Error!\n");
}
}
int yywrap(){return 1;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -