Created
December 22, 2018 07:32
-
-
Save carld/e1d0f7d24b81ba86d1fd41b82473b328 to your computer and use it in GitHub Desktop.
mini parser combinators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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