1811c60cf9
ysyx_22040000 李心杨 Linux calcite 6.1.69 #1-NixOS SMP PREEMPT_DYNAMIC Wed Dec 20 16:00:29 UTC 2023 x86_64 GNU/Linux 10:21:59 up 1 day 10:37, 2 users, load average: 0.48, 0.43, 0.49
36 lines
821 B
Text
36 lines
821 B
Text
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
extern int yylex(void);
|
|
void yyerror(uint32_t *result, const char *err) {
|
|
fprintf(stderr, "Error: %s\n", err);
|
|
}
|
|
%}
|
|
|
|
%token NUMBER HEX_NUMBER
|
|
%start input
|
|
%define api.value.type { uint32_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) { perror("Error: Devide by 0 in expression\n"); YYABORT; }; $$ = $1 / $3;}
|
|
| '-' number { $$ = -$2; }
|
|
| '(' expression ')' { $$ = $2; }
|
|
|
|
number
|
|
: NUMBER
|
|
| HEX_NUMBER
|
|
|
|
%%
|