Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #1072

Re: flex + yacc problem: partial parsing.

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.compilers
Subject Re: flex + yacc problem: partial parsing.
Date 2014-01-07 23:56 +0000
Organization Aioe.org NNTP Server
Message-ID <14-01-005@comp.compilers> (permalink)
References <13-12-008@comp.compilers>

Show all headers | View raw


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

Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

flex + yacc problem: partial parsing. Kaz Kylheku <kaz@kylheku.com> - 2013-12-17 22:52 +0000
  Re: flex + yacc problem: partial parsing. Kaz Kylheku <kaz@kylheku.com> - 2014-01-07 23:56 +0000
  Re: flex + yacc problem: partial parsing. Kaz Kylheku <kaz@kylheku.com> - 2014-01-08 03:18 +0000
    Re: flex + yacc problem: partial parsing. Kaz Kylheku <kaz@kylheku.com> - 2014-01-08 18:29 +0000

csiph-web