Archive for the ‘98(上)編譯器’ Category

編譯器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

7
十二月

Lex/YACC作業二的Lex(hw2.l)

   Posted by: admin 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

7
十二月

Lex/YACC作業二的YACC (hw2.y)

   Posted by: admin Tags: ,

%union{
    int ival;
    char* sval;
}
 
%token <sval> NAME
%token <ival> NUMBER
%type <ival> expression
%start statements
 
%%
statements: statements ',' statement
          | statement
          ;
 
statement: NAME '(' expression ')' { printf("%s=%d\n", $1, $3); free($1);}
	;
 
expression:	expression '+' NUMBER	{ $$ = $1 + $3; }
	|	expression '-' NUMBER	{ $$ = $1 - $3; }
	|	NUMBER			{ $$ = $1; }
	;
%%
main()
{
	yyparse();
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

30
十一月

編譯器課程11/30上課:LEX和YACC(三)

   Posted by: admin Tags: , ,

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

30
十一月

使用Lex/YACC製作簡單計算機範例的YACC (ex2.y)

   Posted by: admin Tags: ,

%union{
    int ival;
    char* sval;
}
 
%token <sval> NAME
%token <ival> NUMBER
%type <ival> expression
%start statement
 
%%
statement: NAME '=' expression { printf("%s=%d\n", $1, $3); free($1);}
	;
 
expression:	expression '+' NUMBER	{ $$ = $1 + $3; }
	|	expression '-' NUMBER	{ $$ = $1 - $3; }
	|	NUMBER			{ $$ = $1; }
	;
%%
main()
{
	yyparse();
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark