aoc-2022/day_7/lexer.l

80 lines
1.8 KiB
Text
Raw Permalink Normal View History

2023-01-07 02:17:15 +00:00
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
#ifdef DOESNT_HAVE_STRDUP
#warning DOESNT_HAVE_STRDUP
char *strdup(const char *s);
#endif
%}
%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]
SPACE [ ]
/*general definitions*/
CHAR [a-zA-Z]
NUM [0-9]
/*operands*/
SHELL [$]
ROOT [/]
PARENT ".."
CHDIR "cd"
LIST "ls"
DIR "dir"
%%
{NL} { return NEWLINE; }
{SPACE} { return SPACE; }
{SHELL}{SPACE} { return PROMPT; }
{CHDIR} { return CHDIR; }
{LIST} { return LIST; }
{ROOT} { return ROOT; }
{PARENT} { return PARENT; }
{DIR} { return DIRECTORY; }
{NUM}+ {
unsigned long num = strtoul(yytext, NULL, 10);
yylval->num = num;
return SIZE;
}
{CHAR}+ |
{CHAR}+"."{CHAR}+ {
yylval->path = strdup(yytext);
// yylval->path = calloc(strlen(yytext) + 1, sizeof(char));
// strcpy(yylval->path, yytext);
return PATHSPEC;
}
<<EOF>> {
return END_OF_FILE;
}
. {
printf("[error] Encountered unexpected token %s\n", yytext);
return 0;
}
%%