c - Lex regex gets some extra characters -
i have next definition in lex file:
l [a-za-z_] [a-za-z_0-9] %% {l}{a}* { yylval.id = yytext; homecoming identifier; }
and next in yacc file:
primary_expression : identifier { puts("identifier: "); printf("%s", $1); }
my source code (the 1 i'm analyzing) has next assignment:
ab= 10;
for reason, printf("%s", $1);
part printing ab=
, not ab
.
i'm pretty sure that's section printing ab=
because when delete printf("%s", $1);
identifier not printed @ all.
i ran out of ideas. doing wrong?
let me know if can more clear.
what doing wrong?
you're assuming string pointed yytext
constant. not.
the lifetime of string pointed yytext
lexical action of associated rule. if rule ends returning, yytext
survive until next time yylex
called. , that's it.
bison
-generated parsers have one-symbol lookahead. time parser executes semantic action, yylex
has been called 1 time again (for lookahead); consequently, can't utilize saved value of yytext
lastly (or only) token in rule.
solution: re-create string. (i utilize strdup
, whatever reason people malloc , strcpy. if do, don't forget nul terminator.) , remember free()
re-create when you're done it.
for reference: what flex manual says.
c bison yacc flex-lexer lex
No comments:
Post a Comment