home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / educatio / xcoral16.zip / GEN.Y < prev    next >
Text File  |  1993-01-15  |  9KB  |  343 lines

  1. %{
  2.  
  3. #include "global_parse.h"
  4. #include "ResultTypes.h"
  5. #include "FileDict.h"
  6. #include "ProcDict.h"
  7. #include "ClassDict.h"
  8. #include "BrowserParse.h"
  9. #include "BrowserUtil.h"
  10. #include <stdio.h>
  11.  
  12.  
  13. /*------------------------------------------------------------------------------
  14. */
  15. char yy_class_name[256];
  16.  
  17.  
  18. static ScopeType yy_scope;
  19.  
  20. #define S_PRECEDENCE       10
  21. #define P_PRECEDENCE       10
  22. #define A_PRECEDENCE       20
  23. #define MAX_PRECEDENCE     30
  24.  
  25. #define parentheses(ref_prec, inter, result) \
  26.           if (inter.precedence < ref_prec)   \
  27.             strcat(result.text, "(");        \
  28.           strcat(result.text, inter.text);   \
  29.           if (inter.precedence < ref_prec)   \
  30.             strcat(result.text, ")");        \
  31.           result.precedence = ref_prec;
  32.  
  33.  
  34. #define YYDEBUG 1
  35.  
  36.  
  37. %}
  38.  
  39. %start     file
  40.  
  41. %right     '*' '&'
  42. %left      ARRAY_TOK
  43. %token     UNSIGNED_TOK SIGNED_TOK 
  44. %token     CHAR_TOK SHORT_TOK INT_TOK LONG_TOK 
  45. %token     STRUCT_TOK UNION_TOK CLASS_TOK
  46. %token     CONST_TOK
  47. %token     IDENT_TOK CPLUS_TOK C_TOK DECL_TOK SYNC_TOK
  48. %token     PUBLIC_TOK PROTECTED_TOK PRIVATE_TOK
  49. %token     LEX_ERROR_TOK
  50.  
  51. %%
  52.  
  53. /************************************************************************/
  54.  
  55. file
  56.   : { yydebug = 0; }
  57.   | file class_decl
  58.   | file procedure_impl
  59.   | file SYNC_TOK
  60.   ;
  61.  
  62.  
  63. /************************************************************************/
  64.  
  65. class_decl
  66.   : class_name '{' member_list '}'
  67.   | class_name '{' '}'
  68.   | class_name parent_list '{' member_list '}'
  69.   | class_name parent_list '{' '}'
  70.   ;
  71.  
  72.  
  73. /************************************************************************/
  74.  
  75. class_name
  76.   : IDENT_TOK {
  77.       strcpy(yy_class_name, $1.text);
  78.       if (add_class_decl($1.text, $1.position) == BERROR)
  79.         YYABORT;
  80.       yy_scope = PRIVATE_TOK;
  81.     }
  82.   ;
  83.  
  84.  
  85. /************************************************************************/
  86.  
  87. parent_list
  88.   : parent
  89.   | parent_list parent
  90.   ;
  91.  
  92.  
  93. /************************************************************************/
  94.  
  95. parent
  96.   : PUBLIC_TOK    IDENT_TOK   {
  97.       if (add_parent(yy_class_name, $2.text, PUBLIC_SCOPE) == BERROR)
  98.         YYABORT;
  99.     }
  100.   | PROTECTED_TOK IDENT_TOK   {
  101.       if (add_parent(yy_class_name, $2.text, PROTECTED_SCOPE) == BERROR)
  102.         YYABORT;
  103.     }
  104.   | PRIVATE_TOK   IDENT_TOK   {
  105.       if (add_parent(yy_class_name, $2.text, PRIVATE_SCOPE) == BERROR)
  106.         YYABORT;
  107.     }
  108.   | IDENT_TOK                 {
  109.       if (add_parent(yy_class_name, $1.text, PRIVATE_SCOPE) == BERROR)
  110.         YYABORT;
  111.     }
  112.   ;
  113.  
  114.  
  115. /************************************************************************/
  116.  
  117. member_list
  118.   : member_decl
  119.   | member_list member_decl
  120.   ;
  121.  
  122.  
  123. /************************************************************************/
  124.  
  125. member_decl
  126.   : PUBLIC_TOK          {
  127.       yy_scope = PUBLIC_SCOPE;
  128.     }
  129.   | PROTECTED_TOK       {
  130.       yy_scope = PROTECTED_SCOPE;
  131.     }
  132.   | PRIVATE_TOK         {
  133.       yy_scope = PRIVATE_SCOPE;
  134.     }
  135.   | DECL_TOK '(' arguments_list ')' trail_decl {
  136.       strcpy($$.text, $1.text);
  137.       strcat($$.text, "(");
  138.       strcat($$.text, $3.text);
  139.       strcat($$.text, ")");
  140.       strcat($$.text, $5.text);
  141.       if (add_method_decl(yy_class_name, $$.text,
  142.                           yy_scope, $1.is_virtual, $1.position) == BERROR)
  143.         YYABORT;
  144.     }
  145.   | CPLUS_TOK '(' arguments_list ')' trail_decl {
  146.       strcpy($$.text, $1.text);
  147.       strcat($$.text, "(");
  148.       strcat($$.text, $3.text);
  149.       strcat($$.text, ")");
  150.       strcat($$.text, $5.text);
  151.       if (add_method_decl(yy_class_name, $$.text,
  152.                           yy_scope, $1.is_virtual, $1.position) == BERROR)
  153.         YYABORT;
  154.       if (add_method_impl(yy_class_name, $$.text, $1.position) == BERROR)
  155.         YYABORT;
  156.     }
  157.   | C_TOK 
  158.   | SYNC_TOK 
  159.   | LEX_ERROR_TOK {
  160.       fprintf(stderr, ">>>>>>  Arguments buffer overflow at line %d\n", $1.position);
  161.       YYABORT;
  162.     }
  163.   ;
  164.  
  165.  
  166. /************************************************************************/
  167.  
  168. procedure_impl
  169.   : CPLUS_TOK '(' arguments_list ')' trail_decl {
  170.       strcpy($$.text, $1.text);
  171.       strcat($$.text, "(");
  172.       strcat($$.text, $3.text);
  173.       strcat($$.text, ")");
  174.       strcat($$.text, $5.text);
  175.       if (yy_class_name[0] == '\0') {
  176.         if (add_proc_impl($$.text, CPLUS_PROC, $1.position) == BERROR)
  177.           YYABORT;
  178.       }
  179.       else {
  180.         if (add_method_impl(yy_class_name, $$.text, $1.position) == BERROR)
  181.           YYABORT;
  182.       }
  183.       *yy_class_name = '\0';
  184.     }
  185.   | C_TOK  {
  186.       strcpy($$.text, $1.text);
  187.       strcat($$.text, "()");
  188.       if (add_proc_impl($$.text, C_PROC, $1.position) == BERROR)
  189.          YYABORT;
  190.       *yy_class_name = '\0';
  191.     }
  192.   | LEX_ERROR_TOK {
  193.       fprintf(stderr, ">>>>>>  Arguments buffer overflow at line %d\n", $1.position);
  194.       YYABORT;
  195.     } 
  196.   ;
  197.  
  198.  
  199. /************************************************************************/
  200.  
  201. trail_decl
  202.   :           { $$.text[0] = '\0';         }
  203.   | CONST_TOK { strcpy($$.text, " const"); }
  204.   ;
  205.  
  206.  
  207. /************************************************************************/
  208.  
  209. arguments_list
  210.   :                              { $$.text[0] = '\0';        }
  211.   | argument                     { strcpy($$.text, $1.text); }
  212.   | arguments_list ',' argument  {
  213.                                    strcpy($$.text, $1.text);
  214.                                    strcat($$.text, ", ");
  215.                                    strcat($$.text, $3.text);
  216.                                  }
  217.   ;
  218.  
  219.  
  220. /************************************************************************/
  221.  
  222. argument
  223.   : type declarator             { 
  224.       strcpy($$.text, $1.text);
  225.       strcat($$.text, $2.text);
  226.     }
  227.   | CONST_TOK type declarator   { 
  228.       strcpy($$.text, "const ");
  229.       strcat($$.text, $2.text);
  230.       strcat($$.text, $3.text);
  231.     }
  232.   | type CONST_TOK declarator   { 
  233.       strcpy($$.text, "const ");
  234.       strcat($$.text, $1.text);
  235.       strcat($$.text, $3.text);
  236.     }
  237.   ;
  238.  
  239.  
  240. /************************************************************************/
  241.  
  242. type
  243.   : CHAR_TOK                     { strcpy($$.text, "char"); }
  244.   | UNSIGNED_TOK CHAR_TOK        { strcpy($$.text, "unsigned char"); }
  245.   | SIGNED_TOK   CHAR_TOK        { strcpy($$.text, "char"); }
  246.   | SHORT_TOK                    { strcpy($$.text, "short"); }
  247.   | UNSIGNED_TOK SHORT_TOK       { strcpy($$.text, "unsigned short"); }
  248.   | SIGNED_TOK   SHORT_TOK       { strcpy($$.text, "short"); }
  249.   | INT_TOK                      { strcpy($$.text, "int"); }
  250.   | UNSIGNED_TOK INT_TOK         { strcpy($$.text, "unsigned int"); }
  251.   | SIGNED_TOK   INT_TOK         { strcpy($$.text, "int"); }
  252.   | UNSIGNED_TOK                 { strcpy($$.text, "unsigned int"); }
  253.   | SIGNED_TOK                   { strcpy($$.text, "int"); }
  254.   | LONG_TOK                     { strcpy($$.text, "long"); }
  255.   | UNSIGNED_TOK LONG_TOK        { strcpy($$.text, "unsigned long"); }
  256.   | SIGNED_TOK   LONG_TOK        { strcpy($$.text, "long"); }
  257.   | STRUCT_TOK   IDENT_TOK       { strcpy($$.text, $2.text); }
  258.   | UNION_TOK    IDENT_TOK       { strcpy($$.text, $2.text); }
  259.   | CLASS_TOK    IDENT_TOK       { strcpy($$.text, $2.text); }
  260.   | IDENT_TOK                    { strcpy($$.text, $1.text); }
  261.   ;
  262.  
  263.  
  264. /************************************************************************/
  265.  
  266. declarator
  267.   :                                      { 
  268.       $$.text[0]    = '\0';
  269.       $$.precedence = MAX_PRECEDENCE;
  270.     }
  271.   | IDENT_TOK                            { 
  272.       $$.text[0]    = '\0';
  273.       $$.precedence = MAX_PRECEDENCE;
  274.     }
  275.   | '*' declarator                       {
  276.       strcpy($$.text, "*");
  277.       parentheses(S_PRECEDENCE, $2, $$)
  278.     }
  279.   | '*' CONST_TOK declarator  %prec '*'  {
  280.       strcpy($$.text, "*const ");
  281.       parentheses(S_PRECEDENCE, $2, $$)
  282.     }
  283.   | '&' declarator                       {
  284.       strcpy($$.text, "&");
  285.       parentheses(P_PRECEDENCE, $2, $$)
  286.     }                     
  287.   | '&' CONST_TOK declarator  %prec '&'  {
  288.       strcpy($$.text, "&const ");
  289.       parentheses(P_PRECEDENCE, $2, $$)
  290.     }                     
  291.   | declarator ARRAY_TOK                 {
  292.       $$.text[0] = '\0';
  293.       parentheses(A_PRECEDENCE, $1, $$)
  294.       strcat($$.text, "[]");
  295.     }
  296.   | '(' declarator ')' '(' arguments_list ')' {
  297.       strcpy($$.text, "(");
  298.       strcat($$.text, $2.text);
  299.       strcat($$.text, ")");
  300.       strcat($$.text, " (");
  301.       strcat($$.text, $5.text);
  302.       strcat($$.text, ")");
  303.       $$.precedence = MAX_PRECEDENCE;
  304.     }
  305.   | '(' declarator ')'                   {
  306.       strcpy($$.text, $2.text);
  307.       $$.precedence = $2.precedence;
  308.     }
  309.   ;
  310.  
  311.  
  312. /************************************************************************/
  313.  
  314.  
  315. %%
  316.  
  317. int yyerror(error_msg)
  318.     char* error_msg;
  319. {
  320.   fprintf(stderr, ">>>>>>  Internal Parser FATAL ERROR at line %d\n", line_count);
  321.   fprintf(stderr, "    >>  %s\n", error_msg);
  322. }
  323.  
  324.  
  325. int browser_yyparse(file_name)
  326.     char* file_name;
  327. {
  328.   FILE* file;
  329.   int   result;
  330.  
  331.   result = 1;
  332.   *yy_class_name = '\0';
  333.   file = fopen(file_name, "r");
  334.   if (file != Null) {
  335.     flex_init(file);
  336.     result = yyparse();
  337.     fclose(file);
  338.   }
  339.   else
  340.     fprintf(stderr, "\n>>>>>>  Unable to open file %s\n", file_name);
  341.   return(result);
  342. }
  343.