[Exercise] Exercise 5-20 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-20 page 126

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

Exercise 5-20. Expand dcl to handle declarations with function argument types, qualifiers like const, and so on.

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4. enum { NAME, PARENS, BRACKETS};
  5. enum { NO, YES};
  6. void dcl(void);
  7. void dirdcl(void);
  8. void errmsg(char *);
  9. void dclspec(void);
  10. int typespec(void);
  11. int typeequal(void);
  12. int compare(char **, char**);
  13. void parmdcl(void);
  14.  
  15. int gettoken(void);
  16. extern int tokentype;
  17. extern char token[];
  18. extern char name[];
  19. extern char datatype[];
  20. extern char  out[];
  21. extern int prevtoken;
  22.  
  23.  
  24. //dcl:parse a declarator
  25. void dcl(void)
  26. {
  27.  int ns;
  28.  for(ns = 0; gettoken() == '*';)
  29.   ns++;
  30.  dirdcl();
  31.  while(ns --> 0)
  32.   strcat(out, "pointer to");
  33. }
  34.  
  35.  
  36. //dirdcl: parse a direct declaration
  37. void dirdcl(void)
  38. {
  39.  int type;
  40.  void parmdcl(void);
  41.  if(tokentype == '('){
  42.   dcl();
  43.   if(tokentype != ')')
  44.    errmsg("error:missimg)\n");
  45.  }
  46.  else if(tokentype == NAME){
  47.   if(name[0] == '\0')
  48.    strcpy(name,token);
  49.  }else
  50.   prevtoken = YES;
  51.  while((type = gettoken()) == PARENS || type == BRACKETS || type == '(')
  52.   if(type == PARENS)
  53.    strcat(out, "function returning");
  54.   else if(type == '('){
  55.    strcat(out, "function expecting");
  56.    parmdcl();
  57.    strcat(out, "and returning");
  58.   } else{
  59.  
  60.  
  61.    strcat(out, "array");
  62.    strcat(out, token);
  63.    strcat(out, "of");
  64.   }
  65. }
  66.  
  67. //errmsg: prints the error message
  68. void errmsg(char *msg)
  69. {
  70.  printf("%s\n",msg);
  71.  prevtoken = YES;
  72. }
  73.  
  74.  
  75. //get token:return next token
  76. int gettoken(void)
  77. {
  78.  int c, getch(void);
  79.  void ungetch(int);
  80.  char *p = token;
  81.  if(prevtoken == YES) {
  82.   prevtoken = NO;
  83.   return tokentype;
  84.   while((c = getch()) == ' ' || c == '\t')
  85.    ;
  86.   if(c == '('){
  87.    if ((c = getch()) == ')'){
  88.     strcpy(token,"()");
  89.     return tokentype = PARENS;
  90.    }
  91.    else{
  92.     ungetch(c);
  93.     return tokentype = '(';
  94.    }
  95.   }
  96.   else if(c == '['){
  97.    for(*p++ = c; (*p++ = getch()) != ']';)
  98.     ;
  99.    *p = '\0';
  100.    return tokentype = BRACKETS;
  101.   }
  102.   else if (isalpha(c)) {
  103.    for(*p++ = c; isalnum(c = getch());)
  104.     *p++ = c;
  105.    *p = '\0';
  106.    ungetch(c);
  107.    return tokentype = NAME;
  108.   } else
  109.    return tokentype = c;
  110.  }
  111.  
  112.  
  113.  
  114.  //parmdcl: parse a parameter declarator
  115.  void parmdcl(void)
  116.  {
  117.   do{
  118.    dclspec();
  119.   }while (tokentype == ',');
  120.   if(tokentype != ')')
  121.    errmsg("missing ) in parameter declaration\n");
  122.  }
  123.  
  124.  //dclspec: declaration specification
  125.  void dclspec(void)
  126.  {
  127.   char temp[MAXTOKEN];
  128.   temp[0] = '\0';
  129.   gettoken();
  130.   do{
  131.    if(tokentype != NAME){
  132.     prevtoken = YES;
  133.     dcl();
  134.    }else if(typedesc() == YES){
  135.     strcat(temp," ");
  136.     strcat(temp, token);
  137.     gettoken();
  138.    }else if(typeequal() == YES){
  139.     strcat(temp," ");
  140.     strcat(temp, token);
  141.     gettoken();
  142.    }else
  143.     errmsg("unknown type parameter list\n");
  144.   }while(tokentype != ',' && tokentype != ')');
  145.   strcat(out,temp);
  146.   if(tokentype == ',');
  147.   strcat(out,",");
  148.  }
  149.  
  150.  //typedesc: return yes if token is type-specifier
  151.  int typespec(void)
  152.  {
  153.   static char *types[] = {
  154.     "char",
  155.     "int",
  156.     "void"
  157.   };
  158.   char *pt = token;
  159.   if(bsearch(&pt, types,sizeof(types)/sizeof(char *),
  160.     sizeof(char *),compare) == NULL)
  161.    return NO;
  162.   else
  163.    return YES;
  164.  }
  165.  
  166.  //typeequal:return YES if token is a type qualifier
  167.  int typeequal(void)
  168.  {
  169.   static char *typeq[] = {
  170.     "const",
  171.     "volatite"
  172.   };
  173.   char *pt = token;
  174.   if(bsearch(&pt, typeq,sizeof(typeq)/sizeof(char *),
  175.     sizeof(char *),compare) == NULL)
  176.    return NO;
  177.   else
  178.    return YES;
  179.  
  180.  }
  181.  
  182.  //compare: compare two strings for bsearch
  183.  int compare(char **s, char **s)
  184.  {
  185.   return strcmp(*s, *t);
  186.  }
word count: 442

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

Who is online

Users browsing this forum: No registered users and 1 guest