4 //#define YYSTYPE struct token
6 #include "ktrfmt.tab.h"
14 /* XXX: need to switch to reentrant lexer */
15 static struct token tokens[NR_TOKENS];
17 static struct symtab *strtab;
23 if (curr_tok == NR_TOKENS) {
25 fprintf(stderr, "Reached max number of tokens\n");
28 return &tokens[curr_tok];
32 tok_free(struct token *tok)
34 assert(&tokens[curr_tok] == tok);
39 * We keep track of strings we've seen before so string comparison
40 * can be done w/ a simple pointer comparison
48 strtab = symtab_new();
49 if ((r = symtab_find(strtab, s)))
53 symtab_insert(strtab, r, r);
59 %option prefix="__ktrfmt"
60 %option outfile="ktrfmt.yy.c"
68 ID [a-z_$][a-zA-Z0-9_]*
69 CTOR [A-Z][a-zA-Z0-9_]*
72 {WHITE}+ { /* ignore */ }
75 yylval->tok = tok_new();
76 yylval->tok->type = TOK_STR;
78 assert(yytext[len - 1] == '"');
79 yytext[len - 1] = '\0'; /* kill trailing quote */
80 printd(LEX, "newstr(\"%s\")\n", yytext + 1);
81 yylval->tok->str = newstr(yytext + 1); /* parser detects oom */
82 yytext[len - 1] = '"'; /* restore quote */
83 printd(LEX, "TOK_STR: \"%s\"\n", yylval->tok->str);
87 yylval->tok = tok_new();
88 yylval->tok->type = TOK_ID;
89 printd(LEX, "tok %p TOK_ID %p:%s\n", yylval->tok, yytext, yytext);
90 yylval->tok->str = newstr(yytext); /* parser detects oom */
94 yylval->tok = tok_new();
95 yylval->tok->type = TOK_CTOR;
96 printd(LEX, "tok %p TOK_CTOR %p:%s\n", yylval->tok, yytext, yytext);
97 yylval->tok->str = newstr(yytext); /* parser detects oom */
101 yylval->tok = tok_new();
102 yylval->tok->type = TOK_INT;
103 yylval->tok->str = strdup(yytext); /* parser detects oom */
104 printd(LEX, "TOK_INT\n");
108 yylval->tok = tok_new();
109 yylval->tok->type = TOK_INT;
110 yylval->tok->str = strdup(yytext); /* parser detects oom */
111 printd(LEX, "TOK_INT\n");
116 printd(LEX, "TOK_EQ\n");
121 printd(LEX, "TOK_DOT\n");
126 printd(LEX, "TOK_LEFT_BRACK\n");
127 return TOK_LEFT_BRACK;
131 printd(LEX, "TOK_RIGHT_BRACK\n");
132 return TOK_RIGHT_BRACK;