Motherboard Forums


Reply
Thread Tools Display Modes

Lex and yacc vs GNU readline

 
 
Neil McNulty
Guest
Posts: n/a
 
      08-07-2011, 04:01 AM
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?
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      08-07-2011, 06:12 AM
On Sat, 06 Aug 2011 21:01:54 -0700, Neil McNulty wrote:

> 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?


Simplified version:

#define YY_INPUT(buf,result,max_size) result = get_input(buf, max_size);

static int get_input(char *buf, int size)
{
char *line;
if (feof(yyin))
return YY_NULL;
line = readline("> ");
if (!line)
return YY_NULL;
if (strlen(line) > size - 2) {
error("input line too long");
return YY_NULL;
}
strcpy(buf, line);
strcat(buf, "\n");
free(line);
add_history(buf);

return strlen(buf);
}

A more complex version would avoid the "line too long" error.

 
Reply With Quote
 
 
 
 
Andrew Smallshaw
Guest
Posts: n/a
 
      08-07-2011, 06:25 AM
On 2011-08-07, Neil McNulty <(E-Mail Removed)> wrote:
> 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.

--
Andrew Smallshaw
(E-Mail Removed)
 
Reply With Quote
 
George Neuner
Guest
Posts: n/a
 
      08-08-2011, 03:05 PM
On Sun, 7 Aug 2011 06:25:17 +0000 (UTC), Andrew Smallshaw
<(E-Mail Removed)> wrote:

>On 2011-08-07, Neil McNulty <(E-Mail Removed)> wrote:
>> I can't find anything online about using GNU readline as a front end

>
>There are a few gotchas to watch ...


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.

George
 
Reply With Quote
 
Andrew Smallshaw
Guest
Posts: n/a
 
      08-08-2011, 05:13 PM
On 2011-08-08, George Neuner <(E-Mail Removed)> wrote:
> On Sun, 7 Aug 2011 06:25:17 +0000 (UTC), Andrew Smallshaw
><(E-Mail Removed)> wrote:
>
>>On 2011-08-07, Neil McNulty <(E-Mail Removed)> wrote:
>>> I can't find anything online about using GNU readline as a front end

>>
>>There are a few gotchas to watch ...

>
> 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.

--
Andrew Smallshaw
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LEX 700XL manual or specs for upgrading? bobsamps Gateway 0 06-24-2011 05:36 PM
lex/yacc efficiency tradeoffs D Yuniskis Embedded 19 08-26-2010 09:51 PM
Looking fro Flex , Yacc type tool suitable for embedded work. Anton Erasmus Embedded 2 04-15-2005 12:03 AM
Re: GNU cross compiler for 68000 target on Windows host -- GNU X-TOOLS for 68K is now avalible from ETN farid Embedded 0 12-28-2004 08:18 AM
small "readline" routine? Grant Edwards Embedded 9 01-10-2004 09:54 PM


All times are GMT. The time now is 01:34 PM.


Welcome!
Welcome to Motherboard Point
 

Advertisment