⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.y

📁 一个C语言的编译器
💻 Y
📖 第 1 页 / 共 2 页
字号:
                {
                  $$ = newStmtNode(InputK);
                  $$ -> attr.name = savedIdName;
                }
            ;
output_stmt : OUTPUT LP id RP SEMI                    //输出语句
                {
                  $$ = newStmtNode(OutputK);
                  $$ -> attr.name = savedIdName;
                }
            ; 
return_stmt : RETURN SEMI                             //函数返回语句
                { $$ = newStmtNode(ReturnK);
                  $$ -> child[0] = NULL;
                }
            | RETURN exp SEMI
                { $$ = newStmtNode(ReturnK);
                  $$ -> child[0] = $2;
                }
            ;                   
exp         : var ASSIGN exp
                { $$ = newStmtNode(AssignK);
                  $$ -> child[0] = $1;
                  $$ -> child[1] = $3;
                }
            | simple_exp
                { $$ = $1; }
            ;
var         : id
                { $$ = newExpNode(IdK);
                  $$ -> attr.name = savedIdName;
                }            
            | id LSP factor RSP
				{ $$ = newExpNode(IdK);
				  $$ -> attr.name = savedIdName;
                  $$ -> child[0] = $3;
                }           
            ;
simple_exp : simple_exp OR simple_exp             //算术运算(+,-,*,/,%,++,--,&,|,^,~,<<,>>)关系运算(==,>,<,>=,<=,!=)逻辑运算(&&,||,!)  
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = OR;
				}
			|simple_exp AND simple_exp                         
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = AND;
				}
			|simple_exp LT simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = LT;
				  $$ -> type = Boolean;
				}
			|simple_exp LE simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = LE;
				  $$ -> type = Boolean;
				}
			|simple_exp GT simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = GT;
				  $$ -> type = Boolean;
				}
			|simple_exp GE simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = GE;
				  $$ -> type = Boolean;
				}
			|simple_exp EQ simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = EQ;
				  $$ -> type = Boolean;
				}
			|simple_exp NEQ simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = NEQ;
				  $$ -> type = Boolean;
				}
			|simple_exp PLUS simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = PLUS;
				}
			|simple_exp SUB simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = SUB;
				}
			|simple_exp MUT simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = MUT;
				}
			|simple_exp DIV simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = DIV;
				}
			|simple_exp MOD simple_exp 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = MOD;
				}
			|simple_exp INC 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> attr.op = INC;
				}
			|LP INC simple_exp RP 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $3;
				  $$ -> attr.op = INC;
				}
			|simple_exp DEC 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> attr.op = DEC;
				}
			|LP DEC simple_exp RP 
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $3;
				  $$ -> attr.op = DEC;
				}
			|simple_exp B_AND simple_exp                         
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = B_AND;
				}
			|simple_exp B_XOR simple_exp                         
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = B_XOR;
				}
			|simple_exp B_OR simple_exp                         
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = B_OR;
				}
			|simple_exp B_LEFT factor                         
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = B_LEFT;
				}
			|simple_exp B_RIGHT factor                         
				{ $$ = newExpNode(OpK);
				  $$ -> child[0] = $1;
				  $$ -> child[1] = $3;
				  $$ -> attr.op = B_RIGHT;
				}
			| NOT factor
			    { $$ = newExpNode(OpK);
				  $$ -> child[0] = $2;
			      $$ -> attr.op = NOT;
			    }
			| B_NOT factor
			    { $$ = newExpNode(OpK);
				  $$ -> child[0] = $2;
			      $$ -> attr.op = B_NOT;
			    }
			| factor
			    { $$ = $1; }
			;
factor      : LP exp RP
                { $$ = $2; }			
            | var
                { $$ = $1; }			
			| call
			    { $$ = $1; }			
			| NUM
			    { $$ = newExpNode(NumK);
			      $$ -> type = Integer;
			      $$ -> attr.val.i = atoi(tokenString);
			    }
			| FNUM
			    { $$ = newExpNode(FnumK);
			      $$ -> type = Double;
			      $$ -> attr.val.f = atof(tokenString);
			    }
			| SCHAR
			    { $$ = newExpNode(CharK);
			      $$ -> type = Char;
			      $$ -> attr.val.i = *(tokenString + 1);
			    }
			;
call        : var LP args RP
                { $$ = newStmtNode(CallK);
				  $$ -> attr.name = $1 -> attr.name;
                  $$ -> child[0] = $3;
                }
            ;
arg_list    : arg_list COMMA exp            
				{ TreeNode * t = $1;
				  if (t != NULL)
				  { while (t->sibling != NULL)
					t = t->sibling;
					t->sibling = $3;
					$$ = $1; }
				  else $$ = $3;
				}	                 
            | exp
                { $$ = $1; }
			;
args        : arg_list
                { $$ = $1; }           
            | 
                { $$ = NULL; }
            ;

%%

/////////////////////////////////////////////////////////////////////////////
// programs section

void yyerror(const char * message)
{ fprintf(listing,"Syntax error at line %d: %s\n",lineno,message);
  fprintf(listing,"Current token: ");
  printToken(yychar,tokenString);
  Error = TRUE;
}

#ifdef YYPROTOTYPE
int YYCDECL yygettoken(void)
#else
int YYCDECL yygettoken()
#endif
{
	return yylex();
}

static TokenType yylex(void)
{ return getToken(); }

TreeNode * parse(void)
{ yyparse();
  return savedTree;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -