複数の規則は「|」でつなげ!
どうやら複数の規則は「|」でつないで書かないとダメらしい
つまり以下の記述はダメ
%% input: #empty | input line ; line: '\n' { $_[1] } line: exp '\n' { print "$_[1]\n" } line: error '\n' { $_[0]->YYErrok } ; exp: NUM ; %% sub _Error { exists $_[0]->YYData->{ERRMSG} and do { print $_[0]->YYData->{ERRMSG}; delete $_[0]->YYData->{ERRMSG}; return; }; print "Syntax error.\n"; } sub _Lexer { my($parser)=shift; $parser->YYData->{INPUT} or $parser->YYData->{INPUT} = <STDIN> or return('',undef); $parser->YYData->{INPUT}=~s/^[ \t]//; for ($parser->YYData->{INPUT}) { s/^([0-9]+(?:\.[0-9]+)?)// and return('NUM',$1); s/^(.)//s and return($1,$1); } } sub Run { my($self)=shift; $self->YYParse( yylex => \&_Lexer, yyerror => \&_Error ); } my($t)=new Test; $t->Run;
で、変換時、以下のエラー
*Error* Unexpected input: ':', at line 14. *Fatal* Errors detected: No output, at eof.
yacc だと「|」でつないで書かかなくても大丈夫
%{ #include <stdio.h> void yyerror(char *s); int yylex(void); %} %token NUM %% input : | input line ; line : '\n' line : exp '\n' { printf("%d\n", $1); } line : error '\n' { } exp : NUM ; %% void yyerror(char *s) { printf("%s\n", s); } int yylex() { int c = getchar(); if (c >= '0' && c <= '9') { yylval = c - '0'; return NUM; } else { return c; } } int main() { return yyparse(); }