Skip to content

Commit e328b41

Browse files
committed
done with pointers
Exercise 5-20 is still partial, doesn't handle the arguments well, and doesn't support qualifiers .
1 parent b4b84f5 commit e328b41

File tree

9 files changed

+441
-0
lines changed

9 files changed

+441
-0
lines changed

Chapter-5/Exercise 5-19/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/Exercise 5-19/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/Exercise 5-19/main.c

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

Chapter-5/Exercise 5-20/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/Exercise 5-20/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/Exercise 5-20/main.c

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
enum { NO, YES };
10+
11+
void dcl(void);
12+
void dirdcl(void);
13+
void prmdcl(void);
14+
15+
int gettoken(void);
16+
int tokentype; /* type of last token */
17+
char token[MAXTOKEN]; /* last token string */
18+
char name[MAXTOKEN]; /* indetifier name */
19+
char datatype[MAXTOKEN]; /* data type = char, int, etc. */
20+
char out[1024]; /* output string */
21+
int prevtoken = NO; /* to indicate if there was a previous token */
22+
23+
int main() {
24+
while (gettoken() != EOF) { /* 1st token on line */
25+
strcpy(datatype, token); /* is the datatype */
26+
out[0] = '\0';
27+
dcl(); /* parse rest of the line */
28+
if (tokentype != '\n')
29+
printf("syntax error\n");
30+
printf("%s: %s %s\n", name, out, datatype);
31+
}
32+
return 0;
33+
}
34+
35+
/* dcl: parse a declarator */
36+
void dcl(void) {
37+
int ns;
38+
39+
for (ns = 0; gettoken() == '*'; ) /* count *'s */
40+
ns++;
41+
dirdcl();
42+
while (ns-- > 0)
43+
strcat(out, " pointer to");
44+
}
45+
46+
/* dirdcl: parse a direct declarator */
47+
void dirdcl(void) {
48+
int type;
49+
50+
if (tokentype == '(') { /* ( dcl ) */
51+
dcl();
52+
if (tokentype != ')')
53+
printf("error: missing )\n");
54+
} else if (tokentype == NAME) /* variable name */
55+
strcpy(name, token);
56+
else {
57+
//printf("error: expected name or (dcl)\n");
58+
prevtoken = YES;
59+
}
60+
while ((type = gettoken()) == PARENS || type == BRACKETS || type == '(')
61+
if (type == PARENS)
62+
strcat(out, " function returning");
63+
else if (type == '(') {
64+
strcat(out, " function expecting");
65+
prmdcl();
66+
strcat(out, " and returning");
67+
}
68+
else {
69+
strcat(out, " array");
70+
strcat(out, token);
71+
strcat(out, " of");
72+
}
73+
}
74+
75+
/* prmdcl: parse the parameters to a function */
76+
void prmdcl(void) {
77+
int type;
78+
while ((type = gettoken()) != ')') {
79+
if (type != NAME && type != ',') {
80+
prevtoken = YES;
81+
dcl();
82+
}
83+
else if (type == ',')
84+
strcat(out, ",");
85+
else {
86+
strcat(out, " ");
87+
strcat(out, token);
88+
}
89+
}
90+
}
91+
92+
int gettoken(void) /* return next token */ {
93+
int c, getch(void);
94+
void ungetch(int);
95+
char *p = token;
96+
97+
if (prevtoken == YES) {
98+
prevtoken = NO;
99+
return tokentype;
100+
}
101+
102+
while((c = getch()) == ' ' || c == '\t')
103+
;
104+
if (c == '(') {
105+
if ((c = getch()) == ')') {
106+
strcpy(token, "()");
107+
return tokentype = PARENS;
108+
} else {
109+
ungetch(c);
110+
return tokentype = '(';
111+
}
112+
} else if (c == '[') {
113+
for (*p++ = c; (*p++ = getch()) != ']'; )
114+
;
115+
*p = '\0';
116+
return tokentype = BRACKETS;
117+
} else if (isalpha(c)) {
118+
for (*p++ = c; isalnum(c = getch()); )
119+
*p++ = c;
120+
*p = '\0';
121+
ungetch(c);
122+
return tokentype = NAME;
123+
} else
124+
return tokentype = c;
125+
}

Chapter-5/practice/dcl/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/practice/dcl/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

0 commit comments

Comments
 (0)