home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / qtawk / cdcl.exp < prev    next >
Text File  |  1990-04-30  |  6KB  |  258 lines

  1. # Decipher C Declarator
  2. # declarator syntax (The C Programming Language, 2cd Edition, page 215)
  3. #
  4. # Copyright (C) 1990 Terry D. Boldt, All Rights Reserved
  5. #
  6. #  declarator:
  7. #   pointer direct-declarator
  8. #
  9. #  direct-declarator:
  10. #   identifier
  11. #   ( declarator )
  12. #   direct-declarator [ constant-expression ]
  13. #   direct-declarator ( parameter-type-list )
  14. #   direct-declarator ( identifier-list )
  15. #
  16. #  pointer:
  17. #   * type-qualifier-list
  18. #   * type-qualifier-list pointer
  19. #
  20. #  type-qualifier-list:
  21. #   type-qualifier
  22. #   type-qualifier-list type-qualifier
  23. #
  24. BEGIN {
  25.     # define error reporting file name
  26.     stderr = "stderr";
  27.     #
  28.     # parenthesis are necessary in following regular expressions
  29.     # so that they may be safely invoked in other regular expressions
  30.     #
  31.     # define regular expression for names
  32.     identifier = /([A-Za-z_][A-Za-z0-9_]*)/;
  33.     # define regular expression for function parenthesis
  34.     parens = /(\(\))/;
  35.     # define regular expression for array brackets
  36.     # with optional array size
  37.     brackets = /(\[{_d}*\])/;
  38.     # define regular expression for plain variable names
  39.     var_identifier = /^{identifier}/;
  40.     # define regular expression for function declarations
  41.     function_identifier = /^{identifier}?{parens}+/;
  42.     # define regular expression for array declarations
  43.     array_identifier = /^{identifier}?{brackets}+/;
  44.     # define regular expression for pointer declarations
  45.     ptr_identifier = /^\*+{identifier}?/;
  46.     # define regular expression for leading parenthesis
  47.     # in parenthesized exp.
  48.     # use look-ahead to ensure that ')' NOT immediately following
  49.     ldg_paren = /^\(@[!\)]/;
  50.     # define pointers
  51.     ldg_pointer = /^\*+/;
  52.  
  53.     Quit = /^{_w}*[Qq](uit)?{_w}*$/;
  54.  
  55.     fprintf(stderr,"Decipher C Declarator.\n");
  56.     fprintf(stderr,"Copyright (C) 1990 Terry D. Boldt, All Rights Reserved.\n");
  57.     fprintf(stderr,"\nEnter C Declaration: (or quit)\n");
  58. }
  59.  
  60. Quit { exit 1; }
  61.  
  62. # examine each input line
  63.     {
  64. # use for optionally tracing execution of user defined functions or switch
  65. # statements
  66. #    TRACE = /_u$|^s/;
  67.     token = $1;
  68.     f = 2;
  69.     type = identifier_token = decl_list = "";
  70.     storage_class;
  71.     data_type;                # find data type
  72.     decl;                # find declarations
  73.     if ( token != ";" && token != "" ) {
  74.     fprintf(stderr,"Error, Unrecognized Symbol : %s\n",token);
  75.     exit 1;
  76.     }
  77.     print identifier_token,":",decl_list,type;    # print declations
  78.     fprintf(stderr,"\nEnter C Declaration: (or quit)\n");
  79. }
  80.  
  81. # function to recognize declarations
  82. function decl() {
  83.     local ns;
  84.  
  85.     if ( token ~~ ldg_pointer ) {
  86.     ns = MLENGTH;
  87.     s_token(MLENGTH);
  88.     } else ns = 0;
  89.     type_qualifier;
  90.     dir_decl;
  91.     while ( ns-- ) decl_list = decl_list " pointer to";
  92. }
  93.  
  94. # function to recognize direct declarations
  95. function dir_decl() {
  96.     local tmp_str;
  97.     local loop = TRUE;
  98.  
  99.     while ( loop ) {
  100.     switch ( token ) {
  101.         case function_identifier:    # find function identifier tokens
  102.         if ( match(token,var_identifier) ) {
  103.             identifier_token = substr(token,1,RLENGTH);
  104.             s_token(RLENGTH);
  105.         }
  106.         if ( match(token,/^{parens}+/) ) {
  107.             tmp_str = substr(token,1,RLENGTH);
  108.             s_token(RLENGTH);
  109.         }
  110.         gsub(parens," function returning",tmp_str); # substitute for parens
  111.         decl_list = decl_list tmp_str;
  112.         break;
  113.         case array_identifier:  # find array identifier tokens
  114.         if ( match(token,var_identifier) ) {
  115.             identifier_token = substr(token,1,RLENGTH);
  116.             s_token(RLENGTH);
  117.         }
  118.         if ( match(token,/^{brackets}+/) ) {
  119.             tmp_str = substr(token,1,RLENGTH);
  120.             s_token(RLENGTH);
  121.         }
  122.         gsub(brackets," array $$0 of",tmp_str); # substitute for arrays
  123.         gsub(/[\[\]]/,"",tmp_str);  # remove '[' & ']'
  124.         decl_list = decl_list tmp_str;
  125.         break;
  126.         case ldg_paren:  # find tokens starting with '('
  127.         s_token(1);
  128.         decl;
  129.         if ( token !~ /^\)/ ) { fprintf(stderr,"Error: Missing ')'\nToken: %s.\n",token); exit 2; }
  130.         s_token(1);
  131.         break;
  132.         case var_identifier:    # find identifier tokens
  133.         identifier_token = substr(token,1,CLENGTH);
  134.         s_token(CLENGTH);
  135.         break;
  136.         default:
  137.         loop = FALSE;
  138.         break;
  139.     }
  140.     }
  141. }
  142.  
  143. # function recognize storage class
  144. function storage_class() {
  145.     local loop = TRUE;
  146.  
  147.     while ( loop ) {
  148.     switch ( token ) {
  149.         case "auto":
  150.         type = type " automatic";
  151.         s_token(0);
  152.         break;
  153.         case "extern":
  154.         type = type " external";
  155.         s_token(0);
  156.         break;
  157.         case "register":
  158.         type = type " register";
  159.         s_token(0);
  160.         break;
  161.         case "typedef":
  162.         type = type " type definition: ";
  163.         s_token(0);
  164.         break;
  165.         case "static":
  166.         type = type "static";
  167.         s_token(0);
  168.         break;
  169.         default:
  170.         loop = FALSE;
  171.         break;
  172.     }
  173.     }
  174. }
  175.  
  176. function type_qualifier() {
  177.     local loop = TRUE;
  178.  
  179.     while ( loop ) {
  180.     switch ( token ) {
  181.         case "const":
  182.         decl_list = decl_list " constant";
  183.         s_token(0);
  184.         break;
  185.         case "volatile":
  186.         decl_list = decl_list " volatile";
  187.         s_token(0);
  188.         break;
  189.         default:
  190.         loop = FALSE;
  191.         break;
  192.     }
  193.     }
  194. }
  195.  
  196. function data_type() {
  197.     local loop = TRUE;
  198.  
  199.     while ( loop ) {
  200.     switch ( token ) {
  201.         case "char":
  202.         type = type " character";
  203.         s_token(0);
  204.         break;
  205.         case "double":
  206.         type = type " double";
  207.         s_token(0);
  208.         break;
  209.         case "float":
  210.         type = type " float";
  211.         s_token(0);
  212.         break;
  213.         case "int":
  214.         type = type " integer";
  215.         s_token(0);
  216.         break;
  217.         case "long":
  218.         type = type " long";
  219.         s_token(0);
  220.         break;
  221.         case "short":
  222.         type = type " short";
  223.         s_token(0);
  224.         break;
  225.         case "signed":
  226.         type = type " signed";
  227.         s_token(0);
  228.         break;
  229.         case "unsigned":
  230.         type = type " unsigned";
  231.         s_token(0);
  232.         break;
  233.         case "void":
  234.         type = type " void";
  235.         s_token(0);
  236.         break;
  237.         case "const":
  238.         type = type " constant";
  239.         s_token(0);
  240.         break;
  241.         case "volatile":
  242.         type = type " volatile";
  243.         s_token(0);
  244.         break;
  245.         default:
  246.         loop = FALSE;
  247.         break;
  248.     }
  249.     }
  250. }
  251.  
  252. # function to shave characters from front of token string
  253. function s_token(cnt) {
  254.  
  255.     if ( cnt == 0 || cnt == length(token) ) token = $(f++);
  256.       else token = substr(token,cnt+1);
  257. }
  258.