请问一下,简单的文法为什么bison 发生了移进,而不是归约.

Viweei 2010-07-21 12:28:34
flex :

%{
#include "stdafx.h"
#include "language.tab.h"
extern int yywrap();
%}

ws [ \t]+
line \n

%%

var { return VAR; }
[a-z]+[a-z0-9]* {
strncpy(yylval.string,yytext,sizeof(yylval.string));
return NAME;
}


. { return yytext[0]; }

%%


bison (部份):

%union{
char string[1024];
};

%token VAR
%token <string> NAME

// %start value
%%

declare: VAR NAME ';'
;
%%


我希望 bison 能分析出 var xxx; 这样的语句, 如果在文法文件中没有加入 ';' 像这样
declare: VAR NAME
bison 是能分析出来的,一但加入了 ';' 在最后一步bison 产生了移进,并不是归约.还跑成了循环.
我输出的 out 文件内容如下:

state 0

0 $accept: . declare $end

VAR shift, and go to state 1

declare go to state 2


state 1

1 declare: VAR . NAME ';'

NAME shift, and go to state 3


state 2

0 $accept: declare . $end

$end shift, and go to state 4
state 3

1 declare: VAR NAME . ';'

';' shift, and go to state 5


state 4

0 $accept: declare $end .

$default accept


state 5

1 declare: VAR NAME ';' .

$default reduce using rule 1 (declare)


希望高手指一二.
...全文
153 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Viweei 2010-07-22
  • 打赏
  • 举报
回复
谢谢大家我知道是什么问题了,是我傻了....呵呵.
ForestDB 2010-07-22
  • 打赏
  • 举报
回复
只能顶。
xero_123 2010-07-22
  • 打赏
  • 举报
回复
就yacc和lex来看好像并不存在问题啊?
baihacker 2010-07-21
  • 打赏
  • 举报
回复
没有问题吧。
3读入;转到5,完成归约,然后递归。
如果在一个declare后没有串了,就是state2进入state4的过程,然后accept。
这是分析多个declare的,而不是一个啊。
localxiao 2010-07-21
  • 打赏
  • 举报
回复
好久没碰yacc lex

一脸茫然了。。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧