yacc/bison input from a string rather than stdin

Hi,

I am using yacc/bison to write a parser (using lex/flex).
The entry point is yyparse () but this parses the stdin.
I have a string to parse pointed to by p_string. How do I
tell yyparse to parse the string and not wait for stdin?
agjAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mlevConnect With a Mentor Commented:
Either write your own YY_INPUT to read from the string,
Or (ugly, but easy) create a pipe, write the string into its one end, and feed the other end to lex.
0
 
agjAuthor Commented:
you're talking about lex here, I am talking about yacc.
how do you get yacc to parse a string? if your answer still
stands can you give me a little more detail about how to
write YY_INPUT?
thanks.
0
 
ahoffmannCommented:
yacc/bison use yylex() as input function, you may use your own one instead; just define it in you code.
See your  /usr/lib/bison.{simple,hairy}  for some details. AFAIK there is also a section about private yylex() in the bison info files.
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

 
duneramCommented:
I have a good way to solve this as I have written 12 languages using lex/yacc.

I would post this as an answer, but its already awaiting a reply, but if the other doesn't help and this does, let me know and I can submit it as an answer.

The best and easiest way to solve this involves using freopen().  That's how I solved it.  You can associate stdin to a filename instead of having to be the tty / keyboard.  In your driver, let a command line arg be the file that will be parsed (assign that file to stdin via freopen().  You can do the same thing with stderr and stdout.   I ended up reassigning all three so I could massage the output and easily read from my modified stdin.

David
0
 
mlevCommented:
Yacc doesn't read any input itself, it calls lex to do that.

Assuming you initialise p_len to strlen(p_string) and p_off to 0,
the following works with flex:
#define YY_INPUT(buf,result,max_size) \
        { \
                if (p_off >= p_len) \
                        result = YY_NULL; \
                else { \
                        if (max_size < p_len - p_off) \
                                result = max_size; \
                        else \
                                result = p_len - p_off; \
                        strncpy(buf, p_string + p_off, result); \
                        p_off += result; \
                } \
        }

0
 
ahoffmannCommented:
mlev, t doesn't call lex, it calls a function yylex().
Anyway, your YY_INPUT will do the job ;-)
0
 
mlevCommented:
ahoffmann: If you want to be accurate, you might say that
yacc doesn't call anything at all, it produces code that does :)
0
 
ahoffmannCommented:
mlev, should we break down to electrical signal?
Anyway, I agree ;-)

I meant that you don't need lex to use (compile and run) yacc programs, while your answer suggested that yacc needs lex routines.
Sorry for being to accurate to you and less precise myself.
0
 
duneramCommented:
If you decided to use the freopen method.

it would be a bit of a highlevel workaruound but it would work very painlessly.

1 Create a file.
2) write the contents of your buffer to the file
3) close the file
4) use freopen to associate stdin with the name of the file you just created.
5) start the parsing

pretty simple.
0
 
duneramCommented:
using freopen would null out the need to change any generated code.  changing generated code is a bit of a hassle as you have to make sure you re-change it everytime you regenerate.
0
 
mlevCommented:
Agreed. I haven't noticed changing generated code proposed here.
0
All Courses

From novice to tech pro — start learning today.