我知道,这个问题表达得很糟糕,我不知道还能怎么问。不管我输入的是什么,我似乎总是在error分支中结束,并且不能弄清楚我把它搞砸了。我使用的是一种特殊风格的Lex/YACC,称为GPPG,它将所有这些都设置为与C#一起使用。
这是我的Y
method : L_METHOD L_VALUE ')' { System.Diagnostics.Debug.WriteLine("Found a method: Name:" + $1.Data ); }
| error { System.Diagnostics.Debug.WriteLine("Not valid in this statement context ");/*Throw new exception*/ }
;这是我的Lex
\'[^']*\' {this.yylval.Data = yytext.Replace("'",""); return (int)Tokens.L_VALUE;}
[a-zA-Z0-9]+\( {this.yylval.Data = yytext; return (int)Tokens.L_METHOD;}我的想法是,我应该能够将Method('value')传递给它,并让它正确地识别出这是正确的语法
最终,计划是执行Method,将各种参数作为值传递
我还尝试了几种派生方法。例如:
method : L_METHOD '(' L_VALUE ')' { System.Diagnostics.Debug.WriteLine("Found a method: Name:" + $1.Data ); }
| error { System.Diagnostics.Debug.WriteLine("Not valid in this statement context: ");/*Throw new exception*/ }
;
\'[^']*\' {this.yylval.Data = yytext.Replace("'",""); return (int)Tokens.L_VALUE;}
[a-zA-Z0-9]+ {this.yylval.Data = yytext; return (int)Tokens.L_METHOD;}发布于 2011-01-06 10:00:53
您需要一个lex规则来“按原样”返回标点符号,以便yacc语法能够识别它们。类似于:
[()] { return *yytext; }添加到您的第二个示例中应该可以做到这一点。
https://stackoverflow.com/questions/4610967
复制相似问题