help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get the actual string for a symbol?


From: Philip Herron
Subject: Re: How to get the actual string for a symbol?
Date: Thu, 31 Dec 2009 05:23:45 +0000
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0

On 31/12/2009 05:11, Peng Yu wrote:
I want to get words from a file and print them out. But I'm not sure
how to make yylval a pointer to the string yytext. Could somebody let
me know how to modify the following code so that the string can be
retrieved the symbol WORD.

yylval_string.l
%option nodefault
%{
# include "yylval_string.tab.h"
%}
%%
[A-Za-z]+ { /*I'm not sure how to make yylval a point to yytext */
return WORD; }
[[:space:]] { /*SPACE*/ }
.       {
  return UNEXPECTED_CHARACTER;
  /*fprintf(stderr, "unexpeced_character: '%s'\n", yytext);*/
  }
%%
yylval_string.y
%{
#  include<stdio.h>
%}
%token WORD UNEXPECTED_CHARACTER
%%
words:
        WORD { printf("WORD = %s\n", $1); }
        | words WORD { printf("WORD = %d\n", $2); }
  ;
%%
main()
{
   yyparse();
}

yyerror(char *s)
{
   fprintf(stderr, "error: %s\n", s);
}


_______________________________________________
address@hidden http://lists.gnu.org/mailman/listinfo/help-bison
I am fairly sure this stuff is well documented now, just going to post an example from one of my projects...

Lexer:

ID       [_a-zA-Z][a-zA_Z0-9_]*

{ID}                    {
                          yylval.string= xstrdup( yytext );
                          return IDENTIFIER;
                        }

Parser:

union {
  char *string;
  ....
}

%token <string>IDENTIFIER // now points to whatever :)

You might want to think about the difference between keywords and static data a little more too. And terminals or non-terminals how it all fits.

--Phil





reply via email to

[Prev in Thread] Current Thread [Next in Thread]