📄 parser.y
字号:
Ident :T_Identifier {printf("rule:Ident->T_Identifier\n"); $$ = new Identifier(@1,$1);} ;Type :T_Bool {printf("rule:Type->T_Bool\n"); $$ = Type::boolType; } |T_Int {printf("rule:Type->T_Int\n"); $$ = Type::intType; } |T_Double {printf("rule:Type->T_Double\n"); $$ = Type::doubleType; } |T_String {printf("rule:Type->T_String\n"); $$ = Type::stringType; } |Ident { printf("rule:Type->Ident\n");NamedType *nameType = new NamedType($1);$$ = nameType;} |Type T_Dims {printf("rule:Type->Type T_Dims\n");$$ = new ArrayType(@1, $1);} ;ClassDecl :T_Class Ident '{' Fieldlist '}' {printf("rule:ClassDecl->T_Class Ident '{' Fieldlist '}' \n"); $$ = new ClassDecl($2,NULL,$4);} |T_Class Ident T_Extends Ident '{' Fieldlist '}' { printf("rule:ClassDecl->T_Class Ident T_Extends Ident '{' Fieldlist '}'\n"); $$ = new ClassDecl($2,new NamedType($4),$6); } ;Fieldlist :Fieldlist Field {printf("rule:Fieldlist->Fieldlist Field\n"); ($$ = $1)->Append($2);} | { $$ = new List<Decl*>; } ;Field :VariableDecl {printf("rule:Field->VariableDecl\n"); $$ = $1; } |FunctionDecl {printf("rule:Field->FunctionDecl\n"); $$ = $1; } ;FunctionDecl :Type Ident '(' Formals ')' StmtBlock {printf("rule:FunctionDecl->Type Ident '(' Formals ')' StmtBlock\n");$$ = new FnDecl($2,$1,$4);$$->SetFunctionBody($6);} |T_Void Ident '(' Formals ')' StmtBlock {printf("rule:FunctionDecl->T_Void Ident '(' Formals ')' StmtBlock\n"); $$ = new FnDecl($2,Type::voidType,$4);$$->SetFunctionBody($6);} |Type Ident '('')' StmtBlock {printf("rule:FunctionDecl->Type Ident '('')' StmtBlock\n");$$ = new FnDecl($2,$1,new List<VarDecl*>);$$->SetFunctionBody($5);} |T_Void Ident '(' ')' StmtBlock {printf("rule:FunctionDecl->T_Void Ident '(' ')' StmtBlock\n"); $$ = new FnDecl($2,Type::voidType,new List<VarDecl*>);$$->SetFunctionBody($5);} ;Formals :Variable { printf("rule:Formals->Variable\n");($$ = new List<VarDecl*>)->Append($1); } |Formals ',' Variable { printf("rule:Formals->Formals','Variable\n");($$ = $1)->Append($3); } ;StmtBlock :'{' NonEmptyVList NonEmptySList '}' { printf("rule:StmtBlock->'{' NonEmptyVList NonEmptySList '}' \n");$$ = new StmtBlock($2,$3);} |'{' NonEmptyVList '}' { printf("rule:StmtBlock->'{' NonEmptyVList '}' \n"); List<Stmt*> *emptySList = new List<Stmt*>; $$ = new StmtBlock($2,emptySList); } |'{' NonEmptySList '}' {printf("rule:StmtBlock->'{' NonEmptySList '}' \n"); List<VarDecl*> *emptyVList = new List<VarDecl*>; $$ = new StmtBlock(emptyVList,$2); } |'{' '}' {printf("rule:StmtBlock->'{' '}' \n"); List<Stmt*> *emptyStmtList = new List<Stmt*>; List<VarDecl*> *emptyVarList = new List<VarDecl*>; $$ = new StmtBlock(emptyVarList,emptyStmtList);} ;NonEmptyVList :VariableDecl {printf("rule:NonEmptyVList->VariableDecl \n"); ($$ = new List<VarDecl*>)->Append($1); } |NonEmptyVList VariableDecl {printf("rule:NonEmptyVList->NonEmptyVList VariableDecl \n"); ($$ = $1)->Append($2);} ;NonEmptySList :Stmt {printf("rule:NonEmptySList->Stmt \n"); ($$=new List<Stmt*>)->Append($1);} |NonEmptySList Stmt {printf("rule:NonEmptyVList->NonEmptySList Stmt \n"); ($$=$1)->Append($2);} ;Stmt :';' {printf("rule:Stmt->';' \n");$$=new ExprStmt(@1,NULL);} |Expr ';' {printf("rule:Stmt->Expr';' \n");$$=new ExprStmt(@1,$1);} |StmtBlock {printf("rule:Stmt->StmtBlock \n");$$=$1;} |IfStmt {printf("rule:Stmt->IfStmt\n");$$=$1;} |SwitchStmt {printf("rule:Stmt->SwitchStmt \n");$$=$1;} |WhileStmt {printf("rule:Stmt->WhileStmt \n");$$=$1;} |ForStmt {printf("rule:Stmt->ForStmt \n");$$=$1;} |ReturnStmt {printf("rule:Stmt->ReturnStmt \n");$$=$1;} |BreakStmt {printf("rule:Stmt->BreakStmt \n");$$=$1;} |PrintStmt {printf("rule:Stmt->PrintStmt \n");$$=$1;} |TryCatchBlock {printf("rule:Stmt->TryCatchBlockStmt \n");$$=$1;} |ThrowStmt {printf("rule:Stmt->ThrowStmt \n");$$=$1;} ;ThrowStmt :T_Throw Expr ';' {printf("rule:ThrowStmt->T_Throw Expr ';' \n");$$ = new ThrowStmt(@2,$2);} ;TryCatchBlock :T_Try '{' StmtList '}' CatchBlockList {printf("rule:TryCatchBlock->T_Try '{' StmtList '}'CatchBlockList \n");$$ = new TryStmt($3,$5);} ;StmtList :NonEmptySList {printf("rule:StmtList->NonEmptySList\n"); $$=$1;} | {printf("rule:StmtList-> \n");$$=new List<Stmt*>;} ;NonEmptyCBList :CatchBlock {printf("rule:NonEmptyCBList->CatchBlock\n");($$=new List<CatchStmt*>)->Append($1);} |NonEmptyCBList CatchBlock {printf("rule:NonEmptyCBList->NonEmptyCBList CatchBlock\n");($$=$1)->Append($2);} ;CatchBlockList :NonEmptyCBList {printf("rule:CatchBlockList->NonEmptyCBList\n");$$ = $1;} | {$$=new List<CatchStmt*>;} ;CatchBlock :T_Catch '(' Variable ')' '{' StmtList '}' {printf("rule:CatchBlock->T_Catch '(' Variable ')' '{' StmtList '}'\n");$$ = new CatchStmt($3,$6);} ;PrintStmt :T_Print '(' NonEmptyActuals ')' ';' {printf("rule:PrintStmt->T_Print '(' NonEmptyActuals ')' ';' \n");$$ = new PrintStmt($3);} ;BreakStmt :T_Break ';' {printf("rule:BreakStmt->T_Break ';' \n");$$ = new BreakStmt(@1);} ;ReturnStmt :T_Return Expr ';' {printf("rule:ReturnStmt->T_Return Expr ';' \n");$$ = new ReturnStmt(@2,$2);} |T_Return ';' {printf("rule:ReturnStmt->T_Return ';' \n");$$ = new ReturnStmt(@1,new EmptyExpr());} ;IfStmt :T_If '(' Expr ')' Stmt %prec Lower_Else {printf("rule:IfStmt->T_If '(' Expr ')' Stmt \n");$$ = new IfStmt($3,$5,NULL);} |T_If '(' Expr ')' Stmt T_Else Stmt {printf("rule:IfStmt->T_If '(' Expr ')' Stmt T_Else Stmt\n"); $$ = new IfStmt($3,$5,$7);} ;SwitchStmt :T_Switch '(' Expr ')' SwitchBody {printf("rule:SwitchStmt->T_Switch '(' Expr ')' SwitchBody \n");$$ = new SwitchStmt($3,$5);} ;SwitchBody :'{' CaseStmtList DefaultStmt '}' {printf("rule:SwitchBody->'{' CaseStmtList DefaultStmt '}'\n");$$ = new SwitchBody($2,$3);} |'{' CaseStmtList '}' {printf("rule:SwitchBody->'{' CaseStmtList '}'\n");$$ = new SwitchBody($2,NULL);} |'{' DefaultStmt '}' {printf("rule:SwitchBody->'{' DefaultStmt '}'\n"); List<CaseStmt*> *emptyCaseList = new List<CaseStmt*>; $$ = new SwitchBody(emptyCaseList,$2); } |'{' '}' {printf("rule:SwitchBody->'{' '}'\n"); List<CaseStmt*> *emptyCaseList = new List<CaseStmt*>; $$ = new SwitchBody(emptyCaseList,NULL); } ;CaseStmtList :CaseStmt {printf("rule:CaseStmtList->CaseStmt\n");($$=new List<CaseStmt*>)->Append($1);} |CaseStmtList CaseStmt {printf("rule:CaseStmtList->CaseStmtList CaseStmt\n");($$=$1)->Append($2);} ;CaseStmt :T_Case IntConstant ':' Stmt {printf("rule:CaseStmt->T_Case IntConstant ':' Stmt \n");$$ = new CaseStmt($2, $4);} ;DefaultStmt :T_Default ':' Stmt {printf("rule:CaseStmt->T_Default ':' Stmt \n");$$ = new DefaultStmt($3);} ;WhileStmt :T_While '(' Expr ')' Stmt {printf("rule:WhileStmt->T_While '(' Expr ')' Stmt \n");$$ = new WhileStmt($3,$5);} ;ForStmt :T_For '(' Expr_Em ';' Expr ';' Expr_Em ')' Stmt {printf("rule:ForStmt->T_For '(' Expr_Em ';' Expr ';' Expr_Em ')' \n");$$ = new ForStmt($3,$5,$7,$9);} ;Expr_Em :Expr {printf("rule:Expr_Em->Expr \n");$$ = $1;} | {printf("rule:Expr_Em-> \n");$$ = new EmptyExpr();} ;Expr :Constant {printf("rule:Expr->Constant \n");$$ = $1;} |Expr '+' Expr {printf("rule:Expr->Expr '+' Expr \n");$$ = new ArithmeticExpr($1,new Operator(@2,"+"),$3);} |Expr '*' Expr {printf("rule:Expr->Expr '*' Expr \n");$$ = new ArithmeticExpr($1,new Operator(@2,"*"),$3);} |Expr '/' Expr {printf("rule:Expr->Expr '/' Expr \n");$$ = new ArithmeticExpr($1,new Operator(@2,"/"),$3);} |Expr '%' Expr {char cp= '%';printf("rule:Expr->Expr %c Expr \n",cp);$$ = new ArithmeticExpr($1,new Operator(@2,"%"),$3);} |Expr '>' Expr {printf("rule:Expr->Expr '>' Expr \n");$$ = new RelationalExpr($1,new Operator(@2,">"),$3);} |Expr '<' Expr {printf("rule:Expr->Expr '<' Expr \n");$$ = new RelationalExpr($1,new Operator(@2,"<"),$3);} |'-'Expr %prec '!' {printf("rule:Expr->'-'Expr \n");$$ = new ArithmeticExpr(new Operator(@1,"-"),$2);} |Expr '-' Expr {printf("rule:Expr->Expr '-' Expr \n");$$ = new ArithmeticExpr($1,new Operator(@2,"-"),$3);} |Expr T_LessEqual Expr {printf("rule:Expr->Expr T_LessEqual Expr \n");$$ = new RelationalExpr($1,new Operator(@2,"<="),$3);} |Expr T_GreaterEqual Expr {printf("rule:Expr->Expr T_GreaterEqual Expr\n");$$ = new RelationalExpr($1,new Operator(@2,">="),$3);} |Expr T_Equal Expr {printf("rule:Expr->Expr T_Equal Expr\n");$$ = new EqualityExpr($1,new Operator(@2,"=="),$3);} |Expr T_NotEqual Expr {printf("rule:Expr->Expr T_NotEqual Expr\n");$$ = new EqualityExpr($1,new Operator(@2,"!="),$3);} |Expr T_Or Expr {printf("rule:Expr->Expr T_Or Expr\n");$$ = new LogicalExpr($1,new Operator(@2,"||"),$3);} |Expr T_And Expr {printf("rule:Expr->Expr T_And Expr\n");$$ = new LogicalExpr($1,new Operator(@2,"&&"),$3);} |'!' Expr {printf("rule:Expr->'!' Expr\n");$$ = new LogicalExpr(new Operator(@1,"!"),$2);} |'(' Expr ')' {printf("rule:Expr->'(' Expr ')' \n");$$ = $2;} |LValue {printf("rule:Expr->LValue \n");$$ = $1;} |LValue '=' Expr {printf("rule:Expr->LValue = Expr\n");$$ = new AssignExpr($1,new Operator(@2,"="),$3);} |T_New '(' Ident ')' {printf("rule:Expr->T_New '(' Ident ')' \n");$$ = new NewExpr(@1,new NamedType($3));} |T_NewArray '(' Expr ',' Type ')' {printf("rule:Expr->T_NewArray '(' Expr ',' Type ')' \n");$$ = new NewArrayExpr(@1,$3,$5);} |T_ReadLine '(' ')' {printf("rule:Expr->T_ReadLine '(' ')' \n");$$ = new ReadLineExpr(@1);} |T_ReadInteger '(' ')' {printf("rule:Expr->T_ReadInteger '(' ')' \n");$$ = new ReadIntegerExpr(@1);} |T_This {printf("rule:Expr->T_This \n");$$ = new This(@1);} |Call {printf("rule:Expr->Call\n");$$ = $1;} |Expr '?' Expr ':' Expr {printf("rule:Expr->Expr '?' Expr ':' Expr\n"); $$ = new ConditionalExpr($1,new Operator(@2,"?"),$3,new Operator(@4,":"),$5); } ;Call :Ident '(' Actuals ')' {printf("rule:Call->Ident '(' Actuals ')'\n");$$ = new Call(@1,NULL,$1,$3);} |Expr '.' Ident '(' Actuals ')' {printf("rule:Call->Expr '.' Ident '(' Actuals ')'\n");$$ = new Call(@1,$1,$3,$5);} ;NonEmptyActuals :Expr {printf("rule:NonEmptyActuals->Expr\n");($$ = new List<Expr*>)->Append($1);} |NonEmptyActuals ',' Expr {printf("rule:NonEmptyActuals->NonEmptyActuals ',' Expr \n");($$ = $1)->Append($3);} ;Actuals :NonEmptyActuals {printf("rule:Actuals->NonEmptyActuals \n");$$ = $1;} | {printf("rule:Actuals-> \n");$$ = new List<Expr*>;} ;LValue :Ident {printf("rule:LValue->Ident \n");$$ = new FieldAccess(NULL,$1);} |Expr '.' Ident {printf("rule:LValue->Expr '.' Ident \n");$$ = new FieldAccess($1,$3);} |Expr '[' Expr ']' {printf("rule:LValue->Expr '[' Expr ']' \n");$$ = new ArrayAccess(@1,$1,$3);} ;IntConstant :T_IntConstant {printf("rule:IntConstant->T_IntConstant \n");$$ = new IntConstant(@1,$1);} ;Constant :IntConstant {printf("rule:Constant->IntConstant \n");$$ = $1;} |T_DoubleConstant {printf("rule:Constant->T_DoubleConstant\n");$$ = new DoubleConstant(@1,$1);} |T_BoolConstant {printf("rule:Constant->T_BoolConstant\n");$$ = new BoolConstant(@1,$1);} |T_StringConstant {printf("rule:Constant->T_StringConstant\n");$$ = new StringConstant(@1,$1);} |T_Null {printf("rule:Constant->T_Null\n");$$ = new NullConstant(@1);} ;%%/* The closing %% above marks the end of the Rules section and the beginning * of the User Subroutines section. All text from here to the end of the * file is copied verbatim to the end of the generated y.tab.c file. * This section is where you put definitions of helper functions. *//* * Function: InitParser * -------------------- * This function will be called before any calls to yyparse(). It is designed * to give you an opportunity to do anything that must be done to initialize * the parser (set global variables, configure starting state, etc.). One * thing it already does for you is assign the value of the global variable * yydebug that controls whether yacc prints debugging information about * parser actions (shift/reduce) and contents of state stack during parser. * If set to false, no information is printed. Setting it to true will give * you a running trail that might be helpful when debugging your parser. * Please be sure the variable is set to false when submitting your final * version. */void InitParser(){ PrintDebug("parser", "Initializing parser"); yydebug = false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -