Skip to content

Commit

Permalink
Exerc 5_19.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsm-lisper committed Jun 21, 2024
1 parent 38ab1ac commit a3e21b0
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chapter_5.pointers_arrays/5_19.undcl_parens_fix/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BINARY=undcl_parens_fix

include ../../Exercise_incl.mk
25 changes: 25 additions & 0 deletions chapter_5.pointers_arrays/5_19.undcl_parens_fix/getch.c
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;
}
}

9 changes: 9 additions & 0 deletions chapter_5.pointers_arrays/5_19.undcl_parens_fix/getch.h
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 chapter_5.pointers_arrays/5_19.undcl_parens_fix/gettoken.c
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 chapter_5.pointers_arrays/5_19.undcl_parens_fix/gettoken.h
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
50 changes: 50 additions & 0 deletions chapter_5.pointers_arrays/5_19.undcl_parens_fix/main.c
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x () * [] * () char
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
char (*(*x())[])()

0 comments on commit a3e21b0

Please sign in to comment.