Solved

yacc/bison input from a string rather than stdin

Posted on 1998-07-15
11
1,186 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 116
Using unmanaged DLL from managed application 14 288
negation in C function 14 132
Unable to start eclipse ? 17 87
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now