agronom81

5.20.dcl

Sep 30th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define MAXTOKEN 100
  6. #define BUFSIZE 100
  7.  
  8. enum {NAME, PARENS, BRACKETS};
  9. enum {NO, YES};
  10.  
  11. void dcl(void);         //parse a declarator
  12. void dirdcl(void);      //parse a direct declarator
  13. int gettoken(void);
  14. void paramdcl(void);    //declarator analysis with parameters
  15. void declspec(void);    //specification declaration
  16. int typespec(void);     //type specifier
  17. int typedesc(void);     //descriptor type
  18. int getch(void);        //get a (possibly pushed-back) character
  19. void ungetch(int c);    //push character back on input
  20. void errmesg(char *msg); //error message
  21.  
  22. int tokentype;              //type of last token
  23. char token[MAXTOKEN];       //last token string
  24. char name[MAXTOKEN];        //identifier name
  25. char datatype[MAXTOKEN];    //data type = char, int, etc.
  26. char buf[BUFSIZE];          //buffer for ungetch;
  27. char out[1000];
  28. int errtoken = NO;
  29. int errbrack = NO;
  30.  
  31.  
  32. /* convert declaration to words */
  33. int main()
  34. {
  35.     while(gettoken() != EOF)        //1st token on line
  36.     {
  37.         strcpy(datatype, token);    //is the datatype
  38.         out[0] = '\0';
  39.         dcl();      //parse rest of line
  40.         if(tokentype != '\n')
  41.             printf("syntax error\n");
  42.         printf("%s: %s %s\n", name, out, datatype);
  43.     }
  44.     return 0;
  45. }
  46.  
  47. /* dcl: parse a declarator */
  48. void dcl(void)
  49. {
  50.     int ns;
  51.    
  52.     for(ns = 0; gettoken() == '*';) //count stars
  53.         ns++;
  54.     dirdcl();
  55.     while(ns-- > 0)
  56.         strcat(out, " pointer to");
  57. }
  58.  
  59. /* dirdcl: parse a direct declarator */
  60. void dirdcl(void)
  61. {
  62.     int type;
  63.    
  64.     if(tokentype == '(') //(dcl)
  65.     {
  66.         dcl();
  67.         if(tokentype != ')')
  68.             errmesg("error: missing )\n");
  69.     }
  70.     else if(tokentype == NAME) //variable name
  71.     {
  72.         if(name[0] == '\0')
  73.             strcpy(name, token);
  74.     }
  75.     else
  76.         errtoken = YES;
  77.     while((type = gettoken()) == PARENS || type == BRACKETS || type == '(')
  78.         if(type == PARENS)
  79.             strcat(out, " function returning");
  80.         else if(type == '(')
  81.         {
  82.             strcat(out, " function taking");
  83.             paramdcl();
  84.             strcat(out, " and returning");
  85.         }
  86.         else
  87.         {
  88.             strcat(out, " array");
  89.             strcat(out, token);
  90.             strcat(out, " of");
  91.         }
  92. }
  93.  
  94. /* errmesg: error message */
  95. void errmesg(char *msg)
  96. {
  97.     printf("%s", msg);
  98.     errtoken = YES;
  99. }
  100.  
  101. /* paramdcl: declarator analysis with parameters */
  102. void paramdcl(void)
  103. {
  104.     do
  105.     {
  106.         declspec(); //specification declaration
  107.     } while(tokentype == ',');
  108.     if(tokentype != ')')
  109.         errmesg("missing ) in declarator of parameters\n");
  110. }
  111.  
  112. /* specification declaration */
  113. void declspec(void)
  114. {
  115.     char temp[MAXTOKEN];
  116.    
  117.     temp[0] = '\0';
  118.     gettoken();
  119.     do
  120.     {
  121.         if(tokentype != NAME)
  122.         {
  123.             errtoken = YES;
  124.             dcl();
  125.         }
  126.         else if(typespec() == YES) // type specifier
  127.         {
  128.             strcat(temp, " ");
  129.             strcat(temp, token);
  130.             gettoken();
  131.         }
  132.         else if(typedesc() == YES) //descriptor type
  133.         {
  134.             strcat(temp, " ");
  135.             strcat(temp, token);
  136.             gettoken();
  137.         }
  138.         else
  139.             errmesg("error: unknown type in the parameter list\n");
  140.     } while(tokentype != ',' && tokentype != ')');
  141.     strcat(out, temp);
  142.     if(tokentype == ',')
  143.         strcat(out, ",");
  144. }
  145.  
  146. /* type specifier */
  147. int typespec(void)
  148. {
  149.     static char *types[] = {"char", "int", "void"};
  150.     char *ptypes = token;
  151.     int result, i;
  152.    
  153.     result = NO;
  154.     for(i = 0; i < 3; i++)
  155.         if(strcmp(ptypes, *(types + i)) == 0)
  156.             result = YES;
  157.         else
  158.             result = NO;
  159.     return result;
  160.        
  161. }
  162.  
  163. /*  descriptor type */
  164. int typedesc(void)
  165. {
  166.     static char *typed[] = {"const", "volatile"};
  167.     char *ptd = token;
  168.    
  169.     int result, i;
  170.    
  171.     result = NO;
  172.     for(i = 0; i < 2; i++)
  173.         if(strcmp(ptd, *(typed + i)) == 0)
  174.             result = YES;
  175.         else
  176.             result = NO;
  177.     return result;
  178. }
  179.  
  180. int gettoken(void)
  181. {
  182.     int c;
  183.  
  184.     char *p = token;
  185.    
  186.     if(errtoken == YES)
  187.     {
  188.         errtoken = NO;
  189.         return tokentype;
  190.     }    
  191.     while((c = getch()) == ' ' || c == '\t')
  192.         ;
  193.     if(c == '(')
  194.     {
  195.         if((c = getch()) == ')')
  196.         {
  197.             strcpy(token, "()");
  198.             return tokentype = PARENS;
  199.         }
  200.         else
  201.         {
  202.             ungetch(c);
  203.             return tokentype = '(';
  204.         }
  205.     }
  206.     else if(c == '[')
  207.     {
  208.         for(*p++ = c; *p != ']';)
  209.         {
  210.             *p = getch();
  211.             if(*p != ']')
  212.             {
  213.                 if(*p == '\n' || *p == ')' || *p == '(')
  214.                 {
  215.                     printf("error: missing ]\n");
  216.                     ungetch(*p);
  217.                     *p = ']';
  218.                 }
  219.                 else
  220.                     p++;
  221.             }
  222.         }
  223.         *++p = '\0';
  224.         return tokentype = BRACKETS;
  225.     }
  226.     else if(isalpha(c))
  227.     {
  228.         for(*p++ = c; isalnum(c = getch());)
  229.             *p++ = c;
  230.         *p = '\0';
  231.         ungetch(c);
  232.         return tokentype = NAME;
  233.     }
  234.     else
  235.         return tokentype = c;
  236. }
  237.  
  238. int bufp = 0;
  239.  
  240. int getch(void) // get a (possibly pushed-back) character  
  241. {
  242.    int bufp = 0; //next free position in buf
  243.    return (bufp > 0) ? buf[--bufp] : getchar();
  244. }
  245.  
  246. void ungetch(int c) // push character back on input
  247. {
  248.     if(bufp >= BUFSIZE)
  249.         printf("ungetch: too many characnters\n");
  250.     else
  251.         buf[bufp++] = c;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment