home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / coven / coven-utils-1.1.tgz / coven-utils-1.1.tar / utils / coven-language / Coven_parser.y < prev    next >
Text File  |  2003-01-28  |  12KB  |  530 lines

  1. /*
  2.  * (C) 2001 Clemson University
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7.  
  8. %{
  9.  
  10. #include <Coven_lang.h>
  11. #include <symbol.h>
  12. #include <stringtbl.h>
  13.  
  14. sym_ent_p cur_mod_sym = NULL;
  15. int mod_arg_pos = 0;
  16. int num_mods = 0;
  17. int num_consts = 0;
  18. int num_master_threads = 0;
  19. int num_slave_threads = 0;
  20. int for_start_loop = 0;
  21. int for_start[128];
  22. int cur_mod_number = -1;
  23. int loop_num = 0;
  24. int depth_loop_num = 0;
  25. int master_section = 1;
  26. int master_thread = 0;
  27. int slave_thread = 0;
  28. mod_call *cur_mod_call = NULL;
  29. mod_call *first_mod_call = NULL;
  30.  
  31. %}
  32.  
  33. %union{
  34.     int i;
  35.     double f;
  36.     char *c;
  37.     sym_ent *s;
  38.     argument_ent *a;
  39. }
  40.  
  41. %token <i> MODULES
  42. %token <i> CONSTANTS 
  43. %token <i> VARIABLES
  44. %token <i> PROGRAM
  45. %token <i> MODULE
  46. %token <c> IDENTIFIER
  47. %token <i> INPUT
  48. %token <i> OUTPUT
  49. %token <i> INOUT
  50. %token <i> STATIC
  51. %token <i> CONST
  52. %token <i> BUFFER
  53. %token <i> PARAMETER
  54. %token <i> LBRACE
  55. %token <i> RBRACE
  56. %token <i> COMMA
  57. %token <i> SEMICOLON
  58. %token <i> THREAD
  59. %token <i> PARALLEL
  60. %token <c> STRING
  61. %token <i> INTEGER
  62. %token <f> FLOAT
  63. %token <i> EQUALS
  64. %token <i> IF
  65. %token <i> LPAREN
  66. %token <i> RPAREN
  67. %token <i> ELSEIF
  68. %token <i> ELSE
  69. %token <i> WHILE
  70. %token <i> FOR
  71. %token <i> LT
  72. %token <i> LEQ
  73. %token <i> GT
  74. %token <i> GEQ
  75. %token <i> EQEQ
  76. %token <i> NEQ
  77. %token <i> PLUSPLUS
  78. %token <i> MINUSMINUS
  79. %token <i> PLUSEQ
  80. %token <i> MINUSEQ
  81.  
  82. %type <i> coven_program, module_section, direction, mclass, .vclass., cmp_operator, for_last_seg
  83.  
  84. %type <f>
  85.  
  86. %type <c> mod_path, identifier, cvalue
  87.  
  88. %type <s> mod_decl, type, .list_of_mod_args., const_decl, var_decl
  89.  
  90. %type <a> mod_arg
  91.  
  92. %start coven_program
  93.  
  94. %%
  95.  
  96. identifier        : IDENTIFIER
  97.               {
  98.                $$ = enter_string($1);
  99.               }
  100.             ;
  101.  
  102. coven_program        : {output_begin();}
  103.               module_section constants_section variables_section 
  104.               program_section
  105.             ;
  106.  
  107. /* --------------------------------------------------------------------------*/
  108. /* BEGIN: MODULES section */
  109. /* --------------------------------------------------------------------------*/
  110. module_section        : MODULES
  111.               {num_mods = 0; output_begin_mod_decls();}
  112.               LBRACE .list_of_mod_decls. RBRACE
  113.               {output_end_mod_decls(num_mods);}
  114.             ;
  115.  
  116. .list_of_mod_decls.    : /* empty */
  117.             | mod_decl .list_of_mod_decls.
  118.             ;
  119.  
  120. mod_decl        : MODULE identifier
  121.               {
  122.                $$ = symenter($2, NULL, TYPE_MODULE);
  123.               }
  124.               LBRACE mod_path .list_of_mod_args. RBRACE
  125.               {
  126.                $3->value.mod->path = $5;
  127.                $3->value.mod->args = $6;
  128.                $3->value.mod->times_called = 0;
  129.                output_mod_decl($3); 
  130.                num_mods++;
  131.               }
  132.             ;
  133.  
  134. mod_path        : STRING 
  135.               {$$ = enter_string($1); }
  136.             ;
  137.  
  138. .list_of_mod_args.    : /* empty */
  139.               {$$ = NULL;}
  140.             | COMMA mod_arg .list_of_mod_args.
  141.               {
  142.                /* $2 is the argument_ent returned by mod_arg */
  143.                $$ = symenter($2->name, $3, TYPE_ARGUMENT);
  144.                $$->value.arg = $2;
  145.                $$->next = $3;
  146.               }
  147.             ;
  148.  
  149. mod_arg            : direction mclass type identifier 
  150.               {
  151.                $$ = (argument_ent_p)emalloc(sizeof(argument_ent));
  152.                $$->name = $4;
  153.                $$->direction = $1;
  154.                $$->mclass = $2;
  155.                $$->type = $3;
  156.               }
  157.             ;
  158.  
  159. direction        : INPUT
  160.             | OUTPUT
  161.             | INOUT
  162.             | STATIC
  163.             | CONST
  164.             ;
  165.  
  166. mclass            : BUFFER
  167.             | PARAMETER
  168.             ;
  169.  
  170. type            : identifier
  171.               {
  172.                sym_ent_p type_sym = lookup_type($1);
  173.                if(type_sym == NULL) {
  174.                    yyerror("Unknown type");
  175.                    exit(1);
  176.                }
  177.                $$ = lookup_type($1);
  178.               }
  179.             ;
  180. /* --------------------------------------------------------------------------*/
  181. /* END: MODULES section */
  182. /* --------------------------------------------------------------------------*/
  183.  
  184. /* --------------------------------------------------------------------------*/
  185. /* BEGIN: CONSTANTS section */
  186. /* --------------------------------------------------------------------------*/
  187.  
  188. constants_section    : CONSTANTS LBRACE 
  189.               { 
  190.                num_consts = 0; 
  191.                output_begin_const_decls();
  192.               } 
  193.               .list_of_const_decls. RBRACE
  194.               {
  195.                output_end_const_decls(num_consts);
  196.               }
  197.             ;
  198.  
  199. .list_of_const_decls.    : /* empty */
  200.             | const_decl .list_of_const_decls.
  201.             ;
  202.  
  203. const_decl        : type identifier EQUALS cvalue SEMICOLON
  204.               {
  205.                $$ = symenter($2, NULL, TYPE_CONST);
  206.                $$->value.con->type = $1;
  207.                $$->value.con->cvalue = $4;
  208.                output_const_decl($$);
  209.                num_consts ++;
  210.               }
  211.             ;
  212.  
  213. cvalue            : STRING 
  214.               { 
  215.                 char *p;
  216.                 p = (char*)emalloc(strlen($1)+1);
  217.                 /* this is so that we strip the quotes from
  218.                  * around it before we put it into the struct */
  219.                 snprintf(p, strlen($1)-1, "%s", &($1[1]));
  220.                 $$ = p;
  221.               }
  222.             | INTEGER 
  223.               { 
  224.                 char *p;
  225.                 p = (char*)emalloc(32);
  226.                 snprintf(p, 32, "%d", $1);
  227.                 $$ = p;
  228.               }
  229.             | FLOAT
  230.               { 
  231.                 char *p;
  232.                 p = (char*)emalloc(32);
  233.                 snprintf(p, 32, "%f", $1);
  234.                 $$ = p;
  235.               }
  236.             ;
  237.  
  238. /* --------------------------------------------------------------------------*/
  239. /* END: CONSTANTS section */
  240. /* --------------------------------------------------------------------------*/
  241.  
  242. /* --------------------------------------------------------------------------*/
  243. /* BEGIN: VARIABLES section */
  244. /* --------------------------------------------------------------------------*/
  245.  
  246. variables_section    : VARIABLES LBRACE .list_of_var_decls. RBRACE
  247.             ;
  248.  
  249. .list_of_var_decls.    : /* empty */
  250.             | var_decl .list_of_var_decls.
  251.             ;
  252.  
  253. var_decl        : .vclass. type identifier SEMICOLON
  254.               {
  255.                $$ = symenter($3, NULL, TYPE_VARIABLE);
  256.                $$->value.var->vclass = $1;
  257.                $$->value.var->type = $2;
  258.               }
  259.             ;
  260.  
  261. .vclass.        : { $$ = -1; } /* empty */
  262.             | BUFFER
  263.             ;
  264.  
  265. /* --------------------------------------------------------------------------*/
  266. /* END: VARIABLES section */
  267. /* --------------------------------------------------------------------------*/
  268.  
  269. /* --------------------------------------------------------------------------*/
  270. /* BEGIN: PROGRAM section */
  271. /* --------------------------------------------------------------------------*/
  272.  
  273. program_section        : PROGRAM LBRACE list_of_pgm_decls RBRACE
  274.               {
  275.                output_all_mod_calls(first_mod_call);
  276.                output_program(master_thread, 
  277.                  slave_thread);
  278.               }
  279.             ;
  280.  
  281. list_of_pgm_decls    : pgm_decl
  282.             | list_of_pgm_decls pgm_decl
  283.             ;
  284.  
  285. pgm_decl        : sp_thread_decl
  286.             | parallel_decl
  287.             ;
  288.  
  289. sp_thread_decl        : thread_decl
  290.             | special_stmt
  291.             ;
  292.  
  293. thread_decl        : THREAD
  294.               {
  295.                /* we need to record which thread we're in here */
  296.                if(master_section) master_thread++;
  297.                else slave_thread++;
  298.               }
  299.               /* identifier */
  300.               LBRACE list_of_stmts RBRACE
  301.             ;
  302.  
  303. /* require at least one thread section */
  304. parallel_decl        : PARALLEL
  305.               {
  306.                /* signify that we're no longer in a 'master proc'
  307.                 * section of the program */
  308.                master_section = 0;
  309.               }
  310.               LBRACE list_of_threads RBRACE
  311.               {
  312.                /* and now we're back in one */
  313.                master_section = 1;
  314.               }
  315.             ;
  316.  
  317. list_of_threads        : sp_thread_decl
  318.             | list_of_threads sp_thread_decl 
  319.             ;
  320.  
  321. /* require at least one program module declaration */
  322. list_of_stmts        : stmt
  323.             | list_of_stmts stmt
  324.             ;
  325.  
  326. special_stmt        : if_stmt
  327.             | while_stmt
  328.             | for_stmt
  329.             ;
  330.  
  331. stmt            : mod_call
  332.             | special_stmt
  333.             | thread_decl
  334.             ;
  335.  
  336. /* BEGIN: mod call */
  337. mod_call        : identifier 
  338.               {
  339.                cur_mod_sym = symlook($1, NULL, TYPE_MODULE);
  340.                //sym_print(cur_mod_sym);
  341.                cur_mod_sym->value.mod->times_called ++;
  342.                output_start_mod_call(cur_mod_sym);
  343.                mod_arg_pos = 0;
  344.               }
  345.               LPAREN .list_ma_mod_args. RPAREN SEMICOLON
  346.               {
  347.                output_end_mod_call(cur_mod_sym);
  348.                cur_mod_number++;
  349.                /* we need to setup the head of the list */
  350.                if(cur_mod_call == NULL) {
  351.                    cur_mod_call = (mod_call*)malloc(
  352.                      sizeof(mod_call));
  353.                    first_mod_call = cur_mod_call;
  354.                }
  355.                else {
  356.                    mod_call *last_mod_call = cur_mod_call;
  357.                    cur_mod_call = (mod_call*)malloc(
  358.                      sizeof(mod_call));
  359.                    last_mod_call->next = cur_mod_call;
  360.                }
  361.                /* record some information about this mod call */
  362.                cur_mod_call->name = cur_mod_sym->name;
  363.                cur_mod_call->proc = (char*)malloc(32);
  364.                if(master_section) {
  365.                    strcpy(cur_mod_call->proc, "MASTER");
  366.                    cur_mod_call->thread = master_thread;
  367.                }
  368.                else {
  369.                    strcpy(cur_mod_call->proc, "SLAVE");
  370.                    cur_mod_call->thread = slave_thread;
  371.                }
  372.                cur_mod_call->btable = (char*)malloc(128);
  373.                sprintf(cur_mod_call->btable, "&_%s_%d",
  374.                  cur_mod_sym->name, 
  375.                  cur_mod_sym->value.mod->times_called);
  376.                cur_mod_call->loop = (char*)malloc(128);
  377.                if(for_start_loop == 1) {
  378.                    for_start[loop_num] = cur_mod_number;
  379.                    //printf("first mod call in a for loop, # = %d\n",
  380.                    //  for_start[loop_num]);
  381.                    for_start_loop = 0;
  382.                    sprintf(cur_mod_call->loop, "&_coven_loop_%d",
  383.                      loop_num);
  384.                }
  385.                else {
  386.                    /* if this is the end of a loop, then we'll
  387.                     * overwrite this string later */
  388.                    strcpy(cur_mod_call->loop, "NULL");
  389.                }
  390.               }
  391.             ;
  392.  
  393. pgm_mod_arg        : identifier 
  394.               {
  395.                //printf("MOD ARG: %s\n", $1);
  396.                output_mod_binding(cur_mod_sym, mod_arg_pos, $1);
  397.                mod_arg_pos++;
  398.               }
  399.             ;
  400.     
  401. /* ndebard:
  402.      we're gonna cut these other options out so that we can simplify
  403.      the compiler at this time */
  404. /*
  405.             | STRING {printf("MOD ARG: string!\n");}
  406.             | INTEGER
  407.             | FLOAT
  408.             | inline_assignment
  409.             ;
  410. */
  411.  
  412. .list_ma_mod_args.    : /* empty */
  413.             | list_ma_mod_args
  414.             ;
  415.  
  416. list_ma_mod_args    : pgm_mod_arg
  417.             | list_ma_mod_args COMMA pgm_mod_arg
  418.             ;
  419.  
  420. /*inline_assignment    : identifier EQUALS identifier
  421.             ;*/
  422.  
  423. /* END: mod call */
  424.  
  425. /* BEGIN: if statement */
  426. if_stmt            : IF LPAREN condition RPAREN LBRACE list_of_stmts RBRACE .else_if_list. .else_stmt.
  427.             ;
  428.  
  429. /* we only allow if(variable) which implies if(variable != 0) */
  430. condition        : identifier
  431.             ;
  432.  
  433. /* elses: they can be empty, because we may not HAVE elses */
  434. .else_if_list.        : /* emtpy */
  435.             | else_if_list
  436.             ;
  437.  
  438. else_if_list        : else_if_stmt
  439.             | else_if_list else_if_stmt
  440.             ;
  441.  
  442. else_if_stmt        : ELSEIF LPAREN condition RPAREN LBRACE list_of_stmts RBRACE
  443.             ;
  444.  
  445. .else_stmt.        : /* empty */
  446.             | ELSE LPAREN condition RPAREN LBRACE list_of_stmts RBRACE
  447.             ;
  448. /* END: if statement */
  449.  
  450. /* BEGIN: while statement */
  451. while_stmt        : WHILE LPAREN condition RPAREN LBRACE list_of_stmts RBRACE
  452.             ;
  453. /* END: while statement */
  454.  
  455. /* BEGIN: for statement */
  456. for_stmt        : FOR LPAREN identifier EQUALS INTEGER SEMICOLON 
  457.               identifier cmp_operator identifier SEMICOLON 
  458.               identifier for_last_seg RPAREN LBRACE
  459.               { /* before for loop internals */
  460.                 for_start_loop = 1;
  461.                 loop_num++;
  462.                 depth_loop_num++;
  463.                 //printf("Starting for loop: loop# = %d, DLN = %d\n",
  464.                 //  loop_num, depth_loop_num);
  465.               }
  466.               list_of_stmts RBRACE
  467.               { /* after for loop internals */
  468.                 int start_val, inc_val, for_end;
  469.                 char coven_cmp_str[64];
  470.                 start_val = $5;
  471.                 inc_val = $12;
  472.                 sprintf(cur_mod_call->loop, "&_coven_loop_%d",
  473.                   depth_loop_num);
  474.                 //printf("For loop done, cur mod = %d\n",
  475.                 //  cur_mod_number);
  476.                 for_end = cur_mod_number;
  477.                 switch($8) {
  478.                     case LT:
  479.                     strcpy(coven_cmp_str, "COVEN_LT");
  480.                     break;
  481.                 case LEQ:
  482.                     strcpy(coven_cmp_str, "COVEN_LEQ");
  483.                     break;
  484.                 case GT:
  485.                     strcpy(coven_cmp_str, "COVEN_GT");
  486.                     break;
  487.                 case GEQ:
  488.                     strcpy(coven_cmp_str, "COVEN_GEQ");
  489.                     break;
  490.                 case EQEQ:
  491.                     strcpy(coven_cmp_str, "COVEN_EQ");
  492.                     break;
  493.                 case NEQ:
  494.                     strcpy(coven_cmp_str, "COVEN_NEQ");
  495.                     break;
  496.                 }
  497.                 output_loop(depth_loop_num, $3, 
  498.                   start_val, coven_cmp_str,
  499.                   $9, inc_val, for_start[depth_loop_num], for_end);
  500.                 for_start_loop = 0;
  501.                 depth_loop_num--;
  502.               }
  503.             ;
  504.  
  505. cmp_operator        : LT
  506.             | LEQ
  507.             | GT
  508.             | GEQ
  509.             | EQEQ
  510.             | NEQ
  511.             ;
  512.  
  513. for_last_seg        : PLUSPLUS {$$ = 0;}
  514.             | MINUSMINUS {$$ = 0;}
  515.             | PLUSEQ INTEGER {$$ = $2;}
  516.             | MINUSEQ INTEGER {$$ = $2;}
  517.             ;
  518. /* END: for statement */
  519.  
  520. /* --------------------------------------------------------------------------*/
  521. /* END: PROGRAM section */
  522. /* --------------------------------------------------------------------------*/
  523.  
  524. %%
  525.  
  526. yywrap()
  527. {
  528.     return 1;
  529. }
  530.