Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Last active November 7, 2018 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswilburn/8f3bfee4417fc6b69944 to your computer and use it in GitHub Desktop.
Save thomaswilburn/8f3bfee4417fc6b69944 to your computer and use it in GitHub Desktop.
K&R exercise 1-24
#include <stdio.h>
//I really want to use exit(), so sue me.
#include <stdlib.h>
#define true 1
#define false 0
//machine states
#define SOURCE 1 //regular code
#define COMMENT 2 //currently in a comment
#define LINE_COMMENT 3 //currently in a single-line comment
#define QUOTE 4 //currently in a quote
#define PRECOMMENT 5 //MAY BE entering a comment (encountered "/", waiting for "*")
#define POSTCOMMENT 6 //MAY BE exiting a comment (encountered "*", waiting for "/")
#define ESCAPING 7 //escape next character
//uncomment to activating debugging output
//#define debug true;
int stack[256];
int stackPosition = 0;
int states[256];
int statePosition = 0;
//manipulate the state stack
int getState() { return states[statePosition]; }
void pushState(int state) { states[++statePosition] = state; }
void popState() { statePosition--; }
//manipulate the nested syntax stack
int getSyntax() { return stack[stackPosition]; }
void pushSyntax(int c) { stack[++stackPosition] = c; }
void popSyntax() { stackPosition--; }
//Print stacks for debugging
void die() {
#ifdef debug
printf("Current syntax stack:\n");
for (int i = 0; i <= stackPosition; i++) {
putchar(stack[i]);
}
printf("\nCurrent state stack:\n");
for (int i = 0; i <= statePosition; i++) {
printf("%d", states[i]);
}
putchar('\n');
#endif
exit(0);
}
//returns -1 for error, otherwise 1
int evaluate(int c) {
int state = getState();
switch (state) {
case SOURCE:
switch (c) {
case '{':
case '(':
case '[':
pushSyntax(c);
break;
case '}':
if (getSyntax() != '{') {
printf("Unexpected }\n");
return -1;
}
popSyntax();
break;
case ')':
if (getSyntax() != '(') {
printf("Unexpected )\n");
return -1;
}
popSyntax();
break;
case ']':
if (getSyntax() != '[') {
printf("Unexpected ]\n");
return -1;
}
popSyntax();
break;
case '/':
pushState(PRECOMMENT);
break;
case '"':
case '\'':
pushSyntax(c);
pushState(QUOTE);
break;
}
break;
case PRECOMMENT:
popState();
if (c == '*') {
pushState(COMMENT); //replace PRECOMMENT with COMMENT
} else if (c == '/') {
pushState(LINE_COMMENT); //replce PRECOMMENT with LINE_COMMENT
}
//if neither matches, it's just division
break;
case POSTCOMMENT:
popState(); //remove POSTCOMMENT state
if (c == '/') {
popState(); //remove COMMENT as well
}
break;
case COMMENT:
if (c == '*') {
pushState(POSTCOMMENT);
}
break;
case LINE_COMMENT:
//end line comment at end of line
if (c == '\n') {
popState();
}
break;
case QUOTE:
if (getSyntax() == c) {
popSyntax();
popState();
}
if (c == '\\') {
pushState(ESCAPING);
}
break;
case ESCAPING:
popState(); //just skips a character, really
}
return true;
}
void main() {
stack[stackPosition] = '_'; //gotta start somewhere
states[statePosition] = SOURCE;
int c;
int pos = 1;
int line = 1;
while ((c = getchar()) != EOF) {
#ifdef debug
putchar(c);
#endif
int result = evaluate(c);
if (result == -1) {
printf("Syntax error at %d:%d\n", line, pos);
die();
}
if (c == '\n') {
pos = 1;
line++;
} else {
pos++;
}
}
//You can't have syntax or state left over
if (stackPosition > 0 || statePosition > 0) {
printf("Unexpected end of file!\n");
die();
}
//You made it!
printf("No syntax errors found!\n");
}
@thomaswilburn
Copy link
Author

Requires C99 mode if debug is defined, due to loop variable declaration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment