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);
}
_______________________________________________
help-bison@gnu.org 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
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison