如何在yacc中对字符串使用yylval [英] How to use yylval with strings in yacc

查看:439
本文介绍了如何在yacc中对字符串使用yylval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递令牌的实际字符串.如果我有一个名为ID的令牌,那么我希望我的yacc文件实际知道称为什么ID.我必须将使用yylval的字符串从flex文件传递到yacc文件.我该怎么办?

I want to pass the actual string of a token. If I have a token called ID, then I want my yacc file to actually know what ID is called. I thing I have to pass a string using yylval to the yacc file from the flex file. How do I do that?

推荐答案

请参见 Flex与YACC交互的手动部分.

15与Yacc接口

flex的主要用途之一是 yacc的同伴 解析器生成器. yacc解析器期望 调用名为yylex()的例程 查找下一个输入令牌.例行 应该返回的类型 下一个令牌以及放置任何令牌 全局yylval中的关联值. 要将yacc与flex一起使用,请指定 yacc的-d选项指示它 生成文件y.tab.h 包含所有 %tokens出现在yacc输入中. 该文件然后包含在flex中 扫描器.例如,如果 令牌是TOK_NUMBER,是 扫描仪可能看起来像:

15 Interfacing with Yacc

One of the main uses of flex is as a companion to the yacc parser-generator. yacc parsers expect to call a routine named yylex() to find the next input token. The routine is supposed to return the type of the next token as well as putting any associated value in the global yylval. To use flex with yacc, one specifies the `-d' option to yacc to instruct it to generate the file y.tab.h containing definitions of all the %tokens appearing in the yacc input. This file is then included in the flex scanner. For example, if one of the tokens is TOK_NUMBER, part of the scanner might look like:

     %{
     #include "y.tab.h"
     %}

     %%

     [0-9]+        yylval = atoi( yytext ); return TOK_NUMBER;

这篇关于如何在yacc中对字符串使用yylval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆