微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 声明转文字: 编写程序将C语言的声明转换为文字描述

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

时间:11-22 来源:互联网 点击:
一. 程序功能

编写程序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];

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top