-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38ab1ac
commit a3e21b0
Showing
8 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
BINARY=undcl_parens_fix | ||
|
||
include ../../Exercise_incl.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# include <stdio.h> | ||
|
||
# define BUFSIZE 128 | ||
|
||
int buf[BUFSIZE]; /* buffer for ungetch */ | ||
int bufp = 0; /* next free position in buf */ | ||
|
||
|
||
/* getch: get a (possibly pushed-back) character */ | ||
int getch (void) | ||
{ | ||
return (bufp > 0) ? buf[--bufp] : getchar(); | ||
} | ||
|
||
|
||
/* ungetch: push character back on input */ | ||
void ungetch (int c) | ||
{ | ||
if (bufp >= BUFSIZE) { | ||
printf("ungetch: too many characters\n"); | ||
} else { | ||
buf[bufp++] = c; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# ifndef GETCH_H | ||
# define GETCH_H | ||
|
||
/* getch: get a (possibly pushed-back) character */ | ||
int getch (void); | ||
/* ungetch: push character back on input */ | ||
void ungetch (int c); | ||
|
||
# endif |
52 changes: 52 additions & 0 deletions
52
chapter_5.pointers_arrays/5_19.undcl_parens_fix/gettoken.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# include <string.h> | ||
# include <ctype.h> | ||
|
||
# include "gettoken.h" | ||
# include "getch.h" | ||
|
||
int tokentype; /* type of last token */ | ||
char token[MAXTOKEN]; /* last token string */ | ||
|
||
/* ignspaces: ignore white space characters (' ', '\t') */ | ||
void ignspaces (void) | ||
{ | ||
int c; | ||
while ((c = getch()) == ' ' || c == '\t') | ||
; | ||
ungetch(c); | ||
} | ||
|
||
/* gettoken: return next token */ | ||
int gettoken (void) | ||
{ | ||
int c; | ||
char *p = token; | ||
|
||
ignspaces(); | ||
if ((c = getch()) == '(') { | ||
ignspaces(); | ||
if ((c = getch()) == ')') { | ||
strcpy(token, "()"); | ||
return tokentype = PARENS; | ||
} | ||
else { | ||
ungetch(c); | ||
return tokentype = '('; | ||
} | ||
} | ||
else if (c == '[') { | ||
for (*p++ = c; (*p++ = getch()) != ']'; ) | ||
; | ||
*p = '\0'; | ||
return tokentype = BRACKETS; | ||
} | ||
else if (isalpha(c)) { | ||
for (*p++ = c; isalnum(c = getch()); ) | ||
*p++ = c; | ||
*p = '\0'; | ||
ungetch(c); | ||
return tokentype = NAME; | ||
} | ||
else | ||
return tokentype = c; | ||
} |
14 changes: 14 additions & 0 deletions
14
chapter_5.pointers_arrays/5_19.undcl_parens_fix/gettoken.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# ifndef GETTOKEN_H | ||
# define GETTOKEN_H | ||
|
||
# define MAXTOKEN 1024 | ||
|
||
enum {NAME, PARENS, BRACKETS}; | ||
|
||
extern int tokentype; /* type of last token */ | ||
extern char token[MAXTOKEN]; /* last token string */ | ||
|
||
/* gettoken: return next token */ | ||
int gettoken (void); | ||
|
||
# endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# include <stdio.h> | ||
# include <string.h> | ||
|
||
# include "gettoken.h" | ||
|
||
# define MAXOUT 1024 | ||
|
||
char out[MAXOUT]; | ||
|
||
/* undcl: convert word descriptions to declarations */ | ||
int main () | ||
{ | ||
int type; | ||
char temp[MAXTOKEN]; | ||
|
||
while (gettoken() != EOF) { | ||
strcpy(out, token); | ||
while ((type = gettoken()) != '\n') | ||
if (type == PARENS || type == BRACKETS) | ||
strcat(out, token); | ||
else if (type == '*') { | ||
if (strlen(out)+1+3 > sizeof(temp)) | ||
strcat(out, "internal error #0"); | ||
else { | ||
strcpy(temp, "(*"); | ||
strcat(temp, out); | ||
strcat(temp, ")"); | ||
strcpy(out, temp); | ||
} | ||
} | ||
else if (type == NAME) { | ||
if (strlen(temp)+1 > sizeof(temp)) { | ||
strcat(out, "internal error #1"); | ||
} | ||
else if (strlen(out) + strlen(token) + 1 > sizeof(temp)) { | ||
strcat(out, "internal error #2"); | ||
} | ||
else { | ||
strcpy(temp, token); | ||
strcat(temp, " "); | ||
strcat(temp, out); | ||
strcpy(out, temp); | ||
} | ||
} | ||
else | ||
printf("invalid input at %s\n", token); | ||
printf("%s\n", out); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x () * [] * () char |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
char (*(*x())[])() |