How to get 'expected token' in bison/yacc GLR-parser?
How to get 'expected token' in bison/yacc GLR-parser?
Hi,
In the project i am doing, there'er a few ambiguou开发者_如何学Pythons gramar. So i am trying to use %glr-parser to solve the shift/reduce confilicts.
When i was using non-GLR parser, i can use the yystate(global variable) to get the "expected token" when detect syntax error.But after switch to GLR parser, i find it's not global variable anymore.
So my question is, is there anyway to get the "expected token" in GLR-parser when there's a syntax error?
It is correct that there is no valid yystate
information in a GLR parser, and so there cannot be a simple solution of "expected token".
To see why one has to understand the difference between the default LALR parser and the GLR parsing algorithms. This is well documented here: http://www.delorie.com/gnu/docs/bison/bison_11.html. Basically, the GLR (Generalised LR) executes multiple concurrent threads, each one trying the different ambiguous alternatives to search for a match. When none of them match the parse fails and you are in a syntax error situation. Therefore there cannot be a single expected token, but there might be many expected tokens. That is why it is ambiguous. A good document explaining how the bison parser works is here: http://www.cs.uic.edu/~spopuri/cparser.html, which explains the function of the yystate variable in a non-GLR parse.
The method of generating better error message for a GLR parser is best covered in @rici's answer to this question: Additional syntax error message on GLR parser when syntax is ambiguous; and in many ways this question is really a duplicate of that one.
It just hasn't been closed as a duplicate because it's rather obscure as to why they are both asking about the same problem.
精彩评论