2024-01-12 15:23:43 +00:00
|
|
|
%{
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
extern int yylex(void);
|
2024-01-13 02:12:48 +00:00
|
|
|
void yyerror(uint32_t *result, const char *err) {
|
2024-01-13 02:25:01 +00:00
|
|
|
fprintf(stderr, "Error: %s\n", err);
|
2024-01-12 15:23:43 +00:00
|
|
|
}
|
|
|
|
%}
|
|
|
|
|
|
|
|
%token NUMBER HEX_NUMBER
|
|
|
|
%start input
|
|
|
|
%define api.value.type { uint32_t }
|
|
|
|
%parse-param { uint32_t *result }
|
2024-01-12 19:02:28 +00:00
|
|
|
%left '-' '+'
|
2024-01-12 15:23:43 +00:00
|
|
|
%left '*' '/'
|
|
|
|
|
|
|
|
%%
|
|
|
|
input
|
|
|
|
: expression { *result = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
expression
|
2024-01-12 18:55:41 +00:00
|
|
|
: number { $$ = $1; }
|
|
|
|
| expression '+' expression { $$ = $1 + $3; }
|
2024-01-12 15:23:43 +00:00
|
|
|
| expression '-' expression { $$ = $1 - $3; }
|
|
|
|
| expression '*' expression { $$ = $1 * $3; }
|
2024-01-13 02:21:59 +00:00
|
|
|
| expression '/' expression { if($3 == 0) { perror("Error: Devide by 0 in expression\n"); YYABORT; }; $$ = $1 / $3;}
|
2024-01-13 02:11:35 +00:00
|
|
|
| '-' number { $$ = -$2; }
|
2024-01-12 15:23:43 +00:00
|
|
|
| '(' expression ')' { $$ = $2; }
|
|
|
|
|
|
|
|
number
|
|
|
|
: NUMBER
|
|
|
|
| HEX_NUMBER
|
|
|
|
|
|
|
|
%%
|