tags:

views:

97

answers:

2

i want to parse from a string rather than a file. i know that v can use yy_scan_string fn to do it.but for me it's not working properly so pls help me

A: 

I fought through this problem myself very recently. The flex documentation on the subject leaves a bit to be desired.

I see two things right off the bat that might be tripping you up. First, note that your string needs to be double NULL terminated. That is, you need to take a regular, NULL terminated string and add ANOTHER NULL terminator at the end of it. That fact is buried in the flex documentation, and it took me a while to find as well.

Second, you've left off a call to "yy_switch_to_buffer". This is also not particularly clear from the documentation. If you change your code to something like this, it should work.

// add the second NULL terminator
int len = strlen(my_string);
char *temp = new char[ len + 2 ];
strcpy( temp, my_string );
temp[ len + 1 ] = 0; // The first NULL terminator is added by strcpy

YY_BUFFER_STATE my_string_buffer = yy_scan_string(temp); 
yy_switch_to_buffer( my_string_buffer ); // switch flex to the buffer we just created
yyparse(); 
yy_delete_buffer(my_string_buffer );
Russell Newquist
nope it's not working. it says YY_BUFFER_STATE is undeclared
ajai
(F)lex and Yacc/Bison are often used in C, less often in C++. Also, you should probably avoid calling `strlen()` twice on the same string.
Chris Lutz
Yes, I'm aware of the strlen issue... I suppose I should've taken the time to optimize my example code. ;)
Russell Newquist
A: 

@rRussell :I used your code dude but it says "YY_BUFFER_STATE" is not declared. so i declared it as extern in yacc yet it's not working Also i am working in solaris .So do i have to link any libraries during compile time.

ajai
Since you used the YY_BUFFER_STATE variable in your own code and it crashed, I'm confused as to how it compiled in the first place and you got as far as you did. However, you can use the "--header-file=xxx" command line option to generate a header file that will define these variables that you can then include in your source.
Russell Newquist
it didn't compile dude...i got the error "YY_BUFFER_STATE" is not declared during compilation itelf.
ajai