声明转文字: 编写程序将C语言的声明转换为文字描述
一. 程序功能 int flag; printf("Please input(ctrl+z to quit)n"); while (gettoken() != EOF) { strcpy(datatype, token); out[0] = ; flag = dcl(); if (tokentype != n) printf("syntax errorn"); if (flag == 1 && tokentype == n) printf("%s: %s %sn", name, out, datatype); } system("pause"); return 0; 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 = ; return tokentype = BRACKETS; } else if (isalpha(c)) { for (*p++ = c; isalnum(c = n_getch());) *p++ = c; *p = ; n_ungetch(c); return tokentype = NAME; } else return tokentype = c; return (bufp > 0) ? buf[--bufp]: getchar(); if (bufp >= BUFSIZE) printf("new_ungetch: too many characters!n"); else buf[bufp++] = c; int ns, flag; for (ns = 0; gettoken() == *;) ns++; flag = dirdcl(); while (ns-- > 0) strcat(out, " pointer to"); return flag; int type, flag = 1; if (tokentype == () { flag = dcl(); if (tokentype != )) { errmsg("error: missing )n"); flag = 0; } } else if (tokentype == NAME) { if (name[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; printf("%s", msg); prevtoken = YES; do { dclspec(); } while (tokentype == ,); if (tokentype != )) errmsg("missing ) in parameter declarationn"); char temp[MAXTOKEN]; temp[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 listn"); } while (tokentype != , && tokentype != )); strcat(out, temp); if (tokentype == ,) strcat(out, ","); 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; 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; return strcmp(*s, *t);
编写程序将C语言的声明转换为文字描述
比如, 输入char **argv,
会打印输出: argv: pointer to pointer to char
二. 程序源码
//main.c
#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)
{
}
//gettoken.c
#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)
{
}
char buf[BUFSIZE];
int bufp = 0;
int n_getch(void)
{
}
void n_ungetch(int c)
{
}
//dcl.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)
{
}
//dirdcl: parse a direct declaration
int dirdcl(void)
{
}
//errmsg: print error message and indicate avail. token
void errmsg(char *msg)
{
}
//parmdcl.c
#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)
{
}
void dclspec(void)
{
}
int typespec(void)
{
}
int typequal(void)
{
}
int compare(char **s, char **t)
{
}
三.程序小结
1. 程序缺陷当输入 int a(
输入不了了 为什么呢?
关键词: 声明转文字编写程序C语言文字描

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW
或用微信扫描左侧二维码