From 07a42d77a1c26e226fec3be7645ba37e8ceafe6e Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sun, 23 Apr 2023 17:42:57 +0530 Subject: 5.20: v2, get rid of unnecessary recursion --- 5.20/dcl.c | 34 ++++++++++++++++------------------ 5.20/main.c | 2 +- 5.20/token.c | 10 ++++------ 5.20/token.h | 2 +- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/5.20/dcl.c b/5.20/dcl.c index e504919..085c424 100644 --- a/5.20/dcl.c +++ b/5.20/dcl.c @@ -26,47 +26,45 @@ void dirdcl(void) dcl(); if (tokentype != ')') warn("missing )"); - } else if (tokentype == ')' || tokentype == ',') { + } else if (tokentype == ')') { prevtoken = YES; } else if (tokentype == NAME) { - strcpy(name, token); + if (!strlen(name)) + strcpy(name, token); } else { + printf("%c - %d : ", tokentype, tokentype); warn("expected () or name or (dcl)"); } while ((type = gettoken()) == PARENS || type == BRACKETS || - type == '(' || type == ',') { + type == '(') { if (type == PARENS) { strncat(out, " function returning", OUTLEN - 1); } else if (type == '(') { - strncat(out, " function expecting", OUTLEN - 1); + strncat(out, "function expecting", OUTLEN - 1); parmdcl(); strncat(out, " and returning", OUTLEN - 1); - } else if (type == ',') { - parmdcl(); - strncat(out, ",", OUTLEN - 1); } else { strncat(out, " array", OUTLEN - 1); strncat(out, token, OUTLEN - 1); strncat(out, " of", OUTLEN - 1); } } - - if (tokentype == '\n') - prevtoken = YES; } void parmdcl(void) { char parmdt[MAXTOKEN]; - parmdt[0] = '\0'; - while (gettoken() == NAME && isdatatyp()) { - strncat(parmdt, " ", MAXTOKEN - 1); - strncat(parmdt, token, MAXTOKEN - 1); - } + do { + strncpy(parmdt, (tokentype == ',') ? "," : "", MAXTOKEN - 1); + while (gettoken() == NAME && isdtspec()) { + strncat(parmdt, " ", MAXTOKEN - 1); + strncat(parmdt, token, MAXTOKEN - 1); + } - prevtoken = YES; - dcl(); - strncat(out, parmdt, OUTLEN - 1); + prevtoken = YES; + dcl(); + strncat(out, parmdt, OUTLEN - 1); + } while (tokentype == ','); } diff --git a/5.20/main.c b/5.20/main.c index 6338524..689c229 100644 --- a/5.20/main.c +++ b/5.20/main.c @@ -16,7 +16,7 @@ int main(void) do { strcat(datatype, token); strcat(datatype, " "); - } while (gettoken() == NAME && isdatatyp()); + } while (gettoken() == NAME && isdtspec()); prevtoken = YES; if (tokentype == EOF) diff --git a/5.20/token.c b/5.20/token.c index 0a69b09..b04acef 100644 --- a/5.20/token.c +++ b/5.20/token.c @@ -22,7 +22,7 @@ char *storspec[] = { "static" }; -char *datatyp[] = { +char *typespec[] = { "char", "int", "long", @@ -36,8 +36,6 @@ char *datatyp[] = { int gettoken(void) { char c, *p = token; - char getch(void); - void ungetch(char); if (prevtoken == YES) { prevtoken = NO; @@ -97,7 +95,7 @@ int peaktoken() return type; } -int isdatatyp(void) +int isdtspec(void) { int i, len; @@ -109,8 +107,8 @@ int isdatatyp(void) if(!strcmp(token, storspec[i])) return 1; - for (i = 0, len = ARR_SIZE(datatyp); i < len; ++i) - if(!strcmp(token, datatyp[i])) + for (i = 0, len = ARR_SIZE(typespec); i < len; ++i) + if(!strcmp(token, typespec[i])) return 1; return 0; diff --git a/5.20/token.h b/5.20/token.h index 327b1e7..7e4fbec 100644 --- a/5.20/token.h +++ b/5.20/token.h @@ -7,7 +7,7 @@ enum { NO, YES }; int gettoken(void); int peaktoken(void); -int isdatatyp(void); +int isdtspec(void); extern char tokentype; extern char token[MAXTOKEN]; -- cgit v1.2.3