-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptimize.lex
52 lines (46 loc) · 1.57 KB
/
optimize.lex
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
%{
#include <stdlib.h>
#include <string.h>
char mystring[100];
char mystring2[100];
char *mychar;
//void yyerror(char *);
%}
%option nounput
%option noinput
%x rlda
%%
[ \t]+ putchar(' ');
[ \t\r]+$
"cmp #0"$
"cmp #$0"$
"cmp #$00"$
"sta "+[A-Za-z0-9]+ {printf("%s",yytext);mychar=strtok(yytext," ");strcpy(mystring,mychar+strlen(mychar)+1);BEGIN(rlda);}
<rlda>"," {printf("%s",yytext);BEGIN(INITIAL);}
<rlda>"+" {printf("%s",yytext);BEGIN(INITIAL);}
<rlda>"#" {printf("%s",yytext);BEGIN(INITIAL);}
<rlda>"(" {printf("%s",yytext);BEGIN(INITIAL);}
<rlda>[ \t\r\n]+ {printf("%s",yytext);}
<rlda>[A-Za-z0-9]+ {if (strcmp(yytext,"lda")) printf("%s",yytext);BEGIN(INITIAL);}
<rlda>"lda"+[ \t]+[A-Za-z0-9]+"+" {printf(" %s",yytext);BEGIN(INITIAL);}
<rlda>"lda"+[ \t]+[A-Za-z0-9]+ {
mychar=strtok(yytext," ");
strcpy(mystring2,mychar+strlen(mychar)+1);
if (strcmp(mystring,mystring2)) {printf(" lda %s",mystring2);BEGIN(INITIAL);}
else {printf(" ; lda %s",mystring2);BEGIN(INITIAL);}
}
<rlda>"ldx"+[ \t]+[A-Za-z0-9]+ {// experimental conversion of sta val/ldx val to sta/tax
mychar=strtok(yytext," ");
strcpy(mystring2,mychar+strlen(mychar)+1);
if (strcmp(mystring,mystring2)) {printf(" ldx %s",mystring2);BEGIN(INITIAL);}
else {printf(" tax\n");BEGIN(INITIAL);}
}
<rlda>"lda"+[ \t]+[^A-Za-z0-9] {printf(" %s",yytext);BEGIN(INITIAL);}
<rlda>"lda.w" {printf(" %s",yytext);BEGIN(INITIAL);}
[A-Za-z]+ printf("%s",yytext);
[0-9]+ { printf("%s", yytext);}
[\n] {printf("\n");}
. { printf("%s", yytext);}
%%
int yywrap(void) { return 1; }
int main(){yylex();}