开发者

Lex Yacc, should i tokenize character literals?

开发者 https://www.devze.com 2023-02-02 11:07 出处:网络
I know, poorly worded question not sure how else to ask though. I always seem to end up in the error branch regardless of what i\'m entering and can\'t figure out where i\'m screwing this up. i\'m us

I know, poorly worded question not sure how else to ask though. I always seem to end up in the error branch regardless of what i'm entering and can't figure out where i'm screwing this up. i'm using a particular flavor of Lex/YACC called GPPG which just sets this all up for use with C#

Here is my 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*/ }          
            ;

here's my Lex

\'[^']*\'           {this.yylval.Data = yytext.Replace("'",""); return (int)Tokens.L_VALUE;}
[a-zA-Z0-9]+\(      {this.yylval.Data = 开发者_JAVA百科yytext; return (int)Tokens.L_METHOD;}

The idea is that i should be able to pass Method('value') to it and have it properly recognize that this is correct syntax

ultimately the plan is to execute the Method passing the various parameters as values

i've also tried several derivations. for example:

 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;}


You need a lex rule to return the punctuation tokens 'as-is' so that the yacc grammar can recognize them. Something like:

[()]        { return *yytext; }

added to your second example should do the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消