Skip to content

Instantly share code, notes, and snippets.

@carld
Created December 22, 2018 07:32
Show Gist options
  • Select an option

  • Save carld/e1d0f7d24b81ba86d1fd41b82473b328 to your computer and use it in GitHub Desktop.

Select an option

Save carld/e1d0f7d24b81ba86d1fd41b82473b328 to your computer and use it in GitHub Desktop.
mini parser combinators
#include<stdio.h>
int lookahead = -1;
void getchar_skipwhitespace() { while( (lookahead = getchar()) == ' ') ; }
int success() { getchar_skipwhitespace(); return 1; }
int failure() { return 0; }
int lparen() { return lookahead == '(' ? success() : failure(); }
int rparen() { return lookahead == ')' ? success() : failure(); }
int sym() { return lookahead >= 'a' && lookahead <= 'z' ? success() : failure(); }
int star( int (*a) (void) ) { return a() ? star(a) : 1; }
int main(int argc, char *argv[]) {
int result = 0;
getchar_skipwhitespace();
result = sym() || (lparen() && star(sym) && rparen()) ;
printf("result: %d\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment