简体   繁体   English

如何将API从lex yacc设置为Program

[英]How to set the API from lex yacc to Program

I create an .exe FILE, which can parser an expression, which is generated by lex and yacc. 我创建了一个.exe文件,该文件可以解析由lex和yacc生成的表达式。 But I do it just get the input from screen, and just return the parser result from screen. 但是我只是从屏幕上获取输入,然后从屏幕上返回解析器结果。 I saw some suggestions about using YY_BUFFER_STATE yy_scan_buffer(char *base, yy_size_t size) , but I still could not find a good way to do it. 我看到了一些有关使用YY_BUFFER_STATE yy_scan_buffer(char *base, yy_size_t size) ,但是我仍然找不到找到它的好方法。

Is it possible that I put some headers (which is compiled by lex yacc) to my main program c++, and then I can use yylex() to call it, giving a string as input, and get the return value in the main program? 是否可以将一些标头(由lex yacc编译)放入我的主程序c ++,然后可以使用yylex()对其进行调用,并提供一个字符串作为输入,并在主程序中获取返回值? Thanks for your help, I am confused about how to realize it. 感谢您的帮助,我对如何实现它感到困惑。 Thanks. 谢谢。

yy_scan_string is how you give flex a string as input. yy_scan_string是如何为flex提供一个字符串作为输入。 You call that first, and then call yylex and it will use that string as the input to get tokens from rather than stdin . 您首先调用它,然后调用yylex ,它将使用该字符串作为输入来获取令牌,而不是从stdin When you get an EOF from yylex , it has scanned the entire string. 当您从yylex获得EOF时,它将扫描整个字符串。 You can then call yy_delete_buffer on the YY_BUFFER_STATE returned by yy_scan_string (to free up memory) and call yy_scan_string again if you want to scan a new string. 然后,您可以在yy_scan_string返回的YY_BUFFER_STATE上调用yy_delete_buffer (以释放内存),如果要扫描新字符串,则可以再次调用yy_scan_string

You can use yy_scan_buffer instead to save a bit of copying, but then you have to set up the buffer properly yourself (basically, it needs to end with two NUL bytes instead of just one). 您可以使用yy_scan_buffer来节省一些复制,但是您必须自己正确设置缓冲区(基本上,它需要以两个 NUL字节而不是一个字节结尾)。

Unfortunately, there's no standard header file from flex declaring these. 不幸的是,flex没有声明这些的标准头文件。 So you need to either declare them yourself somewhere (copy the declarations from the flex documentation), or call them in the 3rd section of the .l file, which is copied verbatim to the end of the lex.yc file. 因此,您需要自己声明它们(复制flex文档中的声明),或在.l文件的第3部分中调用它们,该文件逐字复制到lex.yc文件的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM