Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news.iecc.com!nerds-end From: James Harris Newsgroups: comp.compilers Subject: Re: Yacc/Bison - what semantic actions to take on a parse error Date: Thu, 24 May 2012 12:05:03 -0700 (PDT) Organization: Compilers Central Lines: 52 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <12-05-021@comp.compilers> References: <12-05-014@comp.compilers> NNTP-Posting-Host: news.iecc.com X-Trace: leila.iecc.com 1337922148 85019 64.57.183.58 (25 May 2012 05:02:28 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Fri, 25 May 2012 05:02:28 +0000 (UTC) Keywords: yacc, errors, comment Posted-Date: 25 May 2012 01:02:28 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:650 On May 23, 12:19 pm, James Harris 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 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 $$ = "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]