]> begriffs open source - sa-parse/blob - src/lisp.l
Simplify CSV parser to match one in my blog
[sa-parse] / src / lisp.l
1 /* lisp.l */
2
3 /* disable unused functions so we don't
4    get compiler warnings about them */
5
6 %option noyywrap nounput noinput
7
8 /* change our prefix from yy to lisp */
9
10 %option prefix="lisp"
11
12 /* use the pure parser calling convention */
13
14 %option reentrant bison-bridge
15
16 %{
17 #include "lisp.tab.h"
18
19 #define YY_EXIT_FAILURE ((void)yyscanner, EXIT_FAILURE)
20
21 /* XOPEN for strdup */
22 #define _XOPEN_SOURCE 600
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 /* seems like a bug that I have to do this, since flex
28    should know prefix=lisp and match bison's LISPSTYPE */
29 #define YYSTYPE LISPSTYPE
30
31 int lisperror(const char *msg);
32 %}
33
34 %%
35
36 [[:alpha:]][[:alnum:]]* {
37         /* The memory that yytext points to gets overwritten
38            each time a pattern matches. We need to give the caller
39            a copy. Also, if strdup fails and returns NULL, it's up
40            to the caller (the parser) to detect that.
41
42            Notice yylval is a pointer to union now.  It's passed
43            as an arg to yylex in pure parsing mode */
44
45         yylval->str = strdup(yytext);
46         return ID;
47 }
48
49 [-+]?[[:digit:]]+ {
50         long n = strtol(yytext, NULL, 10);
51
52         if (n < INT_MIN || n > INT_MAX)
53                 lisperror("Number out of range");
54         yylval->num = (int)n;
55         return NUM;
56 }
57
58 [[:space:]]  ; /* ignore */
59
60 . { return *yytext; }