野牛令牌是字符串的其余部分

编程入门 行业动态 更新时间:2024-10-27 15:22:52
本文介绍了野牛令牌是字符串的其余部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我写了一个flex和野牛, 我遇到了一个问题,该问题可以通过以下程序进行说明.

I have written a flex and bison, I am facing a problem which is illustrated via the below program.

该程序用于解析由等号(=)分隔的键/值对 我希望我的野牛脚本能将键和值标记化并打印出来.

The program is intended to parse the key-value pairs separated by an equals (=) sign I am hoping that my bison script tokenizes the key and values and prints them.

下面是我的flex程序的代码段

Below is the snippet of my flex program

%{ /* file : kvp.l */ #include <stdio.h> #define YYSTYPE char* #include "kvp.tab.h" %} %% [a-zA-Z0-9][_a-zA-Z0-9]* { yylval=yytext; return IDENTIFIER; } "=" { yylval=yytext; return EQUALS_OP; } . ; /* Do Nothing */ %% int yywrap() { return 1; }

下面是Bison程序

%{ /* file : kvp.y */ #include <stdio.h> #include <stdlib.h> /* interface to the lexer */ int yylex(); int yyerror (char const *s); %} %token IDENTIFIER EQUALS_OP %start stmt %% stmt: stmt pair | pair; pair: IDENTIFIER EQUALS_OP IDENTIFIER { printf("YACC : Key = \"%s\"\n", $1); printf("YACC : Equals = \"%s\"\n", $2); printf("YACC : Value = \"%s\"\n", $3); }; %% int yyerror (char const *s) { fprintf (stderr, "Error String = \"%s\"\n", s); } int main( int argc, char* argv[]) { yyparse(); return 0; }

我从该程序中得到的结果如下.

The result which I am getting out of this program is as below.

student@debian:~/stack-overflow$ flex kvp.l student@debian:~/stack-overflow$ bison -d --file-prefix=y kvp.y student@debian:~/stack-overflow$ gcc lex.yy.c y.tab.c -o parser student@debian:~/stack-overflow$ ./parser earth=planet YACC : Key = "earth=planet" YACC : Equals = "=planet" YACC : Value = "planet"

可以看到正在打印的令牌是不正确的,所以从令牌开始的整个字符串都被打印了. 请告诉我该程序在哪里出错.

As one can see the tokens which are being printed is not right, the entire string beginning from the token is being printed. Please tell me where I am going wrong in this program.

推荐答案

问题是您正在保存指向yytext的指针,该指针随后在Flex扫描仪中进行了更新.如果将其更改为将字符串的副本保存在yytext中,则将获得更一致的结果,例如,

The problem is that you are saving a pointer to yytext, which is later updated in the flex scanner. If you change that to save a copy of the string in yytext, you will get more consistent results, e.g.,

yylval = strdup(yytext);

而不是

yylval = yytext;

进一步阅读:

  • 回复:[Flex-help] yytext返回为char **
  • 21.3关于yytext和内存的注释
  • Re: [Flex-help] yytext return as char*
  • 21.3 A Note About yytext And Memory

更多推荐

野牛令牌是字符串的其余部分

本文发布于:2023-10-29 22:47:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:野牛   令牌   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!