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: Nils Hasler
Subject: Re: Bison 1.50 question: Can it support Auto-Complete?
Date: Wed, 23 Oct 2002 11:25:14 +0200
User-agent: Mozilla/5.0 (Windows; U; WinNT4.0; de-DE; rv:0.9.2) Gecko/20010726 Netscape6/6.1



Date: Fri, 18 Oct 2002 11:56:14 -1000
From: Chris Hundhausen <address@hidden>

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.


_______________________________________________
address@hidden 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






reply via email to

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