yacc/lex 的基本问题

发布于 2024-09-11 18:34:53 字数 1273 浏览 6 评论 0原文

我对一个非常简单的 yacc/lex 程序有一些问题。我可能忘记了一些基本步骤(我已经很长时间没有使用这些工具了)。

在我的 lex 程序中,我给出了一些基本值,例如:

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

在我的 yacc 程序中,我的语法的开头是(其中 TranslationUnit 是我的 %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 */

当我使用输入运行程序时:

keyword : value

我认为我至少会得到:

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

实际上我得到:

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

我不明白这个结果。我前段时间学过语法,即使我现在不记得所有内容,我也没有看到任何重要的错误......

提前感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2024-09-18 18:34:53

问题在于,除非您保存令牌,否则 Lex/Yacc 会继续覆盖该空间,或指向不同的空间等。因此,您需要在修改之前存储对您至关重要的信息。您在 Lex 代码中的打印应该表明,当调用词法分析器(词法分析器)时,yylval 值是准确的。

另请参阅 SO 2696470,其中遇到并诊断了相同的基本问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文