lolamontes69

K+R Exercise5_20

Sep 25th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1. /* Expand dcl to handle declarations with function argument types, qualifiers
  2.  * like const, and so on.
  3.  *
  4.  * now add args inside brackets, lol.
  5.  * **********************************
  6.  *
  7.     Requires input validation then onto structs, yay.
  8.  *
  9.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. #define MAXTOKEN 100
  16. #define BUFSIZE 100  
  17.  
  18. enum {NAME, PARENS, BRACKETS}; /* NAME is 0, PARENS has val 1, BRACKETS 2 */
  19.  
  20. void dcl(void);
  21. void dirdcl(void);
  22. void clearbuffs(void);
  23.  
  24. int gettoken(void);
  25. int tokentype;           /* type of last token */
  26. int lastchar;
  27.  
  28.  
  29. char token[MAXTOKEN];    /* last token string */
  30. char name[MAXTOKEN];     /* identifier name */
  31. char datatype[MAXTOKEN]; /* data type = char, int, etc. */
  32. char out[1000];
  33.  
  34. char fnargs[MAXTOKEN];    /* last token string */
  35. int faflag=0;
  36.  
  37. char buf[BUFSIZE];
  38. int bufp = 0;            /* next free position in buffer    */
  39.  
  40.  
  41. main()  /* convert declaration to words */
  42. {
  43.     int c;
  44.     while(gettoken() != EOF) {     /* 1st token on line */
  45.         strcpy(datatype, token);   /* is the datatype   */
  46.         out[0] = '\0';
  47.         if(!isalpha(datatype[0])) {
  48.             for( ; (c = getch()) !='\n'; )
  49.                 ;
  50.             puts("error: invalid datatype");
  51.             clearbuffs();
  52.         }
  53.         else if(tokentype==NAME || tokentype==PARENS || tokentype==BRACKETS || \
  54.            tokentype=='*') {
  55.             dcl();                     /* parse rest of line if valid */
  56.         }
  57.         else if(tokentype!='\n') {
  58.             for( ; (c = getch()) !='\n'; )
  59.             printf("syntax error\n");
  60.             clearbuffs();
  61.         }
  62.         if(tokentype=='\n') {
  63.             if(lastchar=='[' || lastchar=='(') printf("error: unclosed %c\n",lastchar);
  64.             else printf("%s: %s %s\n", name, out, datatype);
  65.             clearbuffs();
  66.         }
  67.     }
  68.     return 0;
  69. }
  70.  
  71. int gettoken(void)        /* return next token */
  72. {
  73.     int c, i=0, getch(void);   /* Note the function prototype getch*/
  74.     void ungetch(int);
  75.     char *p = token;
  76.  
  77.     while((c=getch())==' ' || c == '\t')
  78.         ;
  79.     if(c == '(') {
  80.         if((c = getch()) == ')') {
  81.             lastchar=0;
  82.             strcpy(token, "()");
  83.             return tokentype = PARENS;
  84.         } else { ///////////////////////////////////////////////////////////////
  85.             fnargs[i++]=c;
  86.             faflag = 1;
  87.             while((c = getch())!=')' && c!='\n') {
  88.                 fnargs[i++]=c;
  89.             }    ///////////////////////////////////////////////////////////////
  90.             if(c==')') {
  91.                 lastchar=0;
  92.                 strcpy(token, "()");
  93.                 return tokentype = PARENS;
  94.             }
  95.             else return tokentype = c;
  96.         }
  97.     } else if(c == '[') {
  98.         for(*p++ =c; (*p++ = (c=getch())) != ']'; ) {
  99.             if(!isalnum(c)) {
  100.                 lastchar = '[';
  101.                 return tokentype=c;
  102.             }
  103.         } *p = '\0';
  104.         lastchar=']';
  105.         return tokentype = BRACKETS;
  106.     } else if(isalpha(c)) {
  107.         for(*p++ = c; isalnum(c = getch()); )
  108.             *p++ = c;
  109.         *p = '\0';
  110.         ungetch(c);
  111.         return tokentype = NAME;
  112.     } else {
  113.         if(c==')' || c==']')
  114.             lastchar=c;
  115.         return tokentype = c;
  116.     }
  117. }
  118.  
  119. /* get a (possibly pushed-back) character */
  120. int getch(void)
  121. {
  122.     return (bufp > 0) ? buf[--bufp] : getchar();
  123. }
  124.  
  125. /* push character back on input */
  126. void ungetch(int c)
  127. {
  128.     if(bufp >= BUFSIZE)
  129.         printf("ungetch: too many characters\n");
  130.     else
  131.         buf[bufp++] = c;
  132. }
  133.  
  134. /* dcl: parse a declarator */
  135. void dcl(void)
  136. {
  137.     int ns;
  138.  
  139.     for(ns=0; gettoken() == '*'; ) /* count *'s */
  140.         ns++;
  141.     dirdcl();
  142.     while(ns-- > 0)
  143.         strcat(out, " pointer to");
  144. }
  145.  
  146. /* dirdcl: parse a direct declarator */
  147. void dirdcl(void)
  148. {
  149.     int type;
  150.     char temp[MAXTOKEN];
  151.  
  152.     if(tokentype=='(') {         /* ( dcl ) */
  153.         dcl();
  154.         if(tokentype!=')')
  155.             printf("error: missing )\n");
  156.     } else if(tokentype == NAME) /* variable name */
  157.         strcpy(name, token);
  158.     else
  159.         printf("error: expected name or (dcl)\n");
  160.     while((type=gettoken())==PARENS || type == BRACKETS || type==NAME) {
  161.         if(type==PARENS) {
  162.             if(faflag) {
  163.                 strcat(out, " function with args(");
  164.                 strcat(out,fnargs);
  165.                 strcat(out,") returning");
  166.             } else strcat(out, " function returning");
  167.         }
  168.         else if(type==NAME) {
  169.             strcat(datatype," ");
  170.             strcat(datatype,name);
  171.             strcpy(name,token);
  172.         }
  173.         else {
  174.             strcat(out, " array");
  175.             strcat(out, token);
  176.             strcat(out, " of");
  177.         }
  178.     }
  179. }
  180.  
  181. void clearbuffs(void)
  182. {
  183.     int i;
  184.     lastchar=0;
  185.     faflag=0;
  186.     for(i=0; name[i]!='\0'; )
  187.         name[i++] = '\0';
  188.     for(i=0; token[i]!='\0'; )
  189.         token[i++] = '\0';
  190.     for(i=0; datatype[i]!='\0'; )
  191.         datatype[i++] = '\0';
  192.     for(i=0; out[i]!='\0'; )
  193.         out[i++] = '\0';
  194.     for(i=0; fnargs[i]!='\0'; )
  195.         fnargs[i++] = '\0';
  196.     while(bufp>0)
  197.         buf[bufp--]='\0';
  198.     tokentype='\n';
  199. }
Advertisement
Add Comment
Please, Sign In to add comment