字符串输入到Flex词法分析器 [英] String input to flex lexer

查看:809
本文介绍了字符串输入到Flex词法分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建使用Flex /野牛解析器读取-EVAL-打印循环。麻烦的是,弯曲产生的词法分析器想要的类型FILE *的投入,我想这是char *。反正有做到这一点?

I want to create a read-eval-print loop using flex/bison parser. Trouble is, the flex generated lexer wants input of type FILE* and i would like it to be char*. Is there anyway to do this?

一个建议是创建一个管道,喂它字符串和打开文件描述符和发送到词法分析器。这是相当简单的,但感觉费解的独立,而不是十分的平台。有没有更好的办法?

One suggestion has been to create a pipe, feed it the string and open the file descriptor and send to the lexer. This is fairly simple but it feels convoluted and not very platform independent. Is there a better way?

推荐答案

下面的函数可以用来扫描内存字符串而不是文件建立输入缓冲器(如yy_create_buffer一样):

The following routines are available for setting up input buffers for scanning in-memory strings instead of files (as yy_create_buffer does):


  • YY_BUFFER_STATE yy_scan_string(为const char * str中):扫描NULL结尾的string`

  • YY_BUFFER_STATE yy_scan_bytes(为const char *字节,INT LEN):扫描len个字节(包括可能完全无效)起始地址字节

  • YY_BUFFER_STATE yy_scan_string(const char *str): scans a NUL-terminated string`
  • YY_BUFFER_STATE yy_scan_bytes(const char *bytes, int len): scans len bytes (including possibly NULs) starting at location bytes

请注意,这两个函数创建,返回相应YY_BUFFER_STATE手柄(你必须yy_delete_buffer删除()时,用它做),这样函数yylex()扫描字符串或字节的副本。此行为可能是希望的,因为函数yylex()修改缓冲区是扫描)的内容。

Note that both of these functions create, return a corresponding YY_BUFFER_STATE handle (which you must delete with yy_delete_buffer() when done with it) so yylex() scan a copy of the string or bytes. This behavior may be desirable since yylex() modifies the contents of the buffer it is scanning).

如果你想使用避免复制(和yy_delete_buffer):

If you want avoid the copy (and yy_delete_buffer) using:


  • YY_BUFFER_STATE yy_scan_buffer(字符*基地,yy_size_t大小)

  • YY_BUFFER_STATE yy_scan_buffer(char *base, yy_size_t size)

样本主:

int main() {
    yy_scan_buffer("a test string");
    yylex();
}

这篇关于字符串输入到Flex词法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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