1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Lex and yacc vs GNU readline

Discussion in 'Embedded' started by Neil McNulty, Aug 7, 2011.

  1. Neil McNulty

    Neil McNulty Guest

    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?
     
    Neil McNulty, Aug 7, 2011
    #1
    1. Advertising

  2. Neil McNulty

    Nobody Guest

    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.
     
    Nobody, Aug 7, 2011
    #2
    1. Advertising

  3. On 2011-08-07, 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?


    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
     
    Andrew Smallshaw, Aug 7, 2011
    #3
  4. On Sun, 7 Aug 2011 06:25:17 +0000 (UTC), Andrew Smallshaw
    <> wrote:

    >On 2011-08-07, Neil McNulty <> 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
     
    George Neuner, Aug 8, 2011
    #4
  5. On 2011-08-08, George Neuner <> wrote:
    > On Sun, 7 Aug 2011 06:25:17 +0000 (UTC), Andrew Smallshaw
    ><> wrote:
    >
    >>On 2011-08-07, Neil McNulty <> 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
     
    Andrew Smallshaw, Aug 8, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant Edwards

    small "readline" routine?

    Grant Edwards, Jan 7, 2004, in forum: Embedded
    Replies:
    9
    Views:
    329
    Grzegorz Mazur
    Jan 10, 2004
  2. farid
    Replies:
    0
    Views:
    545
    farid
    Dec 28, 2004
  3. Anton Erasmus
    Replies:
    2
    Views:
    399
    Roberto Waltman
    Apr 15, 2005
  4. D Yuniskis

    lex/yacc efficiency tradeoffs

    D Yuniskis, Jul 23, 2010, in forum: Embedded
    Replies:
    19
    Views:
    1,621
    George Neuner
    Aug 26, 2010
  5. bobsamps
    Replies:
    0
    Views:
    1,147
    bobsamps
    Jun 24, 2011
Loading...

Share This Page