help-bison
[Top][All Lists]
Advanced

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

RE: Bison 1.50 question: Can it support Auto-Complete?


From: Chris Hundhausen
Subject: RE: Bison 1.50 question: Can it support Auto-Complete?
Date: Thu, 24 Oct 2002 14:30:44 -1000

Nils,

Thanks so much for your response. I'm wondering if you can give me more
specific instructions on how to implement your proposed changes. I'm
assuming that I'll be modifying the file xxxx.tab.c. I naively tried to
insert your new "expect" function at the bottom of my xxxx.tab.c file,
but I got compile errors. It seems to me that there's more involved
here. Perhaps you could send me a "before and after" example: one
example of what bison generated, and a second example of that same file
with your modifications.

Thanks in advance,
:-)Chris

FROM: Nils HaslerDATE: 10/23/2002 02:25:14
>
>Date: Fri, 18 Oct 2002 11:56:14 -1000
>From: Chris Hundhausen <<EMAIL: PROTECTED>>
>
>I'm trying to build "auto-complete" functionality into an interactive
>development environment. The language I'm parsing is written in
>flex/bison (probably v. 1.25). In other words, for incomplete sentences
>in my language, I want to generate a complete list of valid next
tokens.
>Unfortunately, setting the VERBOSE flag in Bison doesn't do the job. I
>do, indeed, receive *some* valid completions, but not all. As far as I
>can tell, if multiple rules are tried, the verbose error message
reports
>only the next possible tokens in the _last_ rule attempted; possible
>completions from previous rules are not reported.
>
>So, I'm wondering if --report=THINGS does anything differently.  Is
>'itemset' a list of all next possible tokens?
>
>
>Sorry, I'm not the right guy to ask about this (I could read the code,
>but then that wouldn't be more efficient than your doing it).
>
>I'll CC: this to help-bison to see whether others can suggest more.
>
>
>_______________________________________________
><EMAIL: PROTECTED> http://mail.gnu.org/mailman/listinfo/help-bison
>
I did just that with some old bison-version. I don't think you need to
modify bison. At least I didn't...
I only modified the generated source, since that old bison couldn't
handle different templates.
Now what do you need to do?

First I introduced a new global variable holding the state where the
last lookahead was popped. Then I could use the following code to get
all valid lookaheads for that state:

void expect(int state)
{
        short *stack = yyssp;
        int yyn, x, count = 0;
        int len;

        while(1) {
                /* First try to decide what to do without reference to
lookahead token. */
                yyn = yypact[state];
                if(yyn != YYFLAG) {
                        for(x = (yyn < 0 ? -yyn : 0); yytname[x] != 0;
x++) {
                                if(yycheck[x + yyn] == x) {
                                        if(count == 0)
                                                fprintf(stderr,
"expecting %s", yytname[x]);
                                        else
                                                fprintf(stderr, ", %s",
yytname[x]);
                                        count++;
                                }
                        }
                }

                yyn = yydefact[state];
                if(yyn == 0)
                        break;

                len = yyr2[yyn]; // default reduction

                stack -= len; // reduce the stacks
                yyn = yyr1[yyn]; // shift the result of the reduction

                state = yypgoto[yyn - YYNTBASE] + *stack;
                if(state >= 0 && state <= YYLAST && yycheck[state] ==
*stack)
                        state = yytable[state];
                else
                        state = yydefgoto[yyn - YYNTBASE];
                stack++; // we push the new state
        }

        fprintf(stderr, "\n");
}

This code prints all lookaheads for a state, just like the verbose error
handling and then does all default-reductions possible (until
defact[state] == 0), printing all lookaheads on its way.

hope this helps
nils


**********************************************************************
Christopher D. Hundhausen                  mailto:address@hidden
Assistant Professor, Information and Computer Sciences Department
Assistant Director,  Laboratory  for  Interactive Learning Technologies
University of Hawai'i  * 1680 East West Road  *  Honolulu, HI 96822
808.956.3887 * Fax: 808.956.3548 * http://lilt.ics.hawaii.edu/~hundhaus
***********************************************************************





reply via email to

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