[Exercise] Exercise 5-18. Make dcl recover from input errors 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-18. Make dcl recover from input errors page 126

Post by BJT » Sat May 13, 2017 6:50 pm

Exercise 5-18. Make dcl recover from input errors.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define MAXTOKEN 100
  6.  
  7. enum { NAME, PARENS, BRACKETS };
  8. enum { NO, YES};
  9.  
  10. void dcl(void);
  11. void dirdcl(void);
  12. void erromsg(char*);
  13.  
  14. int gettoken(void);
  15. int tokentype;          /*type of last token */
  16. char token[MAXTOKEN];   /*last token string */
  17. char name[MAXTOKEN];    /*identifier name */
  18. char datatype[MAXTOKEN];/*data type = char, int, etc. */
  19. char out[1000];
  20.  
  21. int prevtoken=NO;
  22.  
  23. int getch(void);
  24. void ungetch(int);
  25.  
  26. int main() /* convert declaration to words */
  27. {
  28.     while (gettoken() != EOF) /* 1st token on line */
  29.     {
  30.         strcpy(datatype, token); /* is the datatype */
  31.         out[0] = '\0';
  32.         dcl();                  /* parse rest of line */
  33.         if (tokentype != '\n')
  34.             printf("syntax error\n");
  35.         printf("%s: %s %s\n", name, out, datatype);
  36.     }
  37.     return 0;
  38. }
  39.  
  40. /* dcl: parse a declarator */
  41. void dcl(void)
  42. {
  43.     int ns;
  44.     for (ns = 0; gettoken() == '*'; ) /* count *'s */
  45.         ns++;
  46.     dirdcl();
  47.     while (ns-- > 0)
  48.         strcat(out, " pointer to");
  49. }
  50.  
  51. /* dirdcl: parse a direct declarator */
  52. void dirdcl(void)
  53. {
  54.     int type;
  55.     if (tokentype == '(')  /* ( dcl ) */
  56.     {
  57.         dcl();
  58.         if (tokentype != ')')
  59.             erromsg("erro: missing\n");
  60.     } else if (tokentype == NAME) /* variable name */
  61.         strcpy(name, token);
  62.     else
  63.         erromsg("error: expected name or (dcl)\n");
  64.  
  65.     while ((type=gettoken()) == PARENS || type == BRACKETS)
  66.         if (type == PARENS)
  67.             strcat(out, " function returning");
  68.         else
  69.         {
  70.             strcat(out, " array");
  71.             strcat(out, token);
  72.             strcat(out, " of");
  73.         }
  74. }
  75.  
  76. void erromsg(char *s)
  77. {
  78.     printf("%s",s);
  79.     prevtoken=YES;
  80. }
  81.  
  82. int gettoken(void) /* return next token */
  83. {
  84.     int c, getch(void);
  85.     void ungetch(int);
  86.     char *p = token;
  87.  
  88.     if (prevtoken==YES)
  89.     {
  90.         prevtoken=NO;
  91.         return tokentype;
  92.     }
  93.  
  94.     while ((c = getch()) == ' ' || c == '\t')
  95.         ;
  96.     if (c == '(')
  97.     {
  98.         if ((c = getch()) == ')')
  99.         {
  100.             strcpy(token, "()");
  101.             return tokentype = PARENS;
  102.         }
  103.         else
  104.         {
  105.             ungetch(c);
  106.             return tokentype = '(';
  107.         }
  108.     }
  109.     else if (c == '[')
  110.     {
  111.         for (*p++ = c; (*p++ = getch()) != ']'; )
  112.             ;
  113.         *p = '\0';
  114.         return tokentype = BRACKETS;
  115.     }
  116.     else if (isalpha(c))
  117.     {
  118.         for (*p++ = c; isalnum(c = getch()); )
  119.             *p++ = c;
  120.         *p = '\0';
  121.         ungetch(c);
  122.         return tokentype = NAME;
  123.     }
  124.     else
  125.         return tokentype = c;
  126. }
  127.  
  128. #define BUFSIZE 100
  129. char buf[BUFSIZE]; /* buffer for ungetch */
  130. int bufp = 0;      /* next free position in buf */
  131.  
  132. int getch(void) /* get a (possibly pushed-back) character */
  133. {
  134.     return (bufp > 0) ? buf[--bufp] : getchar();
  135. }
  136.  
  137. void ungetch(int c)
  138. /* push character back on input */
  139. {
  140.     if (bufp >= BUFSIZE)
  141.         printf("ungetch: too many characters\n");
  142.     else
  143.         buf[bufp++] = c;
  144. }

test

Code: Select all

char *p
p:  pointer to char
int (*p)()
p:  pointer to function returning int
char *p(
syntax error
p:  pointer to char
int *(*x())
syntax error
int:  p
syntax error
x:  function returning pointer to int
word count: 394

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

Who is online

Users browsing this forum: No registered users and 1 guest