📄 c--.y
字号:
if($2->funargc!=$5) printf(" Type mismatch(defined more than declartion) in redeclaration of %s\n",$2->name);
for(i=1;i<=$5;i++)
{
if(p1==0||p2==0) break;
if(p1->type!=p2->type)
printf(" Parater %d Type mismatch in redeclaration of %s\n",i,$2->name);
p1=p1->next; p2=p2->next;
}
}else {
$2->funargc=$5;
$2->functype=funtype;
// printf("%d = \n",$2->funargc);
}
offset_len--;
table_len--;
// printf(" argc %d\n", $5);
level--;
funtype=0;
// printf("Level -------- %d %d\n",level, table_len-1);
trace("fun_declar => type_spec fun_tag '(' LL params ')' comp_stmt \n");
}
| type_spec fun_tag '(' LL params ')' ';'
{
// fprintf(yyout, "extrn _%s:near\n", $2->name);
//55
trace("fun_declar => type_spec fun_tag '(' LL params ')' ';' \n");
// printf("ok here\n");
//printf("$5========= %d\n",$5);
$2->isdeclar=1;
$2->funargc=$5;
$2->functype=funtype;
funtype=0;
table_len--;
// printf("aideclar %d far %d\n", $2->isdeclar,$5);
level--;
// printf("end of function declar _%s \n\n", $2->name);
}
| type_spec fun_tag '(' LL params ')'
{
printf("line %d:function %s declaration missing ;\n",nline-1,$2->name);
}
;
//2
LL: ;{level++;trace("LL => ;\n"); insert(NULL, table_stack[table_len-1]);
// printf("Level -------- %d %d\n",level, table_len-1);
}
fun_tag : callconvent ID
{
struct table * tp = table_stack[table_len-1];
symbol *tmp;
isfunction=1;
trace("fun_tag => callconvent ID\n");
// printf("Before insert %s inserat level %d tp.level %d table_len-1 %d\n",$2,level,tp->level,table_len-1);
if((tmp=lookupred($2,tp))==0){
symbol * p;
$$ = insert($2, table_stack[table_len-1]);
}else {
$$=tmp;
if(tmp->type==1){
if(tmp->isdeclar==0) printf("line %d :error: %s :fuction redefinition\n", nline, $2);
else { $$=tmp ;}
}else
printf("line %d :error: %s :redefinition\n", nline, $2);
}
isfunction=0;
}
| ID
{
struct table * tp = table_stack[table_len-1];
symbol *tmp;
isfunction=1;
trace("fun_tag =>ID\n");
// printf("Before insert %s inserat level %d tp.level %d table_len-1 %d\n",$1,level,tp->level,table_len-1);
if((tmp=lookupred($1,tp))==0){
symbol * p;
$$ = insert($1, table_stack[table_len-1]);
}else {
$$=tmp;
if(tmp->type==1){
if(tmp->isdeclar==0) {printf("line %d :error: %s :fuction redefinition\n", nline, $1);}
else {$$=tmp ;}
}else
{printf("line %d :error: %s :redefinition\n", nline, $1);}
}
isfunction=0;
}
;
params : param_list
{ trace("params => param_list \n");
$$ = $1;
}
|VOID
{ trace("params => VOID \n"); argccount=0;
$$ = 0;
}
;
param_list : param_list ',' param
{ trace("param_list => param_list , param \n"); $$ = $1+$3; }
| param
{ trace("param_list => param\n"); $$ = $1; }
;
//22
param : type_spec ID
{ trace("param => type_spec ID\n");
$$ = 1;
{
struct table * tp = table_stack[table_len-1];
//33
if(lookupred($2,tp)==0){
symbol * p = insert($2, tp);
int width=0;
fun_type *tmp; fun_type *pp;
tmp=(fun_type *)malloc(sizeof(fun_type));
tmp->type=$1;
tmp->next=0;
if(funtype==0) {
funtype=tmp;
}
else {
pp=funtype;
while(pp->next){
pp=pp->next;
}
pp->next=tmp;
}
p->type = $1;
p->offset = offset_stack[offset_len-1];
// if ($1 == INT) width = 4; else if ($1 == CHAR) width = 1;
//offset_stack[offset_len-1] -= width;
}else printf("line %d :error %s :redefinition\n", nline, $2);
}
$$ = 1; // printf("ok here\n");
}
| type_spec '*' ID
{ trace("param => type_spec * ID \n");
{
struct table * tp = table_stack[table_len-1];
if(lookupred($3,tp)==0){
symbol * p = insert($3, tp);
int width = 0;
fun_type *tmp; fun_type *pp;
tmp=(fun_type *)malloc(sizeof(fun_type));
tmp->type=$1;
tmp->next=0;
if(funtype==0) {
funtype=tmp;
}
else {
pp=funtype;
while(pp->next){
pp=pp->next;
}
pp->next=tmp;
}
p->type = $1;
p->offset = offset_stack[offset_len-1];
if ($1 == INT) width = 4; else if ($1 == CHAR) width = 1;
offset_stack[offset_len-1] -= width;
}else printf("line %d :error: %s :redefinition\n", nline, $3);
} $$ = 1;
}
| type_spec ID '[' ']'
{ trace("param => type_spec ID [ ]\n");
{
struct table * tp = table_stack[table_len-1];
if(lookupred($2,tp)==0){
symbol * p = insert($2, tp);
int width = 0;
fun_type *tmp; fun_type *pp;
tmp=(fun_type *)malloc(sizeof(fun_type));
tmp->type=$1;
tmp->next=0;
if(funtype==0) {
funtype=tmp;
}
else {
pp=funtype;
while(pp->next){
pp=pp->next;
}
pp->next=tmp;
}
p->type = $1;
p->offset = offset_stack[offset_len-1];
if ($1 == INT) width = 4; else if ($1 == CHAR) width = 1;
offset_stack[offset_len-1] -= width;
}else printf("line %d :error: %s :redefinition\n", nline, $2);
} $$ = 1;
}
;
//66
comp_stmt : '{' local_declars stmt_list '}'
{ $$ = $3; trace(" comp_stmt => local_declars stmt_list\n"); }
;
local_declars : local_declars var_declar
|
{ trace(" local_declars => ε\n"); }
;
stmt_list : stmt_list stmt
{ char * s;
trace(" stmt_list => stmt_list stmt \n");
$$=$2;
// assert($1.code != NULL);
// assert($2.code != NULL);
// s = (char *)malloc(strlen($1.code) + strlen($2.code)+1);
// strcpy(s, $1.code);
// strcat(s, $2.code);
// $$.code = s;
/*释放内存!*/
/*free($1.code); free($2.code); */
}
|
{ $$.code = ""; trace("empty stmt_list\n");}
;
stmt : expr_stmt
{ $$=$1; trace("stmt => expr_stmt\n"); }
| comp_stmt
{ $$=$1; trace("stmt => comp_stmt\n"); }
| if_stmt
{ $$=$1; trace("stmt => if_stmt\n"); }
| while_stmt
{ trace("stmt => while_stmt\n"); }
| return_stmt
{ $$=$1; trace("stmt => return_stmt\n"); }
;
expr_stmt : expr ';'
{
trace("expr_stmt => expr ;\n");
//77
/* if (verbose){
if($1.type==INT)
fprintf(stderr, "expr_stmt => expr ; type is int code=%d\n", $1.value);
else if($1.type==CHAR)
fprintf(stderr, "expr_stmt => expr ; type is char code=%s\n", $1);
}*/
$$=$1;
}
| ';'
{ trace("expr_stmt => ;\n"); }
;
if_stmt : IF '(' expr ')' stmt
{ trace("if_stmt => if ( expr ) stmt\n"); $$=$5; }
| IF '(' expr ')' stmt ELSE stmt
{ trace("if_stmt => if ( expr ) stmt else stmt\n"); $$.code="if------"; }
;
while_stmt : WHILE '(' expr ')' stmt
{ trace("while_stmt => while ( expr ) stmt\n"); }
;
return_stmt : RETURN ';'
{ trace("return_stmt => return ; \n"); }
| RETURN expr ';'
{ trace("return_stmt => return expr ;\n");
$$ = $2; }
;
expr : var '=' expr
{ char code [1000];
trace("expr => var '=' expr \n");
if ($1 != 0) {
if ($1->type != $3.type) {
// printf("var type is %d expr type is %d \n",$1->type,$3.type);
printf("line %d :error : = :the left and the right type does not match\n", nline, $1);
}
$$.type = $1->type;
}
/* if ($3.is_const) sprintf(code, " mov [ebp-%d], %d ;%s\n", $1->offset+4, $3.value, $1->name);
else { int len;
strcpy(code, $3.code);
len = strlen(code);
sprintf(&code[len], " mov %s, %s\n", $1->name, $3.place->name);
}
$$.code = (char *)malloc(strlen(code)+1);
strcpy($$.code, code);
fprintf(yyout, code);
*/
}
| logic_expr{
trace("expr => logic_expr ; \n");
$$.type=$1.type;
// printf("expr => logic_expr type is %d \n",$$.type);
}
;
//4
var : ID
{ if (verbose) fprintf(stderr, "look for %s\n", $1);
$$ = lookup($1, table_stack[table_len-1]);
if ($$ == NULL) { fprintf(stderr, "line %d :undeclared identifier: %s \n", nline, $1);
}// else printf("var id type is %d \n",$$->type);
}
| ID '[' expr ']'
{ trace("var => ID [ expr ]\n");
$$ = lookup($1, table_stack[table_len-1]);
if ($$ == NULL) { fprintf(stderr, "line %d :undeclared identifier: %s \n", nline, $1);
//exit(0);
}
}
;
logic_expr : logic_expr OR logicand_expr ///new added productions!!
{ trace("logic_expr => logic_expr OR logicand_expr\n");
if($1.value||$3.value) $$.value=1;
else $$.value=0;
$$=$3;
}
| logicand_expr
{ trace("logic_expr => logicand_expr\n"); $$=$1;
//printf("expr => logic_expr type is %d \n",$$.type);
}
;
logicand_expr : logicand_expr AND simple_expr
{ trace("logicand_expr => logicand_expr AND simple_expr\n");
if($1.value&&$3.value) $$.value=1;
else $$.value=0;
$$.type=$3.type;
}
| simple_expr
{ trace("logicand_expr => simple_expr\n");
$$=$1;
//printf("ogicand_expr => simple_expr type is %d \n",$$.type);
}
;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -