-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
92 lines (80 loc) · 3.3 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
%{
#include <stdlib.h>
#include "machine.h"
%}
%union {
int i;
char *strval;
struct Operand *oper;
struct Instruction *inst;
}
%token TOKNUM
%token TOKREG
%token TOKCOMA
%token TOKOPAREN
%token TOKCR
%token TOKCPAREN
%token TOKNUMBER
%token TOKID
%token TOKIMM
%type <i> TOKIMM TOKNUMBER
%type <strval> TOKREG TOKID TOKLABEL
%type <oper> operand
%type <inst> inst
%token TOKNOP
%token TOKMOV
%token TOKSW
%token TOKLW
%token TOKPUSH
%token TOKPOP
%token TOKPRINT
%token TOKREAD
%token TOKADD
%token TOKSUB
%token TOKDIV
%token TOKMUL
%token TOKCMP
%token TOKJMP
%token TOKJMPE
%token TOKJMPL
%token TOKHLT
%token TOKLABEL
%token TOKCALL
%token TOKRET
%token TOKAND
%start input
%%
input : /* empty */
| input inst TOKCR { code[count++]=*$2;}
;
inst:
TOKNOP { $$ = malloc(sizeof(struct Instruction)); $$->op=NOP;}
| TOKMOV operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=MOV; $$->src=*$2; $$->dst=*$4;}
| TOKSW operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=SW; $$->src=*$2; $$->dst=*$4;}
| TOKLW operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=LW; $$->src=*$2; $$->dst=*$4;}
| TOKPUSH operand { $$ = malloc(sizeof(struct Instruction)); $$->op=PUSH; $$->src=*$2;}
| TOKPOP operand { $$ = malloc(sizeof(struct Instruction)); $$->op=POP; $$->src=*$2;}
| TOKPRINT operand { $$ = malloc(sizeof(struct Instruction)); $$->op=PRINT ; $$->src=*$2;}
| TOKREAD operand { $$ = malloc(sizeof(struct Instruction)); $$->op=READ; $$->src=*$2;}
| TOKADD operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=ADD; $$->src=*$2; $$->dst=*$4;}
| TOKSUB operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=SUB; $$->src=*$2; $$->dst=*$4;}
| TOKMUL operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=MUL; $$->src=*$2; $$->dst=*$4;}
| TOKDIV operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=DIV; $$->src=*$2; $$->dst=*$4;}
| TOKCMP operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=CMP; $$->src=*$2; $$->dst=*$4;}
| TOKJMP operand { $$ = malloc(sizeof(struct Instruction)); $$->op=JMP; $$->src=*$2; }
| TOKJMPE operand { $$ = malloc(sizeof(struct Instruction)); $$->op=JMPE; $$->src=*$2; }
| TOKJMPL operand { $$ = malloc(sizeof(struct Instruction)); $$->op=JMPL; $$->src=*$2; }
| TOKLABEL { $$ = malloc(sizeof(struct Instruction)); $$->op=LABEL; $$->src.lab=$1; }
| TOKHLT { $$ = malloc(sizeof(struct Instruction)); $$->op=HLT;}
| TOKCALL operand { $$ = malloc(sizeof(struct Instruction)); $$->op=CALL; $$->src=*$2; }
| TOKRET { $$ = malloc(sizeof(struct Instruction)); $$->op=RET;}
| TOKAND operand TOKCOMA operand { $$ = malloc(sizeof(struct Instruction)); $$->op=AND; $$->src=*$2; $$->dst=*$4;}
;
operand:
TOKREG { $$ = malloc(sizeof(struct Operand)); $$->type=REG; $$->val=reg($1);}
| TOKOPAREN TOKREG TOKCPAREN { $$ = malloc(sizeof(struct Operand)); $$->type=MEM; $$->val=reg($2);}
| TOKNUMBER { $$ = malloc(sizeof(struct Operand)); $$->type=MEM; $$->val=$1;}
| TOKIMM { $$ = malloc(sizeof(struct Operand)); $$->type=IMM; $$->val=$1;}
| TOKID { $$ = malloc(sizeof(struct Operand)); $$->type=LABELOP; $$->lab=$1;}
;
%%