Mercurial > lbo > hg > ylisp
changeset 0:a369de4d175f
Add very very basic parser.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 17 Aug 2019 21:40:41 +0200 |
parents | |
children | 648bf38fd86d |
files | gen/debug_parse.c gen/y.yy |
diffstat | 2 files changed, 67 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gen/debug_parse.c Sat Aug 17 21:40:41 2019 +0200 @@ -0,0 +1,8 @@ +#include <stdio.h> + +extern int parse(FILE* in); + +int main(void) { + parse(stdin); + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gen/y.yy Sat Aug 17 21:40:41 2019 +0200 @@ -0,0 +1,59 @@ + +%pointer + +%top { +#include <stdio.h> +} + +%{ + +int yywrap() { return 1; } + +typedef enum { LEFT, RIGHT } Paren; + +void paren(Paren p) { + if (p == LEFT) printf("left paren!\n"); + if (p == RIGHT) printf("right paren!\n"); +} + +void atom(const char* txt) { + printf("atom: %s\n", txt); +} + +void number(const char* txt) { + printf("number: %s\n", txt); +} + +%} + + +SPACE [[:blank:]]+ +LEFTP "(" +RIGHTP ")" +ATOM [a-z][a-z0-9]* +NUMBER [1-9][0-9]* + +%% + +{SPACE} { } +{LEFTP} { paren(LEFT); } +{RIGHTP} { paren(RIGHT); } +{ATOM} { atom(yytext); } +{NUMBER} { number(yytext); } + +%% + +// Parse input from in. Closes after finishing. +int parse(FILE* in) { + #ifndef DEBUG + FILE* out = fopen("/dev/null", "w"); + #else + FILE* out = stdout; + #endif + yyout = out; + yyin = in; + yylex(); + fclose(in); + fclose(out); + return 0; +}