Page 126

Make dcl recover from input errors.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAXTOKEN 100

/* The task is to 'Make dcl recover from input errors'. I interpret this to mean that a
 * best attempt should be printed in the following cases:
 * - missing opening/closing PARENS/BRACKETS
 * - missing NAME
 * The existing behaviour will tend to assume that the input is just incomplete, and
 * wait for further input and so not print anything, and potentially cause problems for
 * subsequent expressions.
 * Tested with `char (*(*x())[])()` by removing parens, brackets, name.
 * It is accepted that this error handling will produce spurious results as it is not
 * within the scope of this task to suggest corrections. 
 * char (*(*x())])() */

enum { NAME = 1, PARENS, BRACKETS };

void dcl(void);
void dirdcl(void);

int gettoken(void);
int tokentype;
char token[MAXTOKEN];
char name[MAXTOKEN];
char datatype[MAXTOKEN];
char out[1000];
bool missing_paren = false;

int main()
{
	while (gettoken() != EOF) {
		strcpy(datatype, token);
		out[0] = '\0';
		dcl();
		if (tokentype != '\n') {
			printf("syntax error\n");
		}
		printf("%s: %s %s\n", name, out, datatype);
	}
	return 0;
}

void dcl(void)
{
	int ns;

	for (ns = 0; gettoken() == '*';) {
		ns++;
	}
	dirdcl();
	while (ns-- > 0)
		strcat(out, " pointer to");
}

void dirdcl(void)
{
	int type;

	if (tokentype == '(') {
		dcl();
		if (tokentype != ')') {
			printf("error: missing )\n");
			missing_paren = true;
		}
	} else if (tokentype == NAME) {
		strcpy(name, token);
	} else {
		printf("error: expected name or (dcl)\n");
	}
	while ((type = gettoken()) == PARENS || type == BRACKETS)
		if (type == PARENS) {
			strcat(out, " function returning");
		} else {
			strcat(out, " array");
			strcat(out, token);
			strcat(out, " of");
		}
}

int gettoken(void)
{
	int c, getch(void);
	void ungetch(int);
	char *p = token;

	if (missing_paren) {
		tokentype = ')';
		missing_paren = false;
		return tokentype;
	}

	while ((c = getch()) == ' ' || c == '\t')
		;
	if (c == '(') {
		if ((c = getch()) == ')') {
			strcpy(token, "()");
			return tokentype = PARENS;
		} else {
			ungetch(c);
			return tokentype = '(';
		}
	} else if (c == '[') {
		/* Support alphanum and underscore inside brackets */
		for (*p = c; isalnum(*(++p) = getch()) || *p == '_';)
			;
		/* Recover if end bracket missing */
		if (*p != ']') {
			ungetch(*p);
			*p = ']';
		}
		*(++p) = '\0';
		return tokentype = BRACKETS;
	} else if (c == ']') {
		*p++ = '[';
		*p++ = ']';
		*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;
	}
}

#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;

int getch(void)
{
	return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c)
{
	if (bufp >= BUFSIZE) {
		printf("ungetch: too many characters\n");
	} else {
		buf[bufp++] = c;
	}
}