From bb631d0e744fa91c663a7a35195125e36c1139b0 Mon Sep 17 00:00:00 2001 From: Felix Perktold Date: Sun, 21 Dec 2025 00:35:10 +0100 Subject: remove WHITESPACE token, ignore whitespace in lexer --- lisp.l | 4 ++-- lisp.y | 21 ++++++++------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/lisp.l b/lisp.l index bbeb02d..7a9eece 100644 --- a/lisp.l +++ b/lisp.l @@ -7,7 +7,7 @@ int tcnt = 0; %} %% -[[:space:]]+ { printf("%d: WHITESPACE\n", ++tcnt); return WHITESPACE; } +[[:space:]]+ { printf("%d: WHITESPACE\n", tcnt); /*ignore whitespace*/} "(" { printf("%d: LPAREN\n", ++tcnt); return LPAREN; } ")" { printf("%d: RPAREN\n", ++tcnt); return RPAREN; } "\." { printf("%d: DOT\n", ++tcnt); return DOT; } @@ -31,7 +31,7 @@ int tcnt = 0; return STRING; } -[^[:space:]\"()]+ { +[^[:space:]\"().]+ { yylval.strval = strdup(yytext); printf("%d: SYMBOL: %s\n", ++tcnt, yylval.strval); return SYMBOL; diff --git a/lisp.y b/lisp.y index dd6eddf..da1b315 100644 --- a/lisp.y +++ b/lisp.y @@ -20,7 +20,7 @@ static int is_integer(double x) { } %token NUMBER %token STRING SYMBOL -%token LPAREN RPAREN DOT WHITESPACE +%token LPAREN RPAREN DOT %token PLUS MINUS MULT DIV %type sexpr atom list list_items @@ -28,27 +28,22 @@ static int is_integer(double x) { %start sexprs %% -opt_ws: /* matches no whitespace */ - | WHITESPACE; - sexprs: sexpr { print_val($1); printf("\n"); } - | sexprs opt_ws sexpr { print_val($3); printf("\n"); }; + | sexprs sexpr { print_val($2); printf("\n"); }; sexpr: atom | list - | LPAREN opt_ws sexpr opt_ws DOT opt_ws sexpr opt_ws RPAREN + | LPAREN sexpr DOT sexpr RPAREN { - $$ = cons($3, $7); - print_val($$); - printf("\n"); + $$ = cons($2, $4); }; -list: LPAREN opt_ws list_items opt_ws RPAREN - { $$ = $3; }; +list: LPAREN list_items RPAREN + { $$ = $2; }; list_items: /* empty list */ { $$ = make_nil(); } - | sexpr opt_ws list_items { $$ = cons($1, $3); } - | sexpr WHITESPACE DOT WHITESPACE sexpr { $$ = cons($1, $5); }; + | sexpr list_items { $$ = cons($1, $2); } + | sexpr DOT sexpr { $$ = cons($1, $3); }; atom: NUMBER { -- cgit v1.2.3