Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

yacc/bison input from a string rather than stdin

Posted on 1998-07-15
11
Medium Priority
?
1,297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 2

Accepted Solution

by:
mlev earned 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.

618 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