Beta033
Beta033

Reputation: 2013

Lex Yacc, should i tokenize character literals?

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

Upvotes: 0

Views: 911

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126175

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.

Upvotes: 1

Related Questions