0

我正在使用这个 lex 程序来分析每个寄存器的使用情况,无论它们是用作源、目标还是用作间接寻址,这些都是我的代码,我必须分析所有 12 个寄存器...有没有反对声明 36 个变量和 36 个规则的其他有效方法是什么?(我是学生)。

%{ 
#include <stdio.h>

int q,w,e,r,t,y,u,i,o,p,a,s = 0;

%}

%%
[push|dec]?+[ ]+(eax|EAX)+[,] q++ ;
[,| ]+(eax|EAX)+[ ]? w++ ;
['[']+(eax|EAX)+[ ]? e++ ;
[push|dec]?+[ ]+(ebx|EBX)+[,] r++ ;
[,| ]+(ebx|EBX)+[ ]? t++ ;
['[']+(ebx|EBX)+[ ]? y++ ;
[push|dec]?+[ ]+(ecx|ECX)+[,] u++ ;
[,| ]+(ecx|ECX)+[ ]? i++ ;
['[']+(ecx|ECX)+[ ]? o++ ;
[push|dec]?+[ ]+(edx|EDX)+[,] p++ ;
[,| ]+(edx|EDX)+[ ]? a++ ;
['[']+(edx|EDX)+[ ]? s++ ;




. ;



%%

main()

{

yylex() ;

printf("EAX as source: %d\n", q) ;
printf("EAX as destination: %d\n", w) ;
printf("EAX as indirect address: %d\n", e) ;
printf("EBX as source: %d\n", r) ;
printf("EBX as destination: %d\n", t) ;
printf("EBX as indirect address: %d\n", y) ;
printf("ECX as source: %d\n", u) ;
printf("ECX as destination: %d\n", i) ;
printf("ECX as indirect address: %d\n", o) ;
printf("EDX as source: %d\n", p) ;
printf("EDX as destination: %d\n", a) ;
printf("EDX as indirect address: %d\n", s) ;
}

这是我的输出:

EAX as source: 56
EAX as destination: 71
EAX as indirect address: 0
EBX as source: 25
EBX as destination: 38
EBX as indirect address: 0
ECX as source: 26
ECX as destination: 30
ECX as indirect address: 0
EDX as source: 30
EDX as destination: 38
EDX as indirect address: 0
ubuntu@ubuntu:~$ ^C

我要分析的文件在这里这是 asm 文件所在的位置

4

1 回答 1

1

正如我在评论中所说,我已经很久没有使用 lex 了。不过,这个想法是定义您的寄存器,然后定义一个使用该定义的表达式。

因此,例如,您的 lex 文件将类似于:

%

R EAX|eax|EBX|ebx|ECX|ecx|EDX|edx

%%

[push|dec]?+[ ]+({R})+[,] q++ ;
[,| ]+({R})+[ ]? w++ ;
['[']+({R})+[ ]? e++ ;

%%

main()
...

我的语法可能并不完美,但这是一般的想法。查找一些简单的 lex 定义示例。

于 2013-09-26T22:21:53.177 回答