Re: flex + yacc problem: partial parsing.

Kaz Kylheku <kaz@kylheku.com>
Tue, 7 Jan 2014 23:56:02 +0000 (UTC)

          From comp.compilers

Related articles
flex + yacc problem: partial parsing. kaz@kylheku.com (Kaz Kylheku) (2013-12-17)
Re: flex + yacc problem: partial parsing. kaz@kylheku.com (Kaz Kylheku) (2014-01-07)
Re: flex + yacc problem: partial parsing. kaz@kylheku.com (Kaz Kylheku) (2014-01-08)
Re: flex + yacc problem: partial parsing. kaz@kylheku.com (Kaz Kylheku) (2014-01-08)
| List of all articles for this month |

From: Kaz Kylheku <kaz@kylheku.com>
Newsgroups: comp.compilers
Date: Tue, 7 Jan 2014 23:56:02 +0000 (UTC)
Organization: Aioe.org NNTP Server
References: 13-12-008
Keywords: lex, yacc
Posted-Date: 08 Jan 2014 00:56:50 EST

On 2013-12-17, John added:
> [You can use YYACCEPT to tell yacc or bison that you're done parsing,
> but I'm not sure about the readahead. -John]


Thanks a lot for that heads up.


I have done an experiment which shows that the readahead will in fact be
suppressed.


However, it requires cooperation from the grammar, so a trivial sprinkling of
YYACCEPT in my actual grammar doesn't do the trick. I already know quite
exactly what the reason is: in my grammar, an expr (that I'm trying to
extract) can be "expr DOTDOT expr" where DOTDOT is a .. operator.
For this reason, we cannot reduce expr to the start symbol without looking
ahead to see whether there is a DOTDOT token, and so by the time we get
to the rule associated with that reduction, the lookahead has taken place.


However, the following test program shows that lookadhead is not performed
unnecessarily to do a reduction which doesn't require it.


If we leave YYACCEPT commented out, then the message "token!" is printed
twice (showing that yylex is called twice) and there is a syntax error.
This is most likely because the parser is processing the embedded rule:


        0 $accept: S . $end


and the scanner has returned another FOO.


If we uncomment YYACCEPT, then yylex is called only once, and the parse
succeeds, showing that no lookahead was performed to achieve the reduction
of S <- FOO, and the $accept <- FOO $end reduction is nicely skipped.


I think I have enough info to make progress on this. Thanks again.


%{
#include "y.tab.h"
void yyerror(const char *str);
%}


%token FOO
%type<str> S


%%


S : FOO { /* YYACCEPT; */ };


%%


int yylex(void)
{
    puts("token!");
    return FOO;
}


void yyerror(const char *str)
{
    puts(str);
}


int main(void)
{
    yyparse();
}




--
Music DIY Mailing List: http://www.kylheku.com/diy
ADA MP-1 Mailing List: http://www.kylheku.com/mp1


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.