LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2011, 07:13 AM   #1
King_DuckZ
Member
 
Registered: Nov 2009
Location: Rome, IT
Distribution: Sabayon
Posts: 61

Rep: Reputation: 2
String buffer with flex and bison


Hello, I'm trying to write a parser for a simple custom language, and I decided to try the well-known combo flex/bison.
I found a lot of examples on how to use them, but I'm a bit confused on how to read data from memory instead than from a file.

From what I've seen, yy_scan_buffer is what I'm looking for. Now my question is: yy_scan_buffer is declared in the lexer file, but the main program will call yyparse, no? So how do I tell yyparse what buffer it should use? And even then, the parser has no prototypes for lexer's functions, so how do I call yy_scan_buffer from there?
I hope someone can help me!
 
Old 11-29-2011, 11:11 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Look here. `yy_switch_to_buffer()' is what you are looking for.

Example:
Code:
%%
[[:alnum:]]+	printf("[%s]\n", yytext);
%%
int main()
{
	char buf[] = "this is a test string\0\0";
	YY_BUFFER_STATE bs = yy_scan_buffer(buf, sizeof(buf));
	yy_switch_to_buffer(bs);
	yylex();
}
Flex can generate a header file with prototypes for you:
Code:
flex  --header-file=lex.h ex1.
This will produce `lex.h' file. You can include it and use `yy_switch_to_buffer()' etc. in bison file.

Hope this helps.

Last edited by firstfire; 11-29-2011 at 11:58 AM.
 
Old 11-30-2011, 03:08 AM   #3
King_DuckZ
Member
 
Registered: Nov 2009
Location: Rome, IT
Distribution: Sabayon
Posts: 61

Original Poster
Rep: Reputation: 2
Thanks firstfire, that's exactly what I needed! I'll put that to good use
I don't know how to mark the thread as solved, tho. If I click edit I can't change the title anyways.
 
Old 11-30-2011, 05:06 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

To mark thread as solved click on the 'Thread Tools' button on top of the page and then 'Mark as solved'.
 
  


Reply

Tags
bison, flex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Flex & Bison] How to check which state Flex is in? courteous Programming 0 06-03-2011 11:46 AM
Is there any support for bison-bridge and bison-locations in flex on windows systems? rami alkhateeb Linux - Software 0 12-29-2010 09:10 AM
installing bison and flex rameshsena Linux - Software 10 06-21-2010 11:33 AM
flex and bison saurav.nith Linux - General 1 04-06-2010 06:38 AM
bison / flex zaman Programming 1 08-16-2005 10:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration