summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lisp.l20
-rw-r--r--lisp.y3
2 files changed, 13 insertions, 10 deletions
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
%%
diff --git a/lisp.y b/lisp.y
index a15b451..dd6eddf 100644
--- a/lisp.y
+++ b/lisp.y
@@ -19,7 +19,7 @@ static int is_integer(double x) {
value *val;
}
%token <dval> NUMBER
-%token <strval> STRING
+%token <strval> STRING SYMBOL
%token <ival> LPAREN RPAREN DOT WHITESPACE
%token <ival> PLUS MINUS MULT DIV
@@ -59,6 +59,7 @@ atom: NUMBER
}
}
| STRING { $$ = make_string($1); };
+ | SYMBOL { $$ = make_symbol($1); };
%%
int yyerror(const char *s) {