Re: Redirect stdin ?





Lothar Behrens wrote:
Hi,

I have a c application (Lex&Yacc based) that parses from stdin. I want
to rewrite the code
a bit to allow reading the source from a char array instead from stdin
without touching the
lex and yacc generated source code.

How could I do that ?

lex uses three macros that can be #defined by the user named 'input',
'unput' and 'output'. It is designed with the intention that you can
override those macros as needed to define different input sources. I
don't know if that meets your needs; it requires a change in the
source code you give to lex, which means that lex will generate
different output code, which is technically a violation of your
requirements. Depending upon the reasons for your requirements, it
might be better to modify your requirements than to use the more
complicated approaches needed to conform to those requirements.

What is about the problem when the buffer is too big to feed the
source to the parser at once ?

Your 'input' macro could refer to a function that stores only as much
of the file as can actually fit into your buffer at any given time. As
long as your buffer is a least big enough to hold the longest look-
ahead required by your grammar, that shouldn't be a problem.
.



Relevant Pages

  • Re: Redirect stdin ?
    ... lex and yacc generated source code. ... override those macros as needed to define different input sources. ... of the file as can actually fit into your buffer at any given time. ...
    (comp.lang.c)
  • Must stdin, stdout, and stderr be macros?
    ... Are stdin, stdout, and stderr required to be macros? ...
    (comp.std.c)
  • Re: Files & dirs: historical reasons?
    ... stdin, stdout and stderr, are macros ... which expand to expressions of type FILE *. ... associated macros. ... Requiring that FILE ...
    (comp.lang.c)
  • Re: stdin ? stdin ? Whats wrong with stdin?
    ... >> when the CRT initializes a console application, they are not macros. ... >> Have you included the proper header file in the module that is reporting ... > stdin, etc. are not visible in the debugger because they are macros. ...
    (microsoft.public.vc.language)
  • Re: Files & dirs: historical reasons?
    ... I don't believe that the standard requires that they be macros. ...
    (comp.lang.c)