12.15
unknown
plain_text
a year ago
7.0 kB
7
Indexable
/* Definition section */ %{ #include "compiler_common.h" #include "compiler_util.h" #include "main.h" int yydebug = 1; %} /* Variable or self-defined structure */ %union { ObjectType var_type; bool b_var; int i_var; float f_var; char *s_var; Object object_val; } /* Token without return */ %token COUT %token SHR SHL BAN BOR BNT BXO ADD SUB MUL DIV REM NOT GTR LES GEQ LEQ EQL NEQ LAN LOR %token VAL_ASSIGN ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN REM_ASSIGN BAN_ASSIGN BOR_ASSIGN BXO_ASSIGN SHR_ASSIGN SHL_ASSIGN INC_ASSIGN DEC_ASSIGN %token IF ELSE FOR WHILE RETURN BREAK CONTINUE %token '{' '}' '(' ')' '[' ']' %token ',' ':' ';' /* Token with return, which need to sepcify type */ %token <var_type> VARIABLE_T %token <b_var> BOOL_LIT %token <i_var> INT_LIT %token <f_var> FLOAT_LIT %token <s_var> STR_LIT %token <s_var> IDENT /* Nonterminal with return, which need to sepcify type */ %type <object_val> Expression %left ADD SUB %left MUL DIV REM %nonassoc UMINUS /* Yacc will start at this nonterminal */ %start Program %% /* Grammar section */ Program : { pushScope(); } GlobalStmtList { dumpScope(); } | /* Empty file */ ; GlobalStmtList : GlobalStmtList GlobalStmt | GlobalStmt ; GlobalStmt : DefineVariableStmt | FunctionDefStmt ; DefineVariableStmt : VARIABLE_T IDENT VAL_ASSIGN Expression ';' ; /* Function */ FunctionDefStmt : VARIABLE_T IDENT { createFunction($<var_type>1, $<s_var>2); } '(' FunctionParameterStmtList ')' '{' StmtList '}' { dumpScope(); } | VARIABLE_T IDENT '[' ']' { createFunction($<var_type>1, $<s_var>2); } '(' FunctionParameterStmtList ')' '{' StmtList '}' { dumpScope(); } ; FunctionParameterStmtList : FunctionParameterStmtList ',' FunctionParameterStmt | FunctionParameterStmt | /* Empty function parameter */ ; FunctionParameterStmt : VARIABLE_T IDENT { pushFunParm($<var_type>1, $<s_var>2, VAR_FLAG_DEFAULT); } | VARIABLE_T IDENT '[' ']' { pushFunParm($<var_type>1, $<s_var>2, VAR_FLAG_DEFAULT); } ; /* Scope */ StmtList : StmtList Stmt | Stmt ; Stmt : ';' | COUT CoutParmListStmt ';' { stdoutPrint(); } | RETURN Expression ';' { printf("RETURN\n"); } ; CoutParmListStmt : CoutParmListStmt SHL Expression { pushFunInParm(&$<object_val>3); } | SHL Expression { pushFunInParm(&$<object_val>2); } ; Expression : Assign | ARG {Object obj; obj.type = $1.type; $$ = obj;} | IDENT { printf("IDENT (name=%s, address=-1)\n", $<s_var>1); Object obj; obj.type = OBJECT_TYPE_STR; $$ = obj; } | num {Object obj; obj.type = $1.type; $$ = obj; } | Expression LOR Expression {printf("LOR\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression LAN Expression {printf("LAN\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression BOR Expression {printf("BOR\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression BXO Expression {printf("BXO\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression BAN Expression {printf("BAN\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression NEQ Expression {printf("NEQ\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression EQL Expression {printf("EQL\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression GTR Expression {printf("GTR\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression LES Expression {printf("LES\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression GEQ Expression {printf("GEQ\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression LEQ Expression {printf("LEQ\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression SHL Expression {printf("SHL\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression SHR Expression {printf("SHR\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression SUB Expression {printf("SUB\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression ADD Expression {printf("ADD\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression REM Expression {printf("REM\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression DIV Expression {printf("DIV\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression MUL Expression {printf("MUL\n");Object obj; obj.type = $3.type; $$ = obj;} | Expression INC_ASSIGN { printf("INC_ASSIGN\n"); Object obj; obj.type = $1.type; $$ = obj; } | Expression DEC_ASSIGN { printf("DEC_ASSIGN\n"); Object obj; obj.type = $1.type; $$ = obj; } | '(' Expression ')' {Object obj; obj.type = $2.type; $$ = obj;} | '-' Expression %prec UMINUS { $$ = -$2; Object obj; obj.type = $2.type; $$ = obj;} | CALL { Object obj; obj.type = $1.type; $$ = obj; } ; Assign : Expression VAL_ASSIGN Expression { printf("EQL_ASSIGN\n"); } | Expression ADD_ASSIGN Expression { printf("ADD_ASSIGN\n"); } | Expression SUB_ASSIGN Expression { printf("SUB_ASSIGN\n"); } | Expression MUL_ASSIGN Expression { printf("MUL_ASSIGN\n"); } | Expression DIV_ASSIGN Expression { printf("DIV_ASSIGN\n"); } | Expression REM_ASSIGN Expression { printf("REM_ASSIGN\n"); } | Expression SHR_ASSIGN Expression { printf("SHR_ASSIGN\n"); } | Expression SHL_ASSIGN Expression { printf("SHL_ASSIGN\n"); } | Expression BAN_ASSIGN Expression { printf("BAN_ASSIGN\n"); } | Expression BOR_ASSIGN Expression { printf("BOR_ASSIGN\n"); } | Expression BXO_ASSIGN Expression { printf("BOX_ASSIGN\n"); } | Expression INC_ASSIGN Expression { printf("INC_ASSIGN\n"); } | Expression DEC_ASSIGN Expression { printf("DEC_ASSIGN\n"); } ; CALL : IDENT '(' ARGS ')' { Object obj; obj.type = OBJECT_TYPE_FLOAT; $$ = obj; } ; num : SUB num { printf("NEG\n"); } | NOT num { printf("NOT\n"); } | BNT num { printf("BNT\n"); } | FLOAT_LIT {printf("FLOAT_LIT %f\n", $1);Object obj; obj.type = OBJECT_TYPE_FLOAT; $$ = obj;} | INT_LIT {printf("INT_LIT %f\n", $1);Object obj; obj.type = OBJECT_TYPE_INT; $$ = obj;} ; ARGS : ARGS ',' ARG | ARG ; ARG : STR_LIT { printf("STR_LIT \"%s\"\n", $1); Object obj; obj.type = OBJECT_TYPE_STR; $$ = obj;} | boolean { $$ = $1; Object obj; obj.type = $1.type; $$ = obj; } | num {$$ = $1; Object obj; obj.type = $1.type; $$ = obj; } | id {$$ = $1; Object obj; obj.type = $1.type; $$ = obj; } ; boolean : NOT boolean { printf("NOT\n"); Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; } | BNT boolean { printf("BNT\n"); Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; } | BOOL_LIT { printf("BOOL_LIT %s\n", $1 ? "TRUE" : "FALSE"); Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; } ; id : SUB id { printf("NEG\n"); } | NOT id { printf("NOT\n"); } | BNT id { printf("BNT\n"); } | IDENT { Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; } | IDENT '[' ARG ']' '[' ARG ']' { Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; } | IDENT '[' ARG ']'{ Object obj; obj.type = OBJECT_TYPE_BOOL; $$ = obj; } ; %% /* C code section */
Editor is loading...
Leave a Comment