[Exercise] Exercise 5-19 undcl not add redundant parentheses page 126

User avatar

Topic author
BJT
Site Admin
Site Admin
Posts: 141
Joined: Thu Apr 20, 2017 4:43 pm
Instruction: University Student
Gender:
Age: 27
Venezuela

Exercise 5-19 undcl not add redundant parentheses page 126

Post by BJT » Sat May 13, 2017 8:17 pm

Exercise 5-19. Modify undcl so that it does not add redundant parentheses to declarations.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define MAXTOKEN 100
  6.  
  7. enum { NAME, PARENS, BRACKETS };
  8. int gettoken(void);
  9. char token[MAXTOKEN];   /*last token string */
  10. char out[1000];
  11. int tokentype;          /*type of last token */
  12.  
  13. int nexttoken(void);
  14.  
  15. int main() /* convert declaration to words */
  16. {
  17.     int type;
  18.     char temp[MAXTOKEN];
  19.  
  20.     while (gettoken() != EOF)
  21.     {
  22.         strcpy(out, token);
  23.         while ((type = gettoken()) != '\n')
  24.         {
  25.             if (type == PARENS || type == BRACKETS)
  26.             strcat(out, token);
  27.             else if (type == '*')
  28.             {
  29.                 if ( (type=nexttoken()) == PARENS || type==BRACKETS )
  30.                     sprintf(temp, "(*%s)", out);
  31.                 else
  32.                     sprintf(temp, "*%s", out);
  33.                 strcpy(out, temp);
  34.             }
  35.             else if (type == NAME)
  36.             {
  37.                 sprintf(temp, "%s %s", token, out);
  38.                 strcpy(out, temp);
  39.             }
  40.             else
  41.                 printf("invalid input at %s\n", token);
  42.         }
  43.  
  44.     }
  45.     printf("%s\n", out);
  46.     return 0;
  47. }
  48.  
  49. enum {NO,YES};
  50. int prevtoken=NO;
  51.  
  52. int nexttoken(void)
  53. {
  54.     int type;
  55.     type=gettoken();
  56.     prevtoken=YES;
  57.     return type;
  58.  
  59. }
  60.  
  61. int gettoken(void) /* return next token */
  62. {
  63.     int c, getch(void);
  64.     void ungetch(int);
  65.     char *p = token;
  66.  
  67.     if (prevtoken==YES)
  68.     {
  69.         prevtoken=NO;
  70.         return tokentype;
  71.     }
  72.  
  73.     while ((c = getch()) == ' ' || c == '\t')
  74.         ;
  75.     if (c == '(')
  76.     {
  77.         if ((c = getch()) == ')')
  78.         {
  79.             strcpy(token, "()");
  80.             return tokentype = PARENS;
  81.         }
  82.         else
  83.         {
  84.             ungetch(c);
  85.             return tokentype = '(';
  86.         }
  87.     }
  88.     else if (c == '[')
  89.     {
  90.         for (*p++ = c; (*p++ = getch()) != ']'; )
  91.             ;
  92.         *p = '\0';
  93.         return tokentype = BRACKETS;
  94.     }
  95.     else if (isalpha(c))
  96.     {
  97.         for (*p++ = c; isalnum(c = getch()); )
  98.             *p++ = c;
  99.         *p = '\0';
  100.         ungetch(c);
  101.         return tokentype = NAME;
  102.     }
  103.     else
  104.         return tokentype = c;
  105. }
  106.  
  107. #define BUFSIZE 100
  108. char buf[BUFSIZE]; /* buffer for ungetch */
  109. int bufp = 0;      /* next free position in buf */
  110.  
  111. int getch(void) /* get a (possibly pushed-back) character */
  112. {
  113.     return (bufp > 0) ? buf[--bufp] : getchar();
  114. }
  115.  
  116. void ungetch(int c)
  117. /* push character back on input */
  118. {
  119.     if (bufp >= BUFSIZE)
  120.         printf("ungetch: too many characters\n");
  121.     else
  122.         buf[bufp++] = c;
  123. }
word count: 287

Link:
BBcode:
HTML:
Hide post links
Show post links

Who is online

Users browsing this forum: No registered users and 1 guest