I can't find anything online about using GNU readline as a front end for an interactive parser using lex and yacc. I would have thought this would be a fairly common thing to do. Does anyone know how I might do this?
Yacc is nothing to do with it: readline will be talking to lex and only to lex. What lex then talks to is an irrelevance.
However, this kind of thing is difficult to impossible to implement using a generic lex that expects input via a file. Input from a buffer is not standardised, so you really need to use the specific mechanism of whatever lex you are using.
Flex, for example, allows you to define a YY_INPUT macro (which will probably end up as a wrapper around a function) to copy from the buffer returned by readline() into the destination buffer indicated as a macro parameter.
There are a few gotchas to watch - the history needs maintaining manually and readline annoyingly strips newlines before you see them
- but it is all relatively easily contained in the definitions section of your lexer (the bit between %{ and %} ). I did this a few months ago - it shouldn't be too difficult to find if you want a concrete example.
One further "gotcha" (if it matters) is that GNU readline is that, although a library, readline is not licenced under LGPL but is instead a viral component licensed under GPL v3.
If that is an issue there is always libedit, which ISTR originates as part of the NetBSD distribution. That is source-compatible with GNU readline but BSD licensed. Only half the size too.
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.