%{ #include #include #include #include #include using namespace std; extern int yylex(); extern void yyerror(const char*); extern int yyparse(); string prog_string; typedef union { int i_val; /*don't need this for proj 1.*/ std::string* s_val; } YYSTYPE; extern YYSTYPE yyval; %} %union { int i_val; std::string* s_val; } %token UMINUS %token C_STRING %token C_INTEGER %token STRING %token VOID %token INT %token CHAR %token RETURN %token IF %token ELSE %token FOR %token WHILE %token EXTERN %token STRUCT %token OP_PAR %token CL_PAR %token OP_SQU %token CL_SQU %token OP_BCS %token CL_BCS %token OP_PL %token OP_PP %token OP_MI %token OP_MM %token OP_MU %token OP_DIV %token OP_XOR %token OP_EQ %token OP_EQ_EQ %token OP_NOT %token OP_NE %token OP_SEM %token OP_CM %token OP_LT %token OP_GT %token OP_LE %token OP_GE %token OP_QU %token OP_SQ %token ELLIPSIS %token AMP_AMP %token OR_OR %left OR_OR %left AMP_AMP %left OP_NE OP_EQ_EQ %left OP_LT OP_LE OP_GT OP_GE %left OP_PL OP_MI %left OP_MU OP_DIV %nonassoc UMINUS OP_NOT %type element %type function %type fun_prot %type statements %type statement %type assign %type decls %type arith_expr %type variable %% program: { } | element program { prog_string = (*$1) + prog_string; } ; element: function { $$ = $1; } ; function : fun_prot OP_BCS decls statements CL_BCS { $$ = new string("function(" + *$1 + "OP_BCS \n" + *$3 + *$4 + "CL_BCS \n)\n"); } ; fun_prot: { } ; statements: { $$ = new string(""); } | statement statements { $$ = new string(*$1 + *$2); } ; statement: assign OP_SEM { $$ = new string("statement(" + *$1 + ")\n"); } | RETURN arith_expr OP_SEM { $$ = new string("statement(RETURN " + *$2 + ")\n"); } ; assign: variable OP_EQ arith_expr { } ; decls: { } ; arith_expr: variable { $$ = new string("a_expr(" + *$1 + ") "); } | OP_MI arith_expr %prec UMINUS { $$ = new string("a_expr(OP_MI " + *$2 + ") "); } | arith_expr OP_MU arith_expr { $$ = new string("a_expr(" + *$1 + "OP_MU " + *$3 + ") "); } | arith_expr OP_MI arith_expr { $$ = new string("a_expr(" + *$1 + "OP_MI " + *$3 + ") "); } ; variable: C_STRING { $$ = new string("var "); } ; %% int main() { prog_string = ""; yyparse(); cout<