-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
97 lines (84 loc) · 1.86 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
90
91
92
93
94
95
96
%{
#include <stdio.h>
#include "globals.h"
int rega; /* Register A */
int regb; /* Register B */
int pc; /* Program Counter */
int statusRegister; /* Status Register */
int memory[MEMSIZE]; /* So, so much memory */
void printMemory(); /* Print Memory Contents */
extern FILE* yyin;
extern int yylineno;
extern char* yytext;
%}
%token MEMADDRESS
%token INP LDA LDB STR PNT JLT JGT JEQ JMP
%token ADD SUB MUL DIV MOD CMP STP
%token EOL
%%
statement:
/* Nothing */
| statement operation EOL { }
| statement EOL {
printMemory();
printf("Program Counter = %d\n",pc);
printf("Register A = %d\n",rega);
printf("Register B = %d\n",regb);
printf("Status Register = %d\n",statusRegister);
}
;
operation:
nullary
| unary
;
nullary:
ADD { regb = rega + regb; }
| SUB { regb = rega - regb; }
| MUL { regb = rega * regb; }
| DIV { regb = rega / regb; }
| MOD { regb = rega % regb; }
| CMP {
if (rega < regb)
statusRegister = -1;
if (rega > regb)
statusRegister = 1;
if (rega == regb)
statusRegister = 0;
}
| STP
;
unary:
INP address address { memory[$2] = $3; } /* file input */
| INP address { memory[$2] = $$; } /* stdin */
| LDA address { rega = memory[$2]; }
| LDB address { regb = memory[$2]; }
| STR address { memory[$2] = regb; }
| PNT address { printf("%i\n",memory[$2]); }
| JLT address { /* Not implemented yet */ }
| JGT address { /* Not implemented yet */ }
| JEQ address { /* Not implemented yet */ }
| JMP address { /* Not implemented yet */ }
;
address: MEMADDRESS
;
%%
void printMemory() {
int i;
for (i=0; i < MEMSIZE; i++) {
printf("%i:%i\n",i,memory[i]);
}
}
main (int argc, char **argv)
{
statusRegister = rega = regb = pc = 0;
if (argc > 1) {
if (!(yyin = fopen(argv[1],"r"))) {
perror(argv[1]);
return (1);
}
}
yyparse();
}
yyerror(char *s) {
printf("%s Line:%i\n", s,yylineno);
}