0
我在這個Bison程序中遇到了麻煩。它必須通過將它們乘以2^n來接收具有諸如「101.101」的句點的1和0的字符串。例如:將語義規則應用到野牛
"101.101" = (1*2^2)+(0*2^1)+(1*2^0)+(1*2^-1)+(0*2^-2)+(1*2^-3)=5.625
該點告訴pow何時正面或負面。我有以下語義動作:
S→ L.R
S→ L
L → L1 B
L → B
R → R1 B
R → B
B→ 0
B → 1
Sematic Rules
L.pos=0;R.pos=-1;S.val=L.val+R.val
L.pos=0;S.val=L.val;
L1.pos = L.pos + 1; B.pos = L.pos; L.val = L1.val + B.val;
B.pos = L.pos; L.val = B.val;
R1.pos = R.pos - 1; B.pos = R.pos; L.val = L1.val + B.val;
B.pos = R.pos; L.val = B.val;
B.val=0;
B.val = 1*2B.pos;
我的問題是,我不知道如何像.VAL和.POS添加變量野牛文件,然後執行C代碼編程。
野牛
%{
#include <string.h>
#include <stdio.h>
#include<stdlib.h>
void yyerror (char *string);
int down =0;
%}
%token r1
%token l1
%token DOT
%token ZERO
%token ONE
%%
s: l DOT r
| l
;
l: l1 b
| b
;
r: r1 b
|b
;
b: ONE
| ZERO
| error
;
%%
#include "lex.yy.c"
void yyerror (char *string){
printf ("%s",string);
}
main(){
yyparse();
}
萊克斯文件
%{
#include <stdio.h>
#include <math.h>
#include "y.tab.h"
%}
BINARY [0-1]
%%
"1" {return ONE;}
"0" {return ZERO;}
"." {return DOT;}
%%
打我吧:)我只是做一個,但值類型爲'struct {int val; int pos;};'使OP更好一點。但繼續... – rici
無論哪種方式,聽起來像你的更好。我很懶。我不擅長解釋東西。您可以將l和r當成雙打。也許從這兩個答案OP將能夠弄清楚。 –