Newbie Yacc/Bison shift reduce question
Hi.
Since yacc/bison only has 1 token of look-ahead, I expect a shift reduce conflict in the second "STRING IDEN ..." rule in the code segment pasted below, but I don't get one. Then in a similar situation, I get a shift reduce conflict! See fprintf(stdout, ..) statements below. What's happening? Thanks in advance! Code:
/* Two questions about shift reduce conflicts found in fprintf(stdout, ..) Code:
gparmod6.y: conflicts: 5 shift/reduce datatype IDEN becomes .... to datatype EXTRACT IDEN becomes ... so that yacc can use that token of look-ahead. |
Aidez-moi s'il vous plait? :)
Any ideas? |
Any thoughts (even if they are guesses) would be appreciated... I haven't been able to find anything in the literature about why that "string iden.." rule wouldn't cause a conflict.
|
Consider what decisions the parser must make and when it is obliged to make them...
In the first case, when we see "STRING IDEN becomes," then even though there are two possible rules that we could be following at this moment in time, there's only one thing that we must do: we must shift. It is not until the next symbol appears that we have narrowed things down to just one rule (OBRACK vs. EXTRACT), and even then we're still going to continue to shift. However... if you introduce the fourth rule, a shift/reduce conflict immediately appears. When presented with the input stream "STRING IDEN," we could either shift that symbol onto the stack and proceed, as we did before, or we could reduce STRING to an instance of DATATYPE. The situation is ambiguous: a shift/reduce conflict. When grokking parser problems, it's really important to understand how the parsing engine actually works: what information it is presented with (this information having been generated from the grammar), what its choices are (shift .. that is to say "push" .. or reduce), and how it will make that decision. The grammar is not the actual input that drives the engine: the parsing engine is a state-machine. |
What a helpful response, sundialsvcs! Thanks. If only there were karma so that I could give you some :).
|
You're welcome. :) I guess it always annoyed me, somehow, that parsers were made out to be so gosh-darned mysterious (and subsequently inflicted upon generations of grad-students) when they really are not.
|
All times are GMT -5. The time now is 06:20 AM. |