summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-20 22:43:27 +0100
committerFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-20 22:43:27 +0100
commitdc0074b1accf92ce2e1fa126dccf702722f11611 (patch)
treea667b62a283362d6ade7555eabbe47b19be5c5f3
parenta75b2c6fe57edec9b1bf876a5e8ad98762c7d0ba (diff)
fix string lexing
-rw-r--r--lisp.l17
1 files changed, 15 insertions, 2 deletions
diff --git a/lisp.l b/lisp.l
index 329fc0f..ecf91db 100644
--- a/lisp.l
+++ b/lisp.l
@@ -11,7 +11,21 @@ int tcnt = 0;
yylval.dval = atof(yytext);
printf("%d: NUMBER: %f\n", ++tcnt, yylval.dval);
return NUMBER;
- }
+ }
+
+\"(\\.|[^"\\])*\" {
+ // 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); }
+ strncpy(str, yytext + 1, len);
+ str[len] = '\0';
+
+ yylval.strval = str;
+ printf("%d: STRING: \"%s\"\n", ++tcnt, yylval.strval);
+ 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; }
@@ -20,6 +34,5 @@ int tcnt = 0;
"-" { printf("%d: MINUS\n", ++tcnt); return MINUS; }
"*" { printf("%d: MULT\n", ++tcnt); return MULT; }
"/" { printf("%d: DIV\n", ++tcnt); return DIV; }
-\".*\" { printf("%d: STRING\n", ++tcnt); return STRING; }
. ; // do nothing
%%