Skip to content

Commit c28fd90

Browse files
committed
added undcl
1 parent 3aa62ce commit c28fd90

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

Chapter-5/undcl/buffer.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#define BUFSIZE 100
4+
5+
int buf[BUFSIZE]; /* buffer for ungetch */
6+
int bufp = 0; /* buffer pointer (not a real pointer) */
7+
8+
int getch(void) /* get a (possibly pushed back) character */ {
9+
return (bufp > 0) ? buf[--bufp] : getchar();
10+
}
11+
12+
void ungetch(int c) /* push character back on input */ {
13+
if (bufp >= BUFSIZE)
14+
printf("ungetch : too many characters\n");
15+
else
16+
buf[bufp++] = c;
17+
}
18+
19+
void ungets(char s[]) /* push string s back to input */ {
20+
int i = strlen(s);
21+
while (i > 0) ungetch(s[--i]);
22+
}

Chapter-5/undcl/buffer.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef BUFFER_H
2+
#define BUFFER_H
3+
4+
int getch(void);
5+
6+
void ungetch(int);
7+
8+
void ungets(char []);
9+
10+
#endif

Chapter-5/undcl/main.c

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <ctype.h>
4+
5+
#define MAXTOKEN 100
6+
7+
enum { NAME, PARENS, BRACKETS };
8+
9+
void dcl(void);
10+
void dirdcl(void);
11+
12+
int gettoken(void);
13+
int tokentype; /* type of last token */
14+
char token[MAXTOKEN]; /* last token string */
15+
char name[MAXTOKEN]; /* indetifier name */
16+
char datatype[MAXTOKEN]; /* data type = char, int, etc. */
17+
char out[1024]; /* output string */
18+
19+
/* undcl: convert word description to declaration */
20+
int main() {
21+
int type;
22+
char temp[MAXTOKEN];
23+
24+
while (gettoken() != EOF) { /* 1st token on line */
25+
strcpy(out, token); /* is the datatype */
26+
while ((type = gettoken()) != '\n')
27+
if (type == PARENS || type == BRACKETS)
28+
strcat(out, token);
29+
else if (type == '*') {
30+
sprintf(temp, "(*%s)", out);
31+
strcpy(out, temp);
32+
} else if (type == NAME) {
33+
sprintf(temp, "%s %s", token, out);
34+
strcpy(out, temp);
35+
} else
36+
printf("invalid input at %s\n", token);
37+
printf("%s\n", out);
38+
}
39+
return 0;
40+
}
41+
42+
/* dcl: parse a declarator */
43+
void dcl(void) {
44+
int ns;
45+
46+
for (ns = 0; gettoken() == '*'; ) /* count *'s */
47+
ns++;
48+
dirdcl();
49+
while (ns-- > 0)
50+
strcat(out, " pointer to");
51+
}
52+
53+
/* dirdcl: parse a direct declarator */
54+
void dirdcl(void) {
55+
int type;
56+
57+
if (tokentype == '(') { /* ( dcl ) */
58+
dcl();
59+
if (tokentype != ')')
60+
printf("error: missing )\n");
61+
} else if (tokentype == NAME) /* variable name */
62+
strcpy(name, token);
63+
else
64+
printf("error: expected name or (dcl)\n");
65+
while ((type = gettoken()) == PARENS || type == BRACKETS)
66+
if (type == PARENS)
67+
strcat(out, " function returning");
68+
else {
69+
strcat(out, " array");
70+
strcat(out, token);
71+
strcat(out, " of");
72+
}
73+
}
74+
75+
int gettoken(void) /* return next token */ {
76+
int c, getch(void);
77+
void ungetch(int);
78+
char *p = token;
79+
80+
while((c = getch()) == ' ' || c == '\t')
81+
;
82+
if (c == '(') {
83+
if ((c = getch()) == ')') {
84+
strcpy(token, "()");
85+
return tokentype = PARENS;
86+
} else {
87+
ungetch(c);
88+
return tokentype = '(';
89+
}
90+
} else if (c == '[') {
91+
for (*p++ = c; (*p++ = getch()) != ']'; )
92+
;
93+
*p = '\0';
94+
return tokentype = BRACKETS;
95+
} else if (isalpha(c)) {
96+
for (*p++ = c; isalnum(c = getch()); )
97+
*p++ = c;
98+
*p = '\0';
99+
ungetch(c);
100+
return tokentype = NAME;
101+
} else
102+
return tokentype = c;
103+
}

0 commit comments

Comments
 (0)