2024-01-12 15:23:43 +00:00
|
|
|
%{
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
extern int yylex(void);
|
|
|
|
void yyerror(uint32_t *result, const char *s) {
|
|
|
|
fprintf(stderr, "Error: %s\n", s);
|
|
|
|
}
|
|
|
|
%}
|
|
|
|
|
|
|
|
%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-12 19:14:31 +00:00
|
|
|
| expression '/' expression { $$ = $1 / $3; if($3 == 0) perror("Devide by 0\n");}
|
2024-01-13 02:11:35 +00:00
|
|
|
| '-' number { $$ = -$2; }
|
2024-01-12 15:23:43 +00:00
|
|
|
| '(' expression ')' { $$ = $2; }
|
|
|
|
|
|
|
|
number
|
|
|
|
: NUMBER
|
|
|
|
| HEX_NUMBER
|
|
|
|
|
|
|
|
%%
|