lolamontes69

K+R Exercise5_19

Sep 25th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. /* Modify undcl so that it does not add redundant parenthese to declarations. */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. #define MAXTOKEN 100
  8. #define BUFSIZE 100
  9.  
  10. enum {NAME, PARENS, BRACKETS}; /* NAME is 0, PARENS has val 1, BRACKETS 2 */
  11.  
  12. char out[1000];
  13. char token[MAXTOKEN];    /* last token string */
  14. int tokentype;           /* type of last token */
  15.  
  16. char tmpstore[MAXTOKEN];
  17.  
  18. int flag=0;              /* whether to add parens or not */
  19. int pos = 0;             /* whether '*' comes before or after '[]()' */
  20.  
  21. char buf[BUFSIZE];
  22. int bufp = 0;            /* next free position in buffer    */
  23.  
  24. int getnext(void);
  25. int gettoken(void);
  26. int getch(void);
  27. void ungetch(int c);
  28.  
  29.  
  30. /* undcl: convert word descriptions to declarations */
  31. main()
  32. {
  33.     int type, i=0;
  34.     char temp[MAXTOKEN];
  35.  
  36.     while(gettoken()!=EOF) {
  37.         strcpy(out,token);
  38.         while((type=gettoken())!='\n') {
  39.             if(type=='*') {
  40.                 if(flag)
  41.                     sprintf(temp, "*%s", out);
  42.                 else {
  43.                     sprintf(tmpstore,"*%s",out);
  44.                     i = getnext();
  45.                     sprintf(temp, "%s", tmpstore);
  46.                 }
  47.                 if(!pos) {
  48.                     pos=1;
  49.                 }
  50.                 strcpy(out, temp);
  51.             }
  52.             else if(type == PARENS || type == BRACKETS) {
  53.                 strcat(out,token);
  54.             } else if(type==NAME) {
  55.                 if(!pos) {
  56.                     pos=1;
  57.                     flag=1;
  58.                 }
  59.                 sprintf(temp, "%s %s", token, out);
  60.                 strcpy(out, temp);
  61.             } else {
  62.                 printf("invalid input at %s\n",token);
  63.                 break;
  64.             }
  65.         }
  66.         printf("%s\n\n",temp);
  67.         flag = 0;
  68.         pos = 0;
  69.     }
  70.     return 0;
  71. }
  72.  
  73. int getnext(void)
  74. {
  75.     char temp[MAXTOKEN];
  76.     int i, c, type;
  77.     strcpy(temp, tmpstore);
  78.    
  79.     while((type=gettoken())!='\n')
  80.         if(type=='*') {
  81.             sprintf(tmpstore,"*%s",temp);
  82.             sprintf(temp, "%s", tmpstore);
  83.         }
  84.         else if(type == NAME) {
  85.             pos=1;
  86.             flag=1;
  87.             sprintf(tmpstore,"%s %s",token,temp);
  88.             return 1;
  89.         }
  90.         else {
  91.             sprintf(tmpstore,"(%s)",temp);
  92.             strcat(tmpstore,token);
  93.             return 0;
  94.         }
  95. }
  96.  
  97. int gettoken(void)  /* return next token */
  98. {
  99.     int c;     /* Note the function prototype getch*/
  100.     char *p = token;
  101.  
  102.     while((c=getch())==' ' || c == '\t')
  103.         ;
  104.     if(c == '(') {
  105.         if((c = getch()) == ')') {
  106.             strcpy(token, "()");
  107.             return tokentype = PARENS;
  108.         } else {
  109.             ungetch(c);
  110.             return tokentype = '(';
  111.         }
  112.     } else if(c == '[') {
  113.         for(*p++ =c; (*p++ = getch()) != ']'; )
  114.             ;
  115.         *p = '\0';
  116.         return tokentype = BRACKETS;
  117.     } else if(isalpha(c)) {
  118.         for(*p++ = c; isalnum(c = getch()); )
  119.             *p++ = c;
  120.         *p = '\0';
  121.         ungetch(c);
  122.         return tokentype = NAME;
  123.     } else
  124.         return tokentype = c;
  125. }
  126.  
  127. /* get a (possibly pushed-back) character */
  128. int getch(void)
  129. {
  130.     return (bufp > 0) ? buf[--bufp] : getchar();  
  131. }
  132.  
  133. /* push character back on input */
  134. void ungetch(int c)
  135. {
  136.     if(bufp >= BUFSIZE)
  137.         printf("ungetch: too many characters\n");
  138.     else
  139.         buf[bufp++] = c;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment