aoc-2022/day_4/lexer.l

50 lines
1,009 B
C

%{ /* -*- C -*- */
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
%}
%option warn nodefault
/* makes the scanner terminate after reaching <<EOF>> instead of assuming a new input was provided */
%option noyywrap
/* disable some unused functionality, add scanner tracking */
%option nounput noinput batch debug
/* gimme a reentrant parser (overkill but more pure) */
%option reentrant
%option bison-bridge
NL [\n]
NUM [0-9]
MINUS [-]
COMMA [,]
%%
{NL} {
return NEWLINE;
}
{NUM}+ {
unsigned long num = strtoul(yytext, NULL, 10);
yylval->num = num;
return NUMBER;
}
{MINUS} {
return MINUS;
}
{COMMA} {
return SEP;
}
<<EOF>> {
return END_OF_FILE;
}
. {
printf("[error] Encountered unexpected token %s\n", yytext);
return 0;
}
%%