您的位置 首页 制造

声明转文字: 编写程序将C言语的声明转换为文字描述

一程序功能编写程序将C语言的声明转换为文字描述比如,输入char**argv,会打印输出:argv:pointertopointertochar二程

一. 程序功用

编写程序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)
{
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;
}
//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)
{
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;
}
//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)
{
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;
}
//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)
{
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. 程序缺点

当输入 int a(
输入不了了 为什么呢?

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/zhizao/262291.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部