beelsebob / CoreParse

A shift/reduce parsing framework for Mac OS X and iOS
BSD 3-Clause "New" or "Revised" License
366 stars 49 forks source link

Grammar error: "Could not insert reduce in action table for state 4, token #" #29

Closed siuying closed 10 years ago

siuying commented 10 years ago

I'm working on a grammar that should accept optional white space. I added the optional white space rule and the parser complain error "Could not insert reduce in action table for state 4, token #".

I could not tell if this is problem in grammar or it is not supported by CoreParse. Appreciate any pointer to this error!

Before I added optional space:

CSSSelectors                    ::= <CSSSelectorSequence> (<CSSCombinator> <CSSSelectorSequence>)*;
CSSCombinator                   ::= <Greater>  'Whitespace'* | <Plus>  'Whitespace'* | <Tilde> 'Whitespace'*| 'Whitespace'+;
Plus                            ::= '+';
Greater                         ::= '>';
Tilde                           ::= '~';

After I added optional space, parser return error:

CSSSelectors                    ::= <CSSSelectorSequence> (<CSSCombinator> <CSSSelectorSequence>)*;
CSSCombinator                   ::= <Greater>  'Whitespace'* | <Plus>  'Whitespace'* | <Tilde> 'Whitespace'*| 'Whitespace'+;
Plus                            ::= 'Whitespace'* '+';
Greater                         ::= 'Whitespace'* '>';
Tilde                           ::= 'Whitespace'* '~';

Full grammar: https://github.com/siuying/CSSSelectorConverter/blob/core-parse/CSSSelectorConverter/CSSSelectorGrammar.txt

beelsebob commented 10 years ago

This is indeed an issue with your grammar not being LALR1. What I'd suggest you do is to instead use your tokeniser delegate to remove whitespace tokens except in any locations where the whitespace has a meaning (and in fact, even in these cases, to use it to refactor your whitespace into something more structured). Then your grammar does not need to deal with whitespace at all. Hope that helps.

siuying commented 10 years ago

@beelsebob Thanks, this help!