help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Distributivity parsing, prompt symbol and line editing


From: Andrej Prsa
Subject: Distributivity parsing, prompt symbol and line editing
Date: Wed, 7 Jul 2004 02:25:06 +0200

Hi!

I'm a total newbie (since two days ago) and I've been struggling with
flex/bison to write a small scripting language. Now I have come to a solid
wall where several hours of documentation-flipping hadn't helped. This is
my problem:

All input lines start with a directive (which is an ordinary word)
followed by one or more comma-separated arguments, e.g.

        directive arg1, arg2, ..., argN '\n'

The directive itself acts only on one argument, so the above line should
reduce (well, not really reduce, distribute really) to:

        directive arg1 '\n'
        directive arg2 '\n'
              . . .
        directive argN '\n'

Now I wonder: is this a job for a scanner or a parser? I tried something
like this for the bison file (I'm giving only the relevant parts):

input   : /* empty */
        | input line    { printf ("> "); }
        ;
line    : '\n'          /* Is it an empty line? */
        | d_line        /* Is it a directive?   */
        ;
d_line  : h_line '\n'
        ;
h_line  : HELP arglist
        ;
arglist : /* no arguments */    { printf ("no args.\n"); }
        | STRARG                { printf ("%s\n", $1);   }
        | arglist ',' STRARG    { printf ("%s\n", $3);   }
        ;

And something like this for the flex file:

help|\?                 return HELP;
{WORD}                  {
                        yylval.str = strdup (yytext);
                        return STRARG;
                        }
{NEWLINE}               return '\n';
{WSPACE}+
.                       return yytext[0];


My question is now this: what would be the proper way to implement
distributivity reduction? Or should I define a global string variable
carrying the directive name and call the appropriate handling function?

Also, if it's not a problem, could you please comment on my naive way of
implementing a prompt with:

input   : /* empty */
        | input line    { printf ("> "); }

Finally, if I haven't completely exhausted your patience, is there any
portable way to have at least minimal line editing capability (cursor
movement, history, ...) in interactive mode?

Please forgive my newbiness! O:) I thank you for your time and patience!

Best wishes,

Andrej




reply via email to

[Prev in Thread] Current Thread [Next in Thread]