Solved

yacc/bison input from a string rather than stdin

Posted on 1998-07-15
11
1,206 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

806 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