solve day 2
This commit is contained in:
parent
625bd8268c
commit
0d315a789b
8 changed files with 2696 additions and 0 deletions
54
day_2-2/Makefile
Normal file
54
day_2-2/Makefile
Normal file
|
@ -0,0 +1,54 @@
|
|||
CC := clang
|
||||
YACC := bison
|
||||
LEX := flex
|
||||
CFLAGS := -std=c17 -Wpedantic -Wall -Wextra
|
||||
BUILD := ./build
|
||||
INCLUDE := -I./
|
||||
OBJ_DIR := $(BUILD)/objects
|
||||
APP_DIR := $(BUILD)/bin
|
||||
TARGET := day_2
|
||||
|
||||
SRC := $(wildcard ./*.cpp) lexer.c parser.c
|
||||
|
||||
OBJECTS := $(SRC:%.c=$(OBJ_DIR)/%.o)
|
||||
|
||||
# targets
|
||||
all: build $(APP_DIR)/$(TARGET)
|
||||
|
||||
run: all
|
||||
$(APP_DIR)/$(TARGET)
|
||||
|
||||
flex:
|
||||
$(LEX) --outfile=lexer.c --header-file=lexer.h lexer.l
|
||||
|
||||
bison:
|
||||
$(YACC) --output=parser.c parser.y
|
||||
|
||||
generate: flex bison
|
||||
|
||||
$(OBJ_DIR)/%.o: %.c
|
||||
@mkdir -p $(@D)
|
||||
$(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $<
|
||||
|
||||
$(APP_DIR)/$(TARGET): flex bison $(OBJECTS)
|
||||
@mkdir -p $(@D)
|
||||
$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $(APP_DIR)/$(TARGET) $(OBJECTS)
|
||||
|
||||
.PHONY: all build clean debug release
|
||||
|
||||
build:
|
||||
@mkdir -p $(APP_DIR)
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
|
||||
debug: CFLAGS += -DDEBUG -g
|
||||
debug: all
|
||||
|
||||
sanitize: CFLAGS += -DDEBUG -g -fsanitize=address
|
||||
sanitize: all
|
||||
|
||||
release: CFLAGS += -O2
|
||||
release: all
|
||||
|
||||
clean:
|
||||
-@rm -rvf $(BUILD)
|
||||
-@rm -rvf {lexer.c,parser.c} {lexer.h,parser.h}
|
2500
day_2-2/input.txt
Normal file
2500
day_2-2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
45
day_2-2/lexer.l
Normal file
45
day_2-2/lexer.l
Normal file
|
@ -0,0 +1,45 @@
|
|||
%{ /* -*- 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]
|
||||
SPACE [ \t]
|
||||
ROCK [A]
|
||||
PAPER [B]
|
||||
SCISSOR [C]
|
||||
LOSS [X]
|
||||
DRAW [Y]
|
||||
WIN [Z]
|
||||
|
||||
%%
|
||||
|
||||
{NL} { return NEWLINE; }
|
||||
{SPACE}+ { /* don't care */}
|
||||
{ROCK} { return ROCK; }
|
||||
{PAPER} { return PAPER; }
|
||||
{SCISSOR} { return SCISSOR; }
|
||||
{LOSS} { return LOSS; }
|
||||
{DRAW} { return DRAW; }
|
||||
{WIN} { return WIN; }
|
||||
<<EOF>> { return END_OF_FILE; }
|
||||
. {
|
||||
printf("[error] Encountered unexpected token %s\n", yytext);
|
||||
return 0;
|
||||
}
|
||||
|
||||
%%
|
97
day_2-2/parser.y
Normal file
97
day_2-2/parser.y
Normal file
|
@ -0,0 +1,97 @@
|
|||
/* Require bison minimal version */
|
||||
%require "3.2"
|
||||
|
||||
/* write out a header file containing the token defines */
|
||||
%header
|
||||
|
||||
// Code for the header file generated by bison
|
||||
%code requires {
|
||||
typedef void* yyscan_t;
|
||||
|
||||
struct parser_state {
|
||||
unsigned long total_score;
|
||||
};
|
||||
}
|
||||
|
||||
// Code for the c file
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "parser.h"
|
||||
#include "lexer.h"
|
||||
|
||||
void yyerror(struct parser_state* state, yyscan_t scanner, const char* msg) {
|
||||
(void)state;
|
||||
(void)scanner;
|
||||
fprintf(stderr, "\033[93mSyntax Error: %s\033[0m\n", msg);
|
||||
}
|
||||
%}
|
||||
|
||||
// define a reentrant parser
|
||||
%define api.pure
|
||||
|
||||
// enable parser tracing and detailed error messages (plus Lookahead Correction)
|
||||
%define parse.trace
|
||||
%define parse.error detailed
|
||||
%define parse.lac full
|
||||
|
||||
%lex-param { yyscan_t scanner }
|
||||
%parse-param { struct parser_state* state }
|
||||
%parse-param { yyscan_t scanner }
|
||||
|
||||
%start input
|
||||
%token NEWLINE
|
||||
%token ROCK PAPER SCISSOR
|
||||
%token LOSS DRAW WIN
|
||||
%term END_OF_FILE
|
||||
|
||||
/* the format is: them<space>me */
|
||||
%%
|
||||
|
||||
input
|
||||
: line input
|
||||
| END_OF_FILE { return 0; }
|
||||
;
|
||||
|
||||
line
|
||||
: game_move NEWLINE
|
||||
;
|
||||
|
||||
game_move
|
||||
: ROCK LOSS { state->total_score += 3 + 0; /* SCISSOR */ }
|
||||
| ROCK DRAW { state->total_score += 1 + 3; /* ROCK */ }
|
||||
| ROCK WIN { state->total_score += 2 + 6; /* PAPER */ }
|
||||
| PAPER LOSS { state->total_score += 1 + 0; /* ROCK */ }
|
||||
| PAPER DRAW { state->total_score += 2 + 3; /* PAPER */ }
|
||||
| PAPER WIN { state->total_score += 3 + 6; /* SCISSOR*/ }
|
||||
| SCISSOR LOSS { state->total_score += 2 + 0; /* PAPER */ }
|
||||
| SCISSOR DRAW { state->total_score += 3 + 3; /* SCISSOR*/ }
|
||||
| SCISSOR WIN { state->total_score += 1 + 6; /* ROCK */ }
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
int main(void) {
|
||||
struct parser_state parser_state;
|
||||
parser_state.total_score = 0;
|
||||
|
||||
yyscan_t scanner;
|
||||
|
||||
if (yylex_init(&scanner)) {
|
||||
fprintf(stderr, "\033[91m[error] Could not initialize lexer\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (yyparse(&parser_state, scanner)) {
|
||||
// TODO: Error handling https://www.gnu.org/software/bison/manual/html_node/Parser-Function.html
|
||||
// error during parse occured
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
yylex_destroy(scanner);
|
||||
|
||||
// task 2
|
||||
printf("total score: %lu\n", parser_state.total_score);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue