tokenizer init

This commit is contained in:
Christian Barthel 2019-06-26 22:21:36 +02:00
commit 8b790839df
1 changed files with 54 additions and 0 deletions

54
tokenize.l Normal file
View File

@ -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 <symbol,number,#function)
* (lambda (param ..) (.. body ..))
* (if <bool> <body> <else>)
*/
int main(void)
{
int tok;
while(tok = yylex()) {
printf("token: %d\n", tok);
if(tok == NUMBER) printf(" = %d\n", yylval);
//else printf("\n");
}
}