Posts Tagged ‘YACC’

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

7
十二月

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

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

%{
#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: ,

7
十二月

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

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

%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

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

Tags: ,

1
十一月

編譯器課程10/23上課

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

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,