简体   繁体   English

在野牛中分割语法规则

[英]Splitting a grammar rule in Bison

I have a Bison rule 我有野牛规则

block: LBRACE  { some code } decls stmts RBRACE {more code } 
     ;

The issue is in the "more code" section, I have 问题在“更多代码”部分,我有

$$ = $3 ;

Basically, I want the return value of block to be stmts. 基本上,我希望块的返回值为stmts。 When I do this, Bison says "$3 of block has no type." 当我这样做时,Bison说“ $ 3的块没有类型。” If I remove the code block containing some code and stick it into the latter block, bison does not complain. 如果删除包含某些代码的代码块并将其粘贴到后一个块中,bison不会抱怨。 I have stmts and all of its derivatives declared as types. 我有stmts及其所有派生声明为类型。 So is this not allowed by Bison? 那么,野牛不允许这样做吗? I can make changes to the grammar to accommodate this, but it will be tedious and much easier to just use the above. 我可以对语法进行更改以适应这种情况,但是仅使用上述内容将很繁琐且容易得多。

Use $4 to refer to stmts . 使用$4引用stmts Since you have a mid-rule action , all proceeding symbol numbers are offset as the action itself can have a value. 由于您具有规则中的操作 ,因此所有后续符号编号都会发生偏移,因为操作本身可以具有值。

The corresponding component numbers are: 相应的组件编号为:
$1 LBRACE $ 1 LBRACE
$2 { some code } $ 2 { some code }
$3 decls $ 3 decls
$4 stmts $ 4 stmts
$5 RBRACE $ 5 RBRACE
$6 { more code } $ 6 { more code }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM