aoc-2022/day_10/parser.y
2023-02-19 20:40:57 +01:00

132 lines
2.7 KiB
Text

/* 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 {
#include <stdbool.h>
typedef void* yyscan_t;
struct parser_state {
/// Program Counter
size_t cycle;
/// Contents of register X
long reg;
/// Task 1
long sum;
size_t next_target_cycle;
};
}
// 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);
}
void increment_cycle(struct parser_state *state) {
state->cycle++;
if (state->cycle == state->next_target_cycle) {
long tmp = state->cycle * state->reg;
printf("[info] Register contents at cycle %zu: %ld\n", state->cycle, state->reg);
state->sum += tmp;
if (state->next_target_cycle < 220) {
state->next_target_cycle += 40;
}
}
}
%}
// 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 }
%union {
int num;
}
%start input
%token NEWLINE
%token NOP ADDX
%token <num> NUM
%term END_OF_FILE
%%
input
: line input
| END_OF_FILE { return 0; }
;
line
: instruction NEWLINE
| NEWLINE
;
instruction
: NOP { increment_cycle(state); }
| ADDX NUM {
increment_cycle(state);
increment_cycle(state);
state->reg += $2;
}
;
%%
int main(void) {
struct parser_state *state = calloc(1, sizeof(struct parser_state));
if (!state) {
fprintf(stderr, "\033[91m[error] Ran out of memory\033[0m\n");
return EXIT_FAILURE;
}
state->reg = 1;
state->next_target_cycle = 20;
yyscan_t scanner;
if (yylex_init(&scanner)) {
fprintf(stderr, "\033[91m[error] Could not initialize lexer\033[0m\n");
return EXIT_FAILURE;
}
if (yyparse(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 1
printf("Sum of signal strengths: %ld\n", state->sum);
free(state);
return 0;
}