Skip to content

Commit 3aa62ce

Browse files
committed
added Exercise 5-18
1 parent 11652b4 commit 3aa62ce

File tree

6 files changed

+249
-0
lines changed

6 files changed

+249
-0
lines changed

Chapter-5/Exercise 5-18/buffer.c

Lines changed: 22 additions & 0 deletions
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/Exercise 5-18/buffer.h

Lines changed: 10 additions & 0 deletions
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/Exercise 5-18/main.c

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

Chapter-5/dcl/buffer.c

Lines changed: 22 additions & 0 deletions
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/dcl/buffer.h

Lines changed: 10 additions & 0 deletions
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/dcl/main.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
int main() {
20+
while (gettoken() != EOF) { /* 1st token on line */
21+
strcpy(datatype, token); /* is the datatype */
22+
out[0] = '\0';
23+
dcl(); /* parse rest of the line */
24+
if (tokentype != '\n')
25+
printf("syntax error\n");
26+
printf("%s: %s %s\n", name, out, datatype);
27+
}
28+
return 0;
29+
}
30+
31+
/* dcl: parse a declarator */
32+
void dcl(void) {
33+
int ns;
34+
35+
for (ns = 0; gettoken() == '*'; ) /* count *'s */
36+
ns++;
37+
dirdcl();
38+
while (ns-- > 0)
39+
strcat(out, " pointer to");
40+
}
41+
42+
/* dirdcl: parse a direct declarator */
43+
void dirdcl(void) {
44+
int type;
45+
46+
if (tokentype == '(') { /* ( dcl ) */
47+
dcl();
48+
if (tokentype != ')')
49+
printf("error: missing )\n");
50+
} else if (tokentype == NAME) /* variable name */
51+
strcpy(name, token);
52+
else
53+
printf("error: expected name or (dcl)\n");
54+
while ((type = gettoken()) == PARENS || type == BRACKETS)
55+
if (type == PARENS)
56+
strcat(out, " function returning");
57+
else {
58+
strcat(out, " array");
59+
strcat(out, token);
60+
strcat(out, " of");
61+
}
62+
}
63+
64+
int gettoken(void) /* return next token */ {
65+
int c, getch(void);
66+
void ungetch(int);
67+
char *p = token;
68+
69+
while((c = getch()) == ' ' || c == '\t')
70+
;
71+
if (c == '(') {
72+
if ((c = getch()) == ')') {
73+
strcpy(token, "()");
74+
return tokentype = PARENS;
75+
} else {
76+
ungetch(c);
77+
return tokentype = '(';
78+
}
79+
} else if (c == '[') {
80+
for (*p++ = c; (*p++ = getch()) != ']'; )
81+
;
82+
*p = '\0';
83+
return tokentype = BRACKETS;
84+
} else if (isalpha(c)) {
85+
for (*p++ = c; isalnum(c = getch()); )
86+
*p++ = c;
87+
*p = '\0';
88+
ungetch(c);
89+
return tokentype = NAME;
90+
} else
91+
return tokentype = c;
92+
}

0 commit comments

Comments
 (0)