>>1I was working on a simple parser example a while ago that's quite inefficient but might be something to work from.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
enum EMaybe{
Nothing,
Just,
};
struct Maybe{
enum EMaybe maybe;
union{
int *ip; int i;
float *fp; float f;
char *cp; char c; bool b;
}result;
size_t length;
};
struct Maybe character(char wanted, char c){
struct Maybe val = {Nothing, 0};
if (c == wanted){
val.maybe = Just;
val.length = 1;
val.result.c = c;
}
return val;
}
struct Maybe many(struct Maybe (fn)(char), char *text){
struct Maybe val = {Nothing, 0, 0};
size_t index;
for (index = 0; (fn(text[index])).maybe == Just; index++);
val.maybe = Just;
val.length = index;
val.result.cp = text;
return val;
}
struct Maybe upper(char l){
struct Maybe val = {Nothing, 0};
if (l > 64 && l < 91){
val.maybe = Just;
val.length = 1;
val.result.c = l;
}
return val;
}
struct Maybe lower(char l){
struct Maybe val = {Nothing, 0};
if (l > 96 && l < 124){
val.maybe = Just;
val.length = 1;
val.result.c = l;
}
return val;
}
struct Maybe letter(char l){
struct Maybe val;
val = upper(l);
if (val.maybe == Just) return val;
val = lower(l);
if (val.maybe == Just) return val;
val.maybe = Nothing;
val.length = 1;
val.result.c = 0;
return val;
}
struct Maybe digit(char d){
struct Maybe val = {Nothing, 0};
if (d > 47 && d < 58){
val.maybe = Just;
val.length = 1;
val.result.c = d;
}
return val;
}
struct Maybe oneOf(char *string, size_t length, char c){
struct Maybe val = {Nothing, 0};
size_t index;
for (index = 0; index < length; index++){
if (string[index] == c){
val.maybe = Just;
val.length = 1;
val.result.c = c;
return val;
}
}
return val;
}
#define letters(x) many(letter, x)
#define digits(x) many(digit, x)
#define spaces(x) many(space, x)
#define whitespace(x) oneOf(" \t\n", 3, x)
#define space(x) character(' ', x)
#define eof(x) character('\0', x)
#define newline(x) character('\n', x)
#define tab(x) ch
Post too long. Click here to view the full text.