fix some errors

This commit is contained in:
Felix Suchert 2022-12-25 23:09:12 +01:00
parent 196660ccb1
commit 7a1411e86d
Signed by: feliix42
GPG key ID: 24363525EA0E8A99
3 changed files with 23 additions and 48 deletions

View file

@ -27,8 +27,8 @@ NUM [0-9]
return NEWLINE; return NEWLINE;
} }
{NUM}+ { {NUM}+ {
unsigned num = strtoul(yytext, NULL, 10); unsigned long num = strtoul(yytext, NULL, 10);
printf("number: %d\n", num); // printf("number: %d\n", num);
yylval->num = num; yylval->num = num;
return NUMBER; return NUMBER;
} }
@ -41,13 +41,3 @@ NUM [0-9]
} }
%% %%
/*int main() {*/
/*std::cout << "hello" << std::endl;*/
/*yyFlexLexer scanner;*/
/*scanner.yylex();*/
/*std::cout << "done" << std::endl;*/
/*return 0;*/
/*}*/

View file

@ -4,6 +4,17 @@
/* write out a header file containing the token defines */ /* write out a header file containing the token defines */
%header %header
// Code for the header file generated by bison
%code requires {
typedef void* yyscan_t;
struct parser_state {
unsigned long tmp_val;
unsigned long* calorie_list;
unsigned long size;
};
}
// Code for the c file // Code for the c file
%{ %{
#include <stdio.h> #include <stdio.h>
@ -13,7 +24,7 @@
void push_value(struct parser_state* state) { void push_value(struct parser_state* state) {
// TODO(feliix42): error handling here? // TODO(feliix42): error handling here?
state->calorie_list = realloc(state->calorie_list, state->size+1); state->calorie_list = realloc(state->calorie_list, (state->size+1) * sizeof(unsigned long));
state->calorie_list[state->size] = state->tmp_val; state->calorie_list[state->size] = state->tmp_val;
state->tmp_val = 0; state->tmp_val = 0;
state->size++; state->size++;
@ -26,17 +37,6 @@
} }
%} %}
// Code for the header file generated by bison
%code requires {
typedef void* yyscan_t;
struct parser_state {
unsigned tmp_val;
unsigned* calorie_list;
size_t size;
};
}
// define a reentrant parser // define a reentrant parser
%define api.pure %define api.pure
@ -55,24 +55,25 @@
%start input %start input
%token NEWLINE END_OF_FILE %token NEWLINE
%token <unsigned> NUMBER %token <num> NUMBER
%term END_OF_FILE
%% %%
input input
: line input : line input
| END_OF_FILE { /* done. */ } | END_OF_FILE { if (state->tmp_val != 0) { push_value(state); } return 0; }
; ;
line line
: NUMBER NEWLINE { state->tmp_val += yylval.num; } : NUMBER NEWLINE { state->tmp_val += $1; }
| NEWLINE { push_value(state); } | NEWLINE { push_value(state); }
; ;
%% %%
int main(int argc, char** argv) { int main(void) {
struct parser_state* parser_state = calloc(1, sizeof(struct parser_state)); struct parser_state* parser_state = calloc(1, sizeof(struct parser_state));
if (!parser_state) { if (!parser_state) {
fprintf(stderr, "[error] failed to allocate parser state\n"); fprintf(stderr, "[error] failed to allocate parser state\n");
@ -82,7 +83,6 @@ int main(int argc, char** argv) {
yyscan_t scanner; yyscan_t scanner;
/*YY_BUFFER_STATE state;*/ /*YY_BUFFER_STATE state;*/
printf("Hello World\n");
if (yylex_init(&scanner)) { if (yylex_init(&scanner)) {
fprintf(stderr, "\033[91m[error] Could not initialize lexer\n"); fprintf(stderr, "\033[91m[error] Could not initialize lexer\n");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -97,14 +97,14 @@ int main(int argc, char** argv) {
yylex_destroy(scanner); yylex_destroy(scanner);
// task 1.1 // task 1.1
unsigned max_val = 0; unsigned long max_val = 0;
for (int i = 0; i < parser_state->size; i++) { for (unsigned long i = 0; i < parser_state->size; i++) {
if (parser_state->calorie_list[i] > max_val) { if (parser_state->calorie_list[i] > max_val) {
max_val = parser_state->calorie_list[i]; max_val = parser_state->calorie_list[i];
} }
} }
printf("Largest individual calorie load: %d\n", max_val); printf("Largest individual calorie load: %lu\n", max_val);
return 0; return 0;

View file

@ -1,15 +0,0 @@
#include <stdlib.h>
struct parser_state {
unsigned tmp_val;
unsigned* calorie_list;
size_t size;
};
void push_value(struct parser_state* state) {
// TODO(feliix42): error handling here?
state->calorie_list = realloc(state->calorie_list, state->size+1);
state->calorie_list[state->size] = state->tmp_val;
state->tmp_val = 0;
state->size++;
}