Posts Tagged ‘Lex’

編譯器yacc作業
寫一個lex程式和一個yacc程式,
計算一些簡單的12個月收入計算:每個月薪水是底薪加獎金減預扣費用
如 jan(20000+5000-2000), feb(20000+5000-2300)

輸出
jan=23000, feb=22700
=============================================================
如 jan(20000+5000-2000), feb(20000+5000-2300),  mar(30000+6000-4000)

輸出
jan=23000, feb=22700, mar=32000

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , , ,

%{
#include "y.tab.h"
%}
 
%%
[0-9]+ { yylval.ival = atoi(yytext); return NUMBER; }
[ \t]	;		/* ignore white space */
\n	return 0;	/* logical EOF */
[\+\-=] {return yytext[0];}
[a-zA-Z]+	{ yylval.sval = (char *)calloc(strlen(yytext)+1,sizeof(char));
            strcpy(yylval.sval,yytext); return NAME;}
. ;
%%

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,

30
十一月

Lex習題的範例 (ex1.l)

   Posted by: admin    in 98(上)編譯器

%%
[\n\t ]	;
 
^[a-zA-Z0-9]+@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4}) { printf("%s is a email\n", yytext); }
^("http://")?([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4}) { printf("%s is an url\n", yytext); }
.+	{ printf("%s is not a legal pattern\n", yytext); }
%%
main()
{
	yylex();
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,

1
十一月

編譯器課程10/09上課

   Posted by: admin    in 97(上)編譯器

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,