From 41d98bd3fde6fdc4d1550108f8528f8dfcc97206 Mon Sep 17 00:00:00 2001 From: Felix Perktold Date: Sun, 21 Dec 2025 00:24:12 +0100 Subject: add symbols, code cleanup --- lisp.l | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'lisp.l') diff --git a/lisp.l b/lisp.l index ecf91db..bbeb02d 100644 --- a/lisp.l +++ b/lisp.l @@ -7,6 +7,11 @@ int tcnt = 0; %} %% +[[:space:]]+ { printf("%d: WHITESPACE\n", ++tcnt); return WHITESPACE; } +"(" { printf("%d: LPAREN\n", ++tcnt); return LPAREN; } +")" { printf("%d: RPAREN\n", ++tcnt); return RPAREN; } +"\." { printf("%d: DOT\n", ++tcnt); return DOT; } + [+-]?([0-9]+|([0-9]*\.[0-9]+))([eE][-+]?[0-9]+)? { yylval.dval = atof(yytext); printf("%d: NUMBER: %f\n", ++tcnt, yylval.dval); @@ -17,7 +22,7 @@ int tcnt = 0; // remove starting and ending quotes int len = yyleng - 2; char *str = (char*)malloc(len + 1); - if (!str) { fprintf(stderr, "Out of memory\n"); exit(1); } + if (!str) { fprintf(stderr, "out of memory!\n"); exit(1); } strncpy(str, yytext + 1, len); str[len] = '\0'; @@ -26,13 +31,10 @@ int tcnt = 0; return STRING; } -[ \t\n]+ { printf("%d: WHITESPACE\n", tcnt); return WHITESPACE; } -"(" { printf("%d: LPAREN\n", ++tcnt); return LPAREN; } -")" { printf("%d: RPAREN\n", ++tcnt); return RPAREN; } -"\." { printf("%d: DOT\n", ++tcnt); return DOT; } -"+" { printf("%d: PLUS\n", ++tcnt); return PLUS; } -"-" { printf("%d: MINUS\n", ++tcnt); return MINUS; } -"*" { printf("%d: MULT\n", ++tcnt); return MULT; } -"/" { printf("%d: DIV\n", ++tcnt); return DIV; } +[^[:space:]\"()]+ { + yylval.strval = strdup(yytext); + printf("%d: SYMBOL: %s\n", ++tcnt, yylval.strval); + return SYMBOL; + } . ; // do nothing %% -- cgit v1.2.3