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


Groups > comp.compilers > #650

Re: Yacc/Bison - what semantic actions to take on a parse error

From James Harris <james.harris.1@gmail.com>
Newsgroups comp.compilers
Subject Re: Yacc/Bison - what semantic actions to take on a parse error
Date 2012-05-24 12:05 -0700
Organization Compilers Central
Message-ID <12-05-021@comp.compilers> (permalink)
References <12-05-014@comp.compilers>

Show all headers | View raw


On May 23, 12:19 pm, James Harris <james.harri...@gmail.com> wrote:

> Yacc etc allow the special "error" keyword to be used in rules to aid
> error recovery. Where those rules are there to generate a node of a
> tree and there has been a parse error what should one tell Yacc to do?

...

> [My standard answer is that the error token is mostly useful for
> resynchronizing to try to find some more syntax errors, but that it's
> a losing battle to try to do much what you've parsed.

I wasn't thinking about using the parse tree after the parse phase so
much as just completing the parse.

An example may help illustrate. Say were defining a node type X where
there is nothing special about that node type. We might have a grammar
construct something like the following. I'll use quotes "..." to
indicate descriptive text.

%type <X_type> X
X
  : "a normal X" ';' { $$ = Xnode("specific data"); }
  | error ';' { ACTION; }
;

The Xnode call constructs a node. The X production expects $$ to be
set to a node of the given type.

The issue is that the error production cannot create a meaningful node
so what actions to replace ACTION are appropriate? Here are some
options.

* Create an X node with dummy values. That would satisfy the type
checking.
* Set $<err_msg>$ = "invalid X node"
* Braces but no action, i.e. {}
* No action clause so default to $$ = $1;
* Some combination of YYERROR; and yyerror();

John, you'll know but for anyone who isn't aware of these options
those for Bison are shown at

  http://www.gnu.org/software/bison/manual/html_node/Action-Features.html

So, which option is 'best'? Or should we just ignore a type mismatch
error?

James
[You already know you ran into a syntax error, so I'd think that type
checking is more likely to produce an error cascade than something
useful. -John]

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


Thread

Yacc/Bison - what semantic actions to take on a parse error James Harris <james.harris.1@gmail.com> - 2012-05-23 04:19 -0700
  Re: Yacc/Bison - what semantic actions to take on a parse error James Harris <james.harris.1@gmail.com> - 2012-05-24 12:05 -0700
    Re: Yacc/Bison - what semantic actions to take on a parse error James Harris <james.harris.1@gmail.com> - 2012-05-24 22:49 -0700
      Re: Yacc/Bison - what semantic actions to take on a parse error Chris F Clark <cfc@shell01.TheWorld.com> - 2012-05-30 14:41 -0400

csiph-web