4 //#define YYSTYPE struct token
8 #include "ktrfmt.tab.h"
15 /* XXX: need to switch to reentrant lexer */
16 static struct token tokens[NR_TOKENS];
18 static struct symtab *strtab;
25 if (curr_tok == NR_TOKENS) {
27 fprintf(stderr, "Reached max number of tokens\n");
30 return &tokens[curr_tok];
34 tok_free(struct token *tok)
36 assert(&tokens[curr_tok] == tok);
41 * We keep track of strings we've seen before so string comparison
42 * can be done w/ a simple pointer comparison
50 strtab = symtab_new();
51 if ((r = symtab_find(strtab, s)))
55 symtab_insert(strtab, r, r);
61 %option prefix="__ktrfmt"
62 %option outfile="ktrfmt.yy.c"
70 ID [a-z_$][a-zA-Z0-9_]*
71 CTOR [A-Z][a-zA-Z0-9_]*
74 {WHITE}+ { /* ignore */ }
77 yylval->tok = tok_new();
78 yylval->tok->type = TOK_STR;
80 assert(yytext[len - 1] == '"');
81 yytext[len - 1] = '\0'; /* kill trailing quote */
82 printd(LEX, "newstr(\"%s\")\n", yytext + 1);
83 yylval->tok->str = newstr(yytext + 1); /* parser detects oom */
84 yytext[len - 1] = '"'; /* restore quote */
85 printd(LEX, "TOK_STR: \"%s\"\n", yylval->tok->str);
89 yylval->tok = tok_new();
90 yylval->tok->type = TOK_ID;
91 printd(LEX, "tok %p TOK_ID %p:%s\n", yylval->tok, yytext, yytext);
92 yylval->tok->str = newstr(yytext); /* parser detects oom */
96 yylval->tok = tok_new();
97 yylval->tok->type = TOK_CTOR;
98 printd(LEX, "tok %p TOK_CTOR %p:%s\n", yylval->tok, yytext, yytext);
99 yylval->tok->str = newstr(yytext); /* parser detects oom */
103 yylval->tok = tok_new();
104 yylval->tok->type = TOK_INT;
105 yylval->tok->str = strdup(yytext); /* parser detects oom */
106 printd(LEX, "TOK_INT\n");
110 yylval->tok = tok_new();
111 yylval->tok->type = TOK_INT;
112 yylval->tok->str = strdup(yytext); /* parser detects oom */
113 printd(LEX, "TOK_INT\n");
118 printd(LEX, "TOK_EQ\n");
123 printd(LEX, "TOK_DOT\n");
128 printd(LEX, "TOK_LEFT_BRACK\n");
129 return TOK_LEFT_BRACK;
133 printd(LEX, "TOK_RIGHT_BRACK\n");
134 return TOK_RIGHT_BRACK;