Last active
October 2, 2022 04:50
-
-
Save depp/19b92ef417a2d68932de6f3be95985eb to your computer and use it in GitHub Desktop.
Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS += -O2 -Wall -Wextra -std=c17 -D_DEFAULT_SOURCE | |
scanner: scanner.o | |
$(CC) -o $@ $^ | |
scanner.c: scanner.l | |
flex -o$@ $< | |
clean: | |
rm -f scanner.o scanner.c scanner | |
.PHONY: clean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include <stdarg.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
enum { | |
End, | |
Comma, | |
Equals, | |
Semicolon, | |
Number, | |
Identifier, | |
}; | |
__attribute__((format(printf, 1, 2), noreturn)) | |
static void fatal_error(const char *msg, ...) { | |
va_list ap; | |
va_start(ap, msg); | |
fprintf(stderr, "line %d: error: ", yylineno); | |
vfprintf(stderr, msg, ap); | |
fputc('\n', stderr); | |
va_end(ap); | |
exit(1); | |
} | |
%} | |
digit [0-9] | |
idstart [a-zA-Z_] | |
idcont [a-zA-Z_0-9] | |
%option noyywrap | |
%option yylineno | |
%% | |
[ \t\n\r]+ { } | |
"//".* { } | |
"," { return Comma; } | |
"=" { return Equals; } | |
";" { return Semicolon; } | |
[-+]?{digit}+ { return Number; } | |
{idstart}{idcont}* { return Identifier; } | |
. { fatal_error("unexpected character: '%s'", yytext); } | |
%% | |
#define NAMESIZE 100 | |
#define DATASIZE (32 * 1024) | |
int main(int argc, char **argv) { | |
(void)argc; | |
(void)argv; | |
char var_name[NAMESIZE]; | |
int *data = malloc(DATASIZE * sizeof(*data)); | |
while (1) { | |
int tok = yylex(); | |
if (tok == 0) { | |
break; | |
} | |
// Parse the variable name. | |
if (tok != Identifier) { | |
fatal_error("expected identifier"); | |
} | |
size_t len = strlen(yytext); | |
if (len >= NAMESIZE) { | |
fatal_error("identifier is too long"); | |
} | |
memcpy(var_name, yytext, len + 1); | |
// Parse the =. | |
tok = yylex(); | |
if (tok != Equals) { | |
fatal_error("expect '='"); | |
} | |
// Parse numbers in a loop. | |
int data_size = 0; | |
while (1) { | |
// First, parse the number. | |
tok = yylex(); | |
if (tok != Number) { | |
fatal_error("expect number"); | |
} | |
if (data_size >= DATASIZE) { | |
fatal_error("too many numbers in array"); | |
} | |
data[data_size] = strtol(yytext, NULL, 0); | |
data_size++; | |
// Next token is either comma or semicolon. | |
tok = yylex(); | |
if (tok != Comma) { | |
if (tok == Semicolon) { | |
break; | |
} | |
fatal_error("expect ',' or ';'"); | |
} | |
} | |
// Print the variable and its value. | |
printf("Variable: %s = { %d", var_name, data[0]); | |
for (int i = 1; i < data_size; i++) { | |
printf(", %d", data[i]); | |
} | |
fputs(" }\n", stdout); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Variable: map_width = { 26 } | |
Variable: map_depth = { 24 } | |
Variable: map_height = { 2 } | |
Variable: tiles = { 5, 5, 5, 1, 1, 1, 9, 1, 1, 1, 1, 1, 10, 5, 5, 10, 1, 1, 1, 1, 1, 9, 1, 5, 5, 5, 5, 5, 5, 1, 1, 1, 9, 1, 1, 1, 1, 1, 10, 5, 5, 10, 1, 1, 1, 1, 1, 9, 1, 5, 5, 5, 5, 5, 5, 1, 1, 1, 9, 1, 1, 1, 1, 1, 9, 5, 5, 9, 1, 1, 1, 1, 1, 9, 1, 5, 5, 5, 5, 5, 5, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 9, 9, 9, 9, 9, 9, 9, 9, 5, 5, 5, 5, 5, 5, 9, 1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1, 9, 5, 5, 5, 5, 5, 5, 9,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } | |
Variable: borders = { 0 } | |
Variable: has_buildings = { 1 } | |
Variable: has_borders = { 0 } | |
Variable: hs_txtiles = { 6, 7, 4, 7, 6, 6, 13, 7, 12, 13, 7, 14, 15, 4, 4, 6, 7, 4, 6, 6, 7, 12, 13, 0, 1, 2, 2, 18, 19, 8, 9, 10, 11, 11, 11, 11, 16, 17, 0, 0, 0, 0, 0, 11, 11, 11, 24, 24, 24, 24, 22, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 22, 23, 24, 24, 24 } | |
Variable: hs_height = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 5, 5, 5 } | |
Variable: hs_type = { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 } | |
Variable: cl_tiles = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment