日期:2014-05-16  浏览次数:20596 次

yacc做shell的命令分析疑问
一下是个命令生成树的顶端,第一个选择应该可以省略掉管道符"|"的,为何移除会判错呢?
%%
CommandLine:
|CommandLine '\n' {newline()}
|CommandLine Command '\n' {Command(),newline()}

....

------解决方案--------------------
CommandLine: /* empty */
|CommandLine '\n' {newline()}
|CommandLine Command '\n' {Command(),newline()}
;

推出空串这么写就行,注释那地方就表示推出空串了。
yacc里epsilon(空串)空着就行了。如果但是第一个分支前去掉‘|’,文法就不能推出空串了。
我看《flex与bison》那本书学会的,推荐你看看。