Advertisement
naorzr

Untitled

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