%{ /* -*- Mode: c++ -*- */ #include #include #include "h.paren-parser" #include "mytree.h" int my_error_count=0; void yyerror(char*s) { my_error_count++; fprintf(stderr,"%s:%d:%d:'%s':%s\n",filename,yylineno,yypos,yytext,s); } int yylex(); %} %union { tr_id *non_id; treevec_type* star; tr_prog * non_prog; vec_prog * nvec_prog; tr_module * non_module; vec_module * nvec_module; tr_exports * non_exports; vec_exports * nvec_exports; tr_export * non_export; vec_export * nvec_export; tr_imports * non_imports; vec_imports * nvec_imports; tr_import * non_import; vec_import * nvec_import; tr_type_class * non_type_class; vec_type_class * nvec_type_class; tr_topdecl * non_topdecl; vec_topdecl * nvec_topdecl; tr_type_class_decl * non_type_class_decl; vec_type_class_decl * nvec_type_class_decl; tr_type_signature * non_type_signature; vec_type_signature * nvec_type_signature; tr_instance * non_instance; vec_instance * nvec_instance; tr_newtype * non_newtype; vec_newtype * nvec_newtype; tr_deriving * non_deriving; vec_deriving * nvec_deriving; tr_id_non * non_id_non; vec_id_non * nvec_id_non; tr_type_synonym * non_type_synonym; vec_type_synonym * nvec_type_synonym; tr_data * non_data; vec_data * nvec_data; tr_simpletype * non_simpletype; vec_simpletype * nvec_simpletype; tr_constrs * non_constrs; vec_constrs * nvec_constrs; tr_field_type_and_param * non_field_type_and_param; vec_field_type_and_param * nvec_field_type_and_param; tr_type_and_param * non_type_and_param; vec_type_and_param * nvec_type_and_param; tr_constr * non_constr; vec_constr * nvec_constr; tr_field_label_constructor * non_field_label_constructor; vec_field_label_constructor * nvec_field_label_constructor; tr_decls * non_decls; vec_decls * nvec_decls; tr_context * non_context; vec_context * nvec_context; tr_a_context * non_a_context; vec_a_context * nvec_a_context; tr_ret_type_and_params * non_ret_type_and_params; vec_ret_type_and_params * nvec_ret_type_and_params; tr_decl * non_decl; vec_decl * nvec_decl; tr_name * non_name; vec_name * nvec_name; tr_positional_constructor * non_positional_constructor; vec_positional_constructor * nvec_positional_constructor; tr_pattern * non_pattern; vec_pattern * nvec_pattern; tr_pattern_ctor * non_pattern_ctor; vec_pattern_ctor * nvec_pattern_ctor; tr_fpat * non_fpat; vec_fpat * nvec_fpat; tr_type * non_type; vec_type * nvec_type; tr_typepls * non_typepls; vec_typepls * nvec_typepls; tr_paren_type * non_paren_type; vec_paren_type * nvec_paren_type; tr_type_ctor * non_type_ctor; vec_type_ctor * nvec_type_ctor; tr_param * non_param; vec_param * nvec_param; tr_qastring * non_qastring; vec_qastring * nvec_qastring; tr_expr * non_expr; vec_expr * nvec_expr; tr_assignments * non_assignments; vec_assignments * nvec_assignments; tr_stmt * non_stmt; vec_stmt * nvec_stmt; tr_alt * non_alt; vec_alt * nvec_alt; } %token gk_id gk_f gk_j gk_astring gk_AS gk_CASE gk_CBEG gk_CC gk_CFD gk_CLASS gk_COMPOSE gk_CONS gk_CONS_LIST gk_CONTEXT gk_DATA gk_DERIVING gk_DO gk_EXPORT_EVERYTHING gk_FIELD gk_FN gk_FPAT gk_FUN gk_GENERIC gk_HEXTOKEN__3A3D gk_INSTANCE gk_LIST gk_LIT gk_MCONS gk_MLIST gk_MTUPLE gk_NEWTYPE gk_NIL gk_NONDET gk_NOTHING gk_NO_SIG gk_PCHAR gk_PIPE gk_PLIST gk_PSTRING gk_PTUPLE gk_STRICT gk_TUPLE gk_TY gk_TYPE_CLASS gk_TYPE_SYNONYM gk_TYSIG gk_UNIT gk_WHERE %type gk_prog %type gk_module %type gk_exports %type gk_export %type gk_imports %type gk_import %type gk_type_class %type gk_topdecl %type gk_type_class_decl %type gk_type_signature %type gk_instance %type gk_newtype %type gk_deriving %type gk_id_non %type gk_type_synonym %type gk_data %type gk_simpletype %type gk_constrs %type gk_field_type_and_param %type gk_type_and_param %type gk_constr %type gk_field_label_constructor %type gk_decls %type gk_context %type gk_a_context %type gk_ret_type_and_params %type gk_decl %type gk_name %type gk_positional_constructor %type gk_pattern %type gk_pattern_ctor %type gk_fpat %type gk_type %type gk_typepls %type gk_paren_type %type gk_type_ctor %type gk_param %type gk_qastring %type gk_expr %type gk_assignments %type gk_stmt %type gk_alt %type ag_a_context_plus %type ag_a_context_star %type ag_alt_star %type ag_assignments_star %type ag_constr_star %type ag_context_opt %type ag_decl_star %type ag_deriving_opt %type ag_export_star %type ag_expr_plus %type ag_expr_star %type ag_field_type_and_param_star %type ag_fpat_star %type ag_id_non_plus %type ag_id_non_star %type ag_import_star %type ag_paren_type_plus %type ag_paren_type_star %type ag_pattern_plus %type ag_pattern_star %type ag_stmt_star %type ag_topdecl_star %type ag_type_and_param_star %type ag_type_class_decl_star %type ag_type_plus %type ag_type_star %type ag_typepls_opt %% gk_start: gk_prog {$1->print();} ; gk_prog: gk_module {$$=new tr_prog($1);} ; gk_module: gk_f gk_CLASS gk_id gk_exports gk_imports gk_CBEG gk_f ag_topdecl_star gk_j gk_j {$$=new tr_module($2,$3,$4,$5,$6,$8);} ; gk_exports: gk_f ag_export_star gk_j {$$=new tr_exports___f_export_star_j($2);} | gk_EXPORT_EVERYTHING {$$=new tr_exports___EXPORT_EVERYTHING($1);} ; gk_export: gk_id {$$=new tr_export($1);} ; gk_imports: gk_f ag_import_star gk_j {$$=new tr_imports($2);} ; gk_import: gk_id {$$=new tr_import($1);} ; gk_type_class: gk_f gk_TYPE_CLASS ag_context_opt gk_type gk_f ag_type_class_decl_star gk_j gk_j {$$=new tr_type_class($2,$3,$4,$6);} ; gk_topdecl: gk_decl {$$=new tr_topdecl___decl($1);} | gk_data {$$=new tr_topdecl___data($1);} | gk_type_synonym {$$=new tr_topdecl___type_synonym($1);} | gk_newtype {$$=new tr_topdecl___newtype($1);} | gk_instance {$$=new tr_topdecl___instance($1);} | gk_type_class {$$=new tr_topdecl___type_class($1);} ; gk_type_class_decl: gk_type_signature {$$=new tr_type_class_decl___type_signature($1);} | gk_decl {$$=new tr_type_class_decl___decl($1);} ; gk_type_signature: gk_f gk_TYSIG gk_name gk_ret_type_and_params gk_j {$$=new tr_type_signature($2,$3,$4);} ; gk_instance: gk_f gk_INSTANCE gk_id gk_simpletype gk_decls gk_j {$$=new tr_instance($2,$3,$4,$5);} ; gk_newtype: gk_f gk_NEWTYPE gk_simpletype gk_type ag_deriving_opt gk_j {$$=new tr_newtype($2,$3,$4,$5);} ; gk_deriving: gk_DERIVING gk_f ag_id_non_plus gk_j {$$=new tr_deriving($1,$3);} ; gk_id_non: gk_id {$$=new tr_id_non($1);} ; gk_type_synonym: gk_f gk_TYPE_SYNONYM gk_simpletype gk_type gk_j {$$=new tr_type_synonym($2,$3,$4);} ; gk_data: gk_f gk_DATA gk_simpletype gk_constrs ag_deriving_opt gk_j {$$=new tr_data($2,$3,$4,$5);} ; gk_simpletype: gk_f ag_id_non_plus gk_j {$$=new tr_simpletype($2);} ; gk_constrs: gk_f ag_constr_star gk_j {$$=new tr_constrs($2);} ; gk_field_type_and_param: gk_f gk_param gk_type gk_j {$$=new tr_field_type_and_param($2,$3);} ; gk_type_and_param: gk_f gk_param gk_type gk_j {$$=new tr_type_and_param($2,$3);} ; gk_constr: gk_positional_constructor {$$=new tr_constr___positional_constructor($1);} | gk_field_label_constructor {$$=new tr_constr___field_label_constructor($1);} ; gk_field_label_constructor: gk_f gk_type_ctor gk_FIELD gk_f ag_field_type_and_param_star gk_j gk_j {$$=new tr_field_label_constructor($2,$3,$5);} ; gk_decls: ag_decl_star {$$=new tr_decls($1);} ; gk_context: gk_CONTEXT gk_f ag_a_context_plus gk_j {$$=new tr_context($1,$3);} ; gk_a_context: gk_f gk_id gk_id gk_j {$$=new tr_a_context($2,$3);} ; gk_ret_type_and_params: ag_context_opt gk_type gk_f ag_type_and_param_star gk_j {$$=new tr_ret_type_and_params($1,$2,$4);} ; gk_decl: gk_f gk_FUN gk_name gk_ret_type_and_params gk_expr gk_j {$$=new tr_decl___f_FUN_name_ret_type_and_params_expr_j($2,$3,$4,$5);} | gk_f gk_FUN gk_name gk_NO_SIG gk_ret_type_and_params gk_expr gk_j {$$=new tr_decl___f_FUN_name_NO_SIG_ret_type_and_params_expr_j($2,$3,$4,$5,$6);} ; gk_name: gk_id {$$=new tr_name($1);} ; gk_positional_constructor: gk_f gk_type_ctor ag_typepls_opt gk_j {$$=new tr_positional_constructor___f_type_ctor_typepls_opt_j($2,$3);} | gk_f gk_TUPLE ag_type_plus gk_j {$$=new tr_positional_constructor___f_TUPLE_type_plus_j($2,$3);} ; gk_pattern: gk_f gk_pattern_ctor ag_pattern_star gk_j {$$=new tr_pattern___f_pattern_ctor_pattern_star_j($2,$3);} | gk_f gk_pattern_ctor gk_FPAT gk_f ag_fpat_star gk_j gk_j {$$=new tr_pattern___f_pattern_ctor_FPAT_f_fpat_star_j_j($2,$3,$5);} | gk_f gk_PTUPLE ag_pattern_plus gk_j {$$=new tr_pattern___f_PTUPLE_pattern_plus_j($2,$3);} | gk_f gk_PLIST ag_pattern_plus gk_j {$$=new tr_pattern___f_PLIST_pattern_plus_j($2,$3);} | gk_f gk_PCHAR gk_astring gk_j {$$=new tr_pattern___f_PCHAR_astring_j($2,$3);} | gk_f gk_PSTRING gk_astring gk_j {$$=new tr_pattern___f_PSTRING_astring_j($2,$3);} | gk_f gk_AS gk_id gk_pattern gk_j {$$=new tr_pattern___f_AS_id_pattern_j($2,$3,$4);} ; gk_pattern_ctor: gk_id {$$=new tr_pattern_ctor___id($1);} | gk_CONS {$$=new tr_pattern_ctor___CONS($1);} | gk_NIL {$$=new tr_pattern_ctor___NIL($1);} ; gk_fpat: gk_f gk_id gk_pattern gk_j {$$=new tr_fpat($2,$3);} ; gk_type: gk_f gk_FN gk_ret_type_and_params gk_j {$$=new tr_type___f_FN_ret_type_and_params_j($2,$3);} | gk_UNIT {$$=new tr_type___UNIT($1);} | gk_positional_constructor {$$=new tr_type___positional_constructor($1);} ; gk_typepls: ag_paren_type_plus {$$=new tr_typepls($1);} ; gk_paren_type: gk_type {$$=new tr_paren_type___type($1);} | gk_f gk_STRICT gk_type gk_j {$$=new tr_paren_type___f_STRICT_type_j($2,$3);} | gk_f gk_GENERIC gk_id gk_j {$$=new tr_paren_type___f_GENERIC_id_j($2,$3);} ; gk_type_ctor: gk_id {$$=new tr_type_ctor___id($1);} | gk_LIST {$$=new tr_type_ctor___LIST($1);} | gk_NONDET {$$=new tr_type_ctor___NONDET($1);} ; gk_param: gk_id {$$=new tr_param($1);} ; gk_qastring: gk_astring {$$=new tr_qastring($1);} ; gk_expr: gk_id {$$=new tr_expr___id($1);} | gk_MCONS {$$=new tr_expr___MCONS($1);} | gk_f gk_PIPE ag_expr_star gk_j {$$=new tr_expr___f_PIPE_expr_star_j($2,$3);} | gk_f gk_CC ag_expr_star gk_j {$$=new tr_expr___f_CC_expr_star_j($2,$3);} | gk_qastring {$$=new tr_expr___qastring($1);} | gk_f gk_LIT gk_astring gk_j {$$=new tr_expr___f_LIT_astring_j($2,$3);} | gk_f gk_TY gk_type gk_expr gk_j {$$=new tr_expr___f_TY_type_expr_j($2,$3,$4);} | gk_f gk_expr ag_expr_star gk_j {$$=new tr_expr___f_expr_expr_star_j($2,$3);} | gk_f gk_DO ag_stmt_star gk_j {$$=new tr_expr___f_DO_stmt_star_j($2,$3);} | gk_f gk_CASE gk_expr ag_alt_star gk_j {$$=new tr_expr___f_CASE_expr_alt_star_j($2,$3,$4);} | gk_f gk_WHERE gk_expr ag_decl_star gk_j {$$=new tr_expr___f_WHERE_expr_decl_star_j($2,$3,$4);} | gk_f gk_CFD gk_id ag_assignments_star gk_j {$$=new tr_expr___f_CFD_id_assignments_star_j($2,$3,$4);} | gk_f gk_COMPOSE ag_expr_plus gk_j {$$=new tr_expr___f_COMPOSE_expr_plus_j($2,$3);} | gk_f gk_MLIST ag_expr_star gk_j {$$=new tr_expr___f_MLIST_expr_star_j($2,$3);} | gk_f gk_CONS_LIST ag_expr_star gk_j {$$=new tr_expr___f_CONS_LIST_expr_star_j($2,$3);} | gk_f gk_MTUPLE ag_expr_star gk_j {$$=new tr_expr___f_MTUPLE_expr_star_j($2,$3);} | gk_NOTHING {$$=new tr_expr___NOTHING($1);} ; gk_assignments: gk_f gk_id gk_expr gk_j {$$=new tr_assignments($2,$3);} ; gk_stmt: gk_expr {$$=new tr_stmt___expr($1);} | gk_f gk_HEXTOKEN__3A3D gk_id gk_expr gk_j {$$=new tr_stmt___f_HEXTOKEN__3A3D_id_expr_j($2,$3,$4);} ; gk_alt: gk_f gk_pattern gk_expr gk_j {$$=new tr_alt($2,$3);} ; ag_a_context_plus: ag_a_context_star gk_a_context {$$=$1; $$->v.push_back($2);}; ag_a_context_star: {$$=new vec_a_context();} | ag_a_context_star gk_a_context {$$=$1; $$->v.push_back($2);}; ag_alt_star: {$$=new vec_alt();} | ag_alt_star gk_alt {$$=$1; $$->v.push_back($2);}; ag_assignments_star: {$$=new vec_assignments();} | ag_assignments_star gk_assignments {$$=$1; $$->v.push_back($2);}; ag_constr_star: {$$=new vec_constr();} | ag_constr_star gk_constr {$$=$1; $$->v.push_back($2);}; ag_context_opt: {$$=0;} | gk_context; ag_decl_star: {$$=new vec_decl();} | ag_decl_star gk_decl {$$=$1; $$->v.push_back($2);}; ag_deriving_opt: {$$=0;} | gk_deriving; ag_export_star: {$$=new vec_export();} | ag_export_star gk_export {$$=$1; $$->v.push_back($2);}; ag_expr_plus: ag_expr_star gk_expr {$$=$1; $$->v.push_back($2);}; ag_expr_star: {$$=new vec_expr();} | ag_expr_star gk_expr {$$=$1; $$->v.push_back($2);}; ag_field_type_and_param_star: {$$=new vec_field_type_and_param();} | ag_field_type_and_param_star gk_field_type_and_param {$$=$1; $$->v.push_back($2);}; ag_fpat_star: {$$=new vec_fpat();} | ag_fpat_star gk_fpat {$$=$1; $$->v.push_back($2);}; ag_id_non_plus: ag_id_non_star gk_id_non {$$=$1; $$->v.push_back($2);}; ag_id_non_star: {$$=new vec_id_non();} | ag_id_non_star gk_id_non {$$=$1; $$->v.push_back($2);}; ag_import_star: {$$=new vec_import();} | ag_import_star gk_import {$$=$1; $$->v.push_back($2);}; ag_paren_type_plus: ag_paren_type_star gk_paren_type {$$=$1; $$->v.push_back($2);}; ag_paren_type_star: {$$=new vec_paren_type();} | ag_paren_type_star gk_paren_type {$$=$1; $$->v.push_back($2);}; ag_pattern_plus: ag_pattern_star gk_pattern {$$=$1; $$->v.push_back($2);}; ag_pattern_star: {$$=new vec_pattern();} | ag_pattern_star gk_pattern {$$=$1; $$->v.push_back($2);}; ag_stmt_star: {$$=new vec_stmt();} | ag_stmt_star gk_stmt {$$=$1; $$->v.push_back($2);}; ag_topdecl_star: {$$=new vec_topdecl();} | ag_topdecl_star gk_topdecl {$$=$1; $$->v.push_back($2);}; ag_type_and_param_star: {$$=new vec_type_and_param();} | ag_type_and_param_star gk_type_and_param {$$=$1; $$->v.push_back($2);}; ag_type_class_decl_star: {$$=new vec_type_class_decl();} | ag_type_class_decl_star gk_type_class_decl {$$=$1; $$->v.push_back($2);}; ag_type_plus: ag_type_star gk_type {$$=$1; $$->v.push_back($2);}; ag_type_star: {$$=new vec_type();} | ag_type_star gk_type {$$=$1; $$->v.push_back($2);}; ag_typepls_opt: {$$=0;} | gk_typepls; %% int yylineno=1; /* !!! WHY */ int yypos=0; FILE*tree_type::fo=stdout; char*filename; void out(const char *x){ fprintf(tree_type::fo,"%s",x); } /* void opt_print(const tree_type*x){ if (x) { x->print(); } }*/ int main(int argc,char**argv){ assert(argc==2); filename=argv[1]; // yydebug=1; yyparse(); if (my_error_count>0) return 1; else return 0; }