abab954dbb
ysyx_22040000 李心杨 Linux calcite 6.1.71 #1-NixOS SMP PREEMPT_DYNAMIC Fri Jan 5 14:18:41 UTC 2024 x86_64 GNU/Linux 11:21:51 up 0:16, 2 users, load average: 0.24, 0.32, 0.19
48 lines
998 B
Text
48 lines
998 B
Text
%code requires {
|
|
#include <common.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
extern int yylex(void);
|
|
}
|
|
%{
|
|
#include <common.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
void yyerror(word_t *result, const char *err) {
|
|
fprintf(stderr, "Error: %s\n", err);
|
|
}
|
|
%}
|
|
|
|
%token NUMBER HEX_NUMBER
|
|
%start input
|
|
%define api.value.type { word_t }
|
|
%parse-param { uint32_t *result }
|
|
%left '-' '+'
|
|
%left '*' '/'
|
|
|
|
%%
|
|
input
|
|
: expression { *result = $1; }
|
|
;
|
|
|
|
expression
|
|
: number { $$ = $1; }
|
|
| expression '+' expression { $$ = $1 + $3; }
|
|
| expression '-' expression { $$ = $1 - $3; }
|
|
| expression '*' expression { $$ = $1 * $3; }
|
|
| expression '/' expression {
|
|
if($3 == 0) {
|
|
fprintf(stderr, "Error: divide by zero at %u / %u\n", $1, $3);
|
|
YYABORT;
|
|
};
|
|
$$ = $1 / $3;
|
|
}
|
|
| '-' number { $$ = -$2; }
|
|
| '(' expression ')' { $$ = $2; }
|
|
|
|
number
|
|
: NUMBER
|
|
| HEX_NUMBER
|
|
|
|
%%
|