开发者

Basic problem with yacc/lex

开发者 https://www.devze.com 2023-01-08 20:53 出处:网络
I have some problems with a very simple yacc/lex program. I have maybe forgotten some basic steps (it\'s been a long time since I\'ve used these tools).

I have some problems with a very simple yacc/lex program. I have maybe forgotten some basic steps (it's been a long time since I've used these tools).

In my lex program I give some basic values like :

word    [a-zA-Z][a-zA-Z]*
%%
":"    return(PV);
{word}  { 
            yylval = yytext;
            printf("yylval = %s\n",yylval);
            return(WORD);
       }
"\n"    return(ENDLINE);

In my yacc program the beginning of my grammar is (where TranslationUnit is my %start) :

TranslationUnit:
               /* Nothing */
              | InfoBlock Data
              ;

InfoBlock:
           /* Nothing */
         | InfoBlock InfoExpression {}
         ;

InfoExpression:
             WORD PV WORD ENDLINE { printf("$1 = %s\n",$1);
 printf("$2 = %s\n",$2);
 printf("$3 = %s\n",$3);
 printf("$4 = %s\n",$4);
                                  }
            | ... /* other 开发者_如何转开发things */
            ;

Data:
    ... /* other things */

When I run my program with input :

keyword : value

I thought that I would get at least :

$1 = keyword
$2 = keyword // yylval not changed for token PV
$3 = value
$4 = value // yylval not changed for token ENDLINE

Actually I get :

$1 = keyword : value
$2 = keyword : value
$3 = value
$4 = value

I do not understand this result. I have studied grammars some time ago and even if I don't remember everything right now, I do not see any important mistake...

Thanks in advance for your help.


The trouble is that unless you save the token, Lex/Yacc goes on to over-write the space, or point to different space, etc. So you need to stash the information that's crucial to you before it gets modified. Your printing in the Lex code should have showed you that the yylval values were accurate at the point when the lexer (lexical analyzer) was called.

See also SO 2696470 where the same basic problem was encountered and diagnosed.

0

精彩评论

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

关注公众号