# Racc error recovery

**URL:** <https://www.ruby-forum.com/t/racc-error-recovery/118232>\
**Category:** Ruby\
**Created:** [October 25, 2007, 12:37am UTC](https://www.ruby-forum.com/t/racc-error-recovery/118232 "2007-10-25T00:37:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [October 25, 2007, 12:37am UTC](https://www.ruby-forum.com/t/racc-error-recovery/118232/1 "2007-10-25T00:37:11Z")

</div>

Hi everyone,

I’m writing a racc parser, and I need to recover from parse errors.  
Basically I’m writing a CSS parser, and I need to handle poorly  
formatted CSS.

Unfortunately I can’t seem to find any good documentation or examples on  
error recovery. I’ve read the Racc documentation about on\_error and  
entering “error recovering mode”, as well as calling yyerrok to leave  
error recovering mode, but I don’t know what that actually means.

I’ve also discovered the “error” rule, but I don’t want to explicitly  
add that rule to every rule that could possibly have an error.

Any tips would be greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [October 25, 2007, 7:15am UTC](https://www.ruby-forum.com/t/racc-error-recovery/118232/2 "2007-10-25T07:15:06Z")

</div>

> I’m writing a racc parser, and I need to recover from parse errors.  
> …  
> Unfortunately I can’t seem to find any good documentation or examples on  
> error recovery. I’ve read the Racc documentation about on\_error and

Wild stab in the dark: is there any equivalent stuff in yacc you could  
use as a starting point?

I realize it probably isn’t modeled that closely, but that’s the first  
thought I had.

Wild stab in the dark, part 2: tried using Sass instead? It’s a CSS  
DSL. Or do you have to work with the CSS you’ve already got?

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [October 26, 2007, 8:30am UTC](https://www.ruby-forum.com/t/racc-error-recovery/118232/3 "2007-10-26T08:30:28Z")

</div>

On Thu, Oct 25, 2007 at 02:14:20PM +0900, Giles B. wrote:

> > I’m writing a racc parser, and I need to recover from parse errors.  
> > …  
> > Unfortunately I can’t seem to find any good documentation or examples on  
> > error recovery. I’ve read the Racc documentation about on\_error and
> 
> Wild stab in the dark: is there any equivalent stuff in yacc you could  
> use as a starting point?

Yes there is. I was hoping I could get an answer without digging up my  
lex & yacc book. 😉 Fortunately I found it and there is an error  
recovery section that has definitely helped.

> I realize it probably isn’t modeled that closely, but that’s the first  
> thought I had.
> 
> Wild stab in the dark, part 2: tried using Sass instead? It’s a CSS  
> DSL. Or do you have to work with the CSS you’ve already got?

No. I’m trying to parse CSS, not generate it.

---

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [October 28, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/racc-error-recovery/118232/4 "2007-10-28T19:40:36Z")

</div>

On Oct 25, 10:29 pm, Aaron P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> lex & yacc book. 😉 Fortunately I found it and there is an error  
> No. I’m trying to parse CSS, not generate it.
> 
> –  
> Aaron P.[http://tenderlovemaking.com/](http://tenderlovemaking.com/)

Aaron,

Would you mind sharing some of what you found in the book? I’m more  
interested in what you were thinking originally and then what the book  
had to say to change your mind.

I’m interested in putting together some more documentation for Racc.

Mike B.

---

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [October 29, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/racc-error-recovery/118232/5 "2007-10-29T17:42:06Z")

</div>

On Mon, Oct 29, 2007 at 03:40:02AM +0900, barjunk wrote:

> > > DSL. Or do you have to work with the CSS you’ve already got?  
> > > had to say to change your mind.  
> > > What I was thinking originally was that on\_error could eat up tokens  
> > > until the grammar could be recovered. Then, after reading the lex/yacc  
> > > book I found out that that is exactly what the “error” token does.

Take this grammar/script for example:

class Tester

```
token A LBRACE

```

rule  
a\_a\_lbrace  
: A A LBRACE { puts val }  
| A error LBRACE { puts “got error”; puts val }  
;

end

require ‘tester.tab’

class Foo \< Tester  
def initialize  
@tokens = [  
[:A, ‘a’],  
[:B, ‘b’],  
[:LBRACE, ‘{’],  
]  
end

```
def parse
  do_parse
end

def next_token
  @tokens.shift
end

```

end

Foo.new.parse

The second rule gets called, and Racc uses LBRACE as a “pivot point”  
(IIRC, that is what the yacc book called it). The error rule will eat  
up tokens until an LBRACE is found. Then parsing can continue as usual.

Unfortunately, I’m not sure when on\_error is supposed to be called. I’m  
also having a hard time figuring out where to place the error token in  
some of my rules. But I am going to start a new thread about that.
