将语义规则应用于野牛

时间:2013-10-10 17:10:21

标签: c bison lex

我在这个Bison计划中遇到了麻烦。它必须通过将它们乘以2 ^ n来接收一个1s和0s的字符串,其周期类似于“101.101”。例如:

 "101.101" = (1*2^2)+(0*2^1)+(1*2^0)+(1*2^-1)+(0*2^-2)+(1*2^-3)=5.625

这一点说明战俘何时是积极的还是消极的。我有以下语义行为:

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等变量添加到Bison文件中,然后实现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();
}

Lex文件

%{
#include <stdio.h>
#include <math.h>
#include "y.tab.h"
%}
BINARY [0-1] 
%%
"1" {return ONE;}
"0" {return ZERO;}
"." {return DOT;}
%%

1 个答案:

答案 0 :(得分:1)

我写了这个,它并不完全是你想要的,但它展示了如何在yacc中给出规则类型以及如何将C代码放入规则中。

The C code is in braces { } 
$$ means "this rule"
$1 means "argument 1"
$2 means "argument 2"

So the rule
    r : r b     
and the code 
   { $$ = $1 << 1 | $2; } 
means 
   r = r << 1 | b

yacc文件是:

%{
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void yyerror (char *string);
int down =0;

%}

%union { // %union is needed to tell yacc about the types your rules will return
    unsigned val; // rules with type "val" will return unsigned
};


%token DOT
%token ZERO
%token ONE

%type <val> l r b; // rules l, r, and b are of type val

%%

s : l DOT r { printf("l.r = 0x%x.0x%x\n", $1, $3); }
  | l       { printf("l = 0x%x\n", $1); }
  ;

l : l b     { $$ = $1 << 1 | $2; } // l = l << 1 | b
  | b       { $$ = $1; } // l = b
  ;

r : r b     { $$ = $1 << 1 | $2; } // r = r << 1 | b
  | b       { $$ = $1; } // r = b
  ;

b : ONE     { $$ = 1; } // b = 0
  | ZERO            { $$ = 0; } // b = 1
  // | error
  ;

%%
//#include "lex.yy.c"

void yyerror (char *string){
  printf ("%s",string);
}

int yywrap() { return 1; }

int main (){
    yyparse();
    return 0;
}

lex文件

%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
"1" {return ONE;}
"0" {return ZERO;}
"." {return DOT;}
%%

请注意,lex文件包含“y.tab.h”。这是由命令

创建的
yacc -d file.y

运行是这样的,程序名为“x”:

[Charlies-MacBook-Pro:~] crb% cat i
011.110
[Charlies-MacBook-Pro:~] crb% x < i
l.r = 0x3.0x6

您可以根据需要更改代码。或者您 可以在“s”规则中进行函数调用并倒入位l和r 在那。

相关问题