This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asm.l
60 lines (53 loc) · 2.39 KB
/
asm.l
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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
void yyerror(char *);
int htoi(char *); /* change base 16 to base 10 number */
int Ohtoi(char c); /* change base 16 to base 10 number (1 character) */
%}
%%
"$r"[A-Z] { yylval.c = yytext[2]-'A'; return VAR;} /* Variable Token ($rA-$rZ) */
[0-9]+ {yylval.i = atoi(yytext); return NUM;} /* number Token (0-infinity)*/
[0-9A-Fa-f]+[Hh] {yylval.i = htoi(yytext); return NUM;} /* base 16 number Token (15AH) */
[-+*()\n/=\\] {return *yytext;} /* operation Token */
("AND"|"&") {return AND;} /* Bitwise AND Token */
("OR"|"|") {return OR;} /* Bitwise OR Token */
("NOT"|"!"|"~") {return NOT;} /* Bitwise NOT Token */
("XOR"|"^") {return XOR;} /* Bitwise XOR Token */
"PUSH" {return PUSH;} /* PUSH Token */
"POP" {return POP;} /* POP Token */
"SHOW" {return SHOW;} /* SHOW Token */
"LOAD" {return LOAD;} /* LOAD Token */
"$top" {return TOP;} /* $top Token */
"$size" {return SIZE;} /* $size Token */
"$acc" {return ACC;} /* $acc Token */
[ \t\f\v] ; //Ignore
. {return UNKNOWN;} /* out of gramma character Token */
%%
int Ohtoi(char c){ /* change base 16 to base 10 number (1 character) */
int r;
if(c>='0' && c<='9'){
r = c - '0';
}
else if(c>='A' && c<='F'){
r = c - 'A' + 10;
}
else if(c>='a' && c<='f'){
r = c - 'a' + 10;
}
return r;
}
int htoi(char *s){ /* change base 16 to base 10 number */
int i;
int result = Ohtoi(s[0]);
for(i=1; i<strlen(s)-1; i++){
result *= 16;
result += Ohtoi(s[i]);
}
return result;
}
int yywrap(void){
return 1;
}