一. 程序功能
二. 程序源码
#include
#include
#include
#define MAXTOKEN 100
enum {NAME, PARENS, BRACKETS};
int dcl(void);
int dirdcl(void);
int gettoken(void);
int tokentype;
char token[MAXTOKEN];
char name[MAXTOKEN];
char datatype[MAXTOKEN];
char out[1000];
int main(void)
{
int flag;
printf("Please input(ctrl+z to quit)\n");
while (gettoken() != EOF) {
strcpy(datatype, token);
out[0] = '\0';
flag = dcl();
if (tokentype != '\n')
printf("syntax error\n");
if (flag == 1 && tokentype == '\n')
printf("%s: %s %s\n", name, out, datatype);
}
system("pause");
return 0;
}
#include
#include
#include
#define BUFSIZE 100
enum {NAME, PARENS, BRACKETS};
enum {NO, YES};
extern int tokentype;
extern char token[];
int prevtoken = NO;
int n_getch(void);
void n_ungetch(int);
int gettoken(void)
{
int c;
char *p = token;
if (prevtoken == YES) {
prevtoken = NO;
return tokentype;
}
while ((c = n_getch()) == ' ' || c == '\t')
;
if (c == '(') {
if ((c = n_getch()) == ')') {
strcpy(token, "()");
return tokentype = PARENS;
} else {
n_ungetch(c);
return tokentype = '(';
}
} else if (c == '[') {
for (*p++ = c; (*p++ = n_getch()) != ']';)
;
*p = '\0';
return tokentype = BRACKETS;
} else if (isalpha(c)) {
for (*p++ = c; isalnum(c = n_getch());)
*p++ = c;
*p = '\0';
n_ungetch(c);
return tokentype = NAME;
} else
return tokentype = c;
}
char buf[BUFSIZE];
int bufp = 0;
int n_getch(void)
{
return (bufp > 0) ? buf[--bufp]: getchar();
}
void n_ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("new_ungetch: too many characters!\n");
else
buf[bufp++] = c;
}
#include
#include
#include
enum {NAME, PARENS, BRACKETS};
enum {NO, YES};
int dcl(void);
int dirdcl(void);
void errmsg(char *);
int gettoken(void);
void parmdcl(void);
extern int tokentype;
extern char token[];
extern char name[];
extern char datatype[];
extern char out[];
extern int prevtoken;
//dcl: parse a declarator
int dcl(void)
{
int ns, flag;
for (ns = 0; gettoken() == '*';)
ns++;
flag = dirdcl();
while (ns-- > 0)
strcat(out, " pointer to");
return flag;
}
//dirdcl: parse a direct declaration
int dirdcl(void)
{
int type, flag = 1;
if (tokentype == '(') {
flag = dcl();
if (tokentype != ')')
{
errmsg("error: missing )\n");
flag = 0;
}
} else if (tokentype == NAME) {
if (name[0] == '\0')
strcpy(name, token);
} else {
prevtoken = YES;
flag = 0;
}
while ((type = gettoken()) == PARENS || type == BRACKETS || type == '(')
{
if (type == PARENS)
strcat(out, " function returning");
else if (type == '(') {
strcat(out, " function expecting");
parmdcl();
strcat(out, " and returning");
} else {
strcat(out, " array");
strcat(out, token);
strcat(out, " of");
}
}
return flag;
}
//errmsg: print error message and indicate avail. token
void errmsg(char *msg)
{
printf("%s", msg);
prevtoken = YES;
}
#include
#include
#include
#include
#define MAXTOKEN 100
enum {NAME, PARENS, BRACKETS};
enum {NO, YES};
void dcl(void);
void errmsg(char *);
void dclspec(void);
int typespec(void);
int typequal(void);
int compare(char **, char **);
int gettoken(void);
extern int tokentype;
extern char token[];
extern char name[];
extern char datatype[];
extern char out[];
extern int prevtoken;
void parmdcl(void)
{
do {
dclspec();
} while (tokentype == ',');
if (tokentype != ')')
errmsg("missing ) in parameter declaration\n");
}
void dclspec(void)
{
char temp[MAXTOKEN];
temp[0] = '\0';
gettoken();
do {
if (tokentype != NAME) {
prevtoken = YES;
dcl();
} else if (typespec() == YES) {
strcat(temp, " ");
strcat(temp, token);
gettoken();
} else if (typequal() == YES) {
strcat(temp, " ");
strcat(temp, token);
gettoken();
} else
errmsg("unknown type in parameter list\n");
} while (tokentype != ',' && tokentype != ')');
strcat(out, temp);
if (tokentype == ',')
strcat(out, ",");
}
int typespec(void)
{
static char *types[] = {"char", "int", "void"};
char *pt = token;
if (bsearch(&pt, types, sizeof(types)/sizeof(char *), sizeof(char *), compare) == NULL)
return NO;
else
return YES;
}
int typequal(void)
{
static char *typeq[] = {"const", "volatile"};
char *pt = token;
if (bsearch(&pt, typeq, sizeof(typeq)/sizeof(char *), sizeof(char *), compare) == NULL)
return NO;
else
return YES;
}
int compare(char **s, char **t)
{
return strcmp(*s, *t);
}
1. 程序缺陷