From 8b790839df78609f3cb4d259ca7c609c96452371 Mon Sep 17 00:00:00 2001 From: Christian Barthel Date: Wed, 26 Jun 2019 22:21:36 +0200 Subject: [PATCH] tokenizer init --- tokenize.l | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tokenize.l diff --git a/tokenize.l b/tokenize.l new file mode 100644 index 0000000..28b20de --- /dev/null +++ b/tokenize.l @@ -0,0 +1,54 @@ +/* + * flex tokenize.l + * cc lex.yy.c -lfl + */ +%{ +enum yytokentype { + NUMBER = 258, + LPAR = 259, + RPAR = 260, + SYM = 261, + STR = 262, + EOL = 263 +}; + +int yylval; +char *yystr; +%} + +%% +"(" { return LPAR; } +")" { return RPAR; } +[0-9]+ { yylval = atoi(yytext); return NUMBER; } +[ \t\n] { /* ignore white space */ } +\".*\" { printf("str: %s\n", yytext); } +[a-zA-Z][a-zA-Z0-9]* { printf("sym: %s\n", yytext); } +. { printf("error: %s\n", yytext); } +%% + + +/* + * Goal: write LISP read-eval-print Loop and support + * @ Function + * 0,1 boolean (nand 0 1) -> boolean + * (eq 0 0) -> boolean + * "str" Strings (streq str1 str2) -> boolean + * abc Symboles (symeq s1 s2) -> boolean + * 1234 Number/Integer [add,sub,div,mul,mod] -> number + * [lt, numeq] -> boolean + * + * (define a ) + */ + +int main(void) +{ + int tok; + + while(tok = yylex()) { + printf("token: %d\n", tok); + if(tok == NUMBER) printf(" = %d\n", yylval); + //else printf("\n"); + } +}