Solved

yacc/bison input from a string rather than stdin

Posted on 1998-07-15
11
1,235 Views
Last Modified: 2011-09-20
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?
0
Comment
Question by:agj
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 2

Accepted Solution

by:
mlev earned 200 total points
ID: 1251710
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
 

Author Comment

by:agj
ID: 1251711
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1251712
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:duneram
ID: 1251713
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
 
LVL 2

Expert Comment

by:mlev
ID: 1251714
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1251715
mlev, t doesn't call lex, it calls a function yylex().
Anyway, your YY_INPUT will do the job ;-)
0
 
LVL 2

Expert Comment

by:mlev
ID: 1251716
ahoffmann: If you want to be accurate, you might say that
yacc doesn't call anything at all, it produces code that does :)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1251717
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
 
LVL 2

Expert Comment

by:duneram
ID: 1251718
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
 
LVL 2

Expert Comment

by:duneram
ID: 1251719
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
 
LVL 2

Expert Comment

by:mlev
ID: 1251720
Agreed. I haven't noticed changing generated code proposed here.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question