ysyx-workbench/nemu/src/monitor/sdb/addrexp.y
tracer-ysyx 64cf984f5f > compile NEMU
ysyx_22040000 李心杨
Linux calcite 6.1.75 #1-NixOS SMP PREEMPT_DYNAMIC Thu Jan 25 23:27:52 UTC 2024 x86_64 GNU/Linux
 22:44:49  up   4:45,  2 users,  load average: 0.41, 0.21, 0.15
2024-02-05 22:44:49 +08:00

56 lines
1.1 KiB
Text

%code requires {
#include <common.h>
#include <memory/vaddr.h>
#include <stdio.h>
#include <stdlib.h>
extern int yylex(void);
}
%{
#include <common.h>
#include <isa.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
%token REGISTER
%locations
%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 {
printf("deref: %u\n", $2);
// $$ = vaddr_read($2, WORD_BYTES);
}
| '(' expression ')' { $$ = $2; }
number
: REGISTER
| NUMBER
| HEX_NUMBER
%%