[Code example] dcl page 123,124,125

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

dcl page 123,124,125

Post by BJT » Sat May 13, 2017 1:44 am

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

input

Code: Select all

char (*(*x())[])()
output

Code: Select all

x:  function returning pointer to array[] of pointer to function returning char
word count: 341

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

Who is online

Users browsing this forum: No registered users and 1 guest