tags:

views:

29

answers:

1

I'm new to yacc/lex and I'm working on a parser that was written by someone else. I notice that when an undefined token is found, the parser returns an error and stops. Is there a simple way to just make it ignore completely lines that it cannot parse and just move on to the next one?

A: 

just add a rule that looks like

. {
  // do nothing
}

at the bottom of all of your rules, and it will just ignore everything it comes across that doesn't fit any of the previous rules.

Edit: if you have multiple states, then a catch-all that works in any state would then look like:

<*>. {

}
AwesomeJosh
Thanks, but I'm still not entirely sure where this should go - I tried putting this in both my lex and parser files - yacc returned an error when i tried putting this in the lex file, it just didn't do anything. can you please elaborate? thanks.
Udi
It just goes below any other rules you have; for example, if you have just two tokens that you're trying to recognize, and you want to ignore everything else, you would have your two rules, and then just place the catch-all in the same block, but just make sure that it is below the rest of the rules, so that it has the lowest priority.
AwesomeJosh
The period just matches any single character, and if the code block is empty, it will just match everything that doesn't match another rule, and then perform what is in the code block (i.e. nothing)
AwesomeJosh