Re: Redirect stdin ?



On Dec 31, 7:00 pm, jameskuy...@xxxxxxxxxxx wrote:
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.


I do understand these macros as callbacks where I could do the
feeding.

My requirements were not to modify the generated code from lex and
yacc to
get a workaround (as of I wouldn't be aware of Lex & Yacc a bit)

Currently I have changed the grammar. There was a problem with the
supported
syntax of my SQL code. So I tend to change it as you supposed.

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.

As of the case I handle SQL statements, the lookahead buffer may be
big enough,
when I allocate 100 bytes. That will be enough for a small query.

In case not, where do I see the size of this buffer ?

Thank you,

Lothar
.



Relevant Pages

  • Re: Redirect stdin ?
    ... I have a c application that parses from 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)
  • Re: Redirect stdin ?
    ... get a workaround (as of I wouldn't be aware of Lex & Yacc a bit) ... generated code from lex and yacc". ... One interpretation is that you do not want to hand-modify the C code ... yacc would generate would take the supplied input routine into account. ...
    (comp.lang.c)
  • Re: A Readable C Code without #DEFINEs ...
    ... you feel the need to actually modify the lex or yacc ... your design that prevent you from using these tools correctly. ...
    (comp.lang.c.moderated)
  • Re: A Readable C Code without #DEFINEs ...
    ... > I am using LEX and YACC and they produce this grossly unreadable C code ... Hopefully the input files are readable, ... > Is there a switch is C, or, if you know about a hidden switch in either ...
    (comp.lang.c.moderated)
  • Re: How is a compiler written
    ... >> The Dragon Book is a good one suggestion, ... >> best way to write a compiler is with lex and yacc is not. ...
    (comp.lang.c)