yacc学习笔记(2) 2013.05.13

拿的还是一本书的例子, 为了更方便研究,还把它改了下
% option main 

% {

#include 
< stdlib.h >
#include 
< string .h >

% }
%%

[\t ]
+          /*  ignore white space  */  ;

go        { printf(
" %s: is a verb\n " , yytext); }

[a
- zA - Z] +      { printf( " %s: is not a verb\n " , yytext); }

.
| \n        { ECHO;  /*  normal default anyway  */  }

%%

lex拥有一套简单的消除歧义的规则,

1. lex只匹配输入字符或字符串一次.
2. lex执行当前输入最长可能匹配的动伤. 假设如果输入 gohead , gohead 匹配到 [a-zA-Z]+ 和 go 这两条规则, 但 [a-zA-Z]+ 是更长的匹配动作, 所以输入 gohead 应输出  is not a verb 

如果把 [a-zA-Z]+  去掉,
(1)输入  gohead 那么 go 会匹配到,  head  不作处理
输出如下:
go: is a verb
head

(2)输入 headgo
输出如下:
headtogo
headtogo: is a verb
连 head 都输出来认为匹配了 go, 输出了 is a verb.....

你可能感兴趣的:(yacc学习笔记(2) 2013.05.13)