2024-01-13 06:05:09 +00:00
|
|
|
%code requires {
|
2024-01-13 03:18:05 +00:00
|
|
|
#include <common.h>
|
2024-01-13 03:21:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
extern int yylex(void);
|
2024-01-13 03:18:05 +00:00
|
|
|
}
|
2024-01-12 15:23:43 +00:00
|
|
|
%{
|
2024-01-13 03:21:51 +00:00
|
|
|
#include <common.h>
|
2024-01-15 14:57:06 +00:00
|
|
|
#include <isa.h>
|
2024-01-12 15:23:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-01-13 03:21:51 +00:00
|
|
|
void yyerror(word_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
|
2024-01-15 08:27:37 +00:00
|
|
|
%token REGISTER
|
2024-01-12 15:23:43 +00:00
|
|
|
%start input
|
2024-01-13 03:18:05 +00:00
|
|
|
%define api.value.type { word_t }
|
2024-01-12 15:23:43 +00:00
|
|
|
%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:36:30 +00:00
|
|
|
| expression '/' expression {
|
|
|
|
if($3 == 0) {
|
|
|
|
fprintf(stderr, "Error: divide by zero at %u / %u\n", $1, $3);
|
|
|
|
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
|
2024-01-15 15:12:20 +00:00
|
|
|
| REGISTER { $$ = $1; }
|
2024-01-12 15:23:43 +00:00
|
|
|
| HEX_NUMBER
|
|
|
|
|
|
|
|
%%
|