Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #define MAXTOKEN 100
- #define BUFSIZE 100
- enum {NAME, PARENS, BRACKETS};
- enum {NO, YES};
- void dcl(void); //parse a declarator
- void dirdcl(void); //parse a direct declarator
- int gettoken(void);
- void paramdcl(void); //declarator analysis with parameters
- void declspec(void); //specification declaration
- int typespec(void); //type specifier
- int typedesc(void); //descriptor type
- int getch(void); //get a (possibly pushed-back) character
- void ungetch(int c); //push character back on input
- void errmesg(char *msg); //error message
- int tokentype; //type of last token
- char token[MAXTOKEN]; //last token string
- char name[MAXTOKEN]; //identifier name
- char datatype[MAXTOKEN]; //data type = char, int, etc.
- char buf[BUFSIZE]; //buffer for ungetch;
- char out[1000];
- int errtoken = NO;
- int errbrack = NO;
- /* convert declaration to words */
- int main()
- {
- while(gettoken() != EOF) //1st token on line
- {
- strcpy(datatype, token); //is the datatype
- out[0] = '\0';
- dcl(); //parse rest of line
- if(tokentype != '\n')
- printf("syntax error\n");
- printf("%s: %s %s\n", name, out, datatype);
- }
- return 0;
- }
- /* dcl: parse a declarator */
- void dcl(void)
- {
- int ns;
- for(ns = 0; gettoken() == '*';) //count stars
- ns++;
- dirdcl();
- while(ns-- > 0)
- strcat(out, " pointer to");
- }
- /* dirdcl: parse a direct declarator */
- void dirdcl(void)
- {
- int type;
- if(tokentype == '(') //(dcl)
- {
- dcl();
- if(tokentype != ')')
- errmesg("error: missing )\n");
- }
- else if(tokentype == NAME) //variable name
- {
- if(name[0] == '\0')
- strcpy(name, token);
- }
- else
- errtoken = YES;
- while((type = gettoken()) == PARENS || type == BRACKETS || type == '(')
- if(type == PARENS)
- strcat(out, " function returning");
- else if(type == '(')
- {
- strcat(out, " function taking");
- paramdcl();
- strcat(out, " and returning");
- }
- else
- {
- strcat(out, " array");
- strcat(out, token);
- strcat(out, " of");
- }
- }
- /* errmesg: error message */
- void errmesg(char *msg)
- {
- printf("%s", msg);
- errtoken = YES;
- }
- /* paramdcl: declarator analysis with parameters */
- void paramdcl(void)
- {
- do
- {
- declspec(); //specification declaration
- } while(tokentype == ',');
- if(tokentype != ')')
- errmesg("missing ) in declarator of parameters\n");
- }
- /* specification declaration */
- void declspec(void)
- {
- char temp[MAXTOKEN];
- temp[0] = '\0';
- gettoken();
- do
- {
- if(tokentype != NAME)
- {
- errtoken = YES;
- dcl();
- }
- else if(typespec() == YES) // type specifier
- {
- strcat(temp, " ");
- strcat(temp, token);
- gettoken();
- }
- else if(typedesc() == YES) //descriptor type
- {
- strcat(temp, " ");
- strcat(temp, token);
- gettoken();
- }
- else
- errmesg("error: unknown type in the parameter list\n");
- } while(tokentype != ',' && tokentype != ')');
- strcat(out, temp);
- if(tokentype == ',')
- strcat(out, ",");
- }
- /* type specifier */
- int typespec(void)
- {
- static char *types[] = {"char", "int", "void"};
- char *ptypes = token;
- int result, i;
- result = NO;
- for(i = 0; i < 3; i++)
- if(strcmp(ptypes, *(types + i)) == 0)
- result = YES;
- else
- result = NO;
- return result;
- }
- /* descriptor type */
- int typedesc(void)
- {
- static char *typed[] = {"const", "volatile"};
- char *ptd = token;
- int result, i;
- result = NO;
- for(i = 0; i < 2; i++)
- if(strcmp(ptd, *(typed + i)) == 0)
- result = YES;
- else
- result = NO;
- return result;
- }
- int gettoken(void)
- {
- int c;
- char *p = token;
- if(errtoken == YES)
- {
- errtoken = NO;
- return tokentype;
- }
- while((c = getch()) == ' ' || c == '\t')
- ;
- if(c == '(')
- {
- if((c = getch()) == ')')
- {
- strcpy(token, "()");
- return tokentype = PARENS;
- }
- else
- {
- ungetch(c);
- return tokentype = '(';
- }
- }
- else if(c == '[')
- {
- for(*p++ = c; *p != ']';)
- {
- *p = getch();
- if(*p != ']')
- {
- if(*p == '\n' || *p == ')' || *p == '(')
- {
- printf("error: missing ]\n");
- ungetch(*p);
- *p = ']';
- }
- else
- p++;
- }
- }
- *++p = '\0';
- return tokentype = BRACKETS;
- }
- else if(isalpha(c))
- {
- for(*p++ = c; isalnum(c = getch());)
- *p++ = c;
- *p = '\0';
- ungetch(c);
- return tokentype = NAME;
- }
- else
- return tokentype = c;
- }
- int bufp = 0;
- int getch(void) // get a (possibly pushed-back) character
- {
- int bufp = 0; //next free position in buf
- return (bufp > 0) ? buf[--bufp] : getchar();
- }
- void ungetch(int c) // push character back on input
- {
- if(bufp >= BUFSIZE)
- printf("ungetch: too many characnters\n");
- else
- buf[bufp++] = c;
- }
Advertisement
Add Comment
Please, Sign In to add comment