On 06/26/2012 11:58 PM, Hans Aberg wrote:
On 26 Jun 2012, at 13:23, Timothy Madden wrote:
I wrote a grammar file for an approximation of the php language (with the
purpose of adding braces to single-statements under if, for, while, but that is
not included yet), that simply outputs the found tokens back to an output
stream.
If you want to write a grammar for a known language, there is a good chance
somebody already has done so. You might inquiry in the Usenet newsgroup
comp.compilers.
My problem is I got like 233 shift/reduce conflicts (and 18 reduce/reduce
conflicts) and I do not really understand them. But searching the internet
shows most people have like 3 or 4 of them ... what did I do ?
Is there a way for me to understand that report file bison outputs ? I looked a
lot through it, but I can not get where the conflicts are comming from. It does
says something about the first rules, describing whitespace, which I want to
preserve as significant part of the grammar.
I have CenOS 6.2 64-bit (up-to-date), GNU bison 2.4.1 64-bit.
The grammar and report files are attached.
Bison token preferences act on the tokens immediately before and after the
parsing position '.' of the conflicting shift/reduce rules. So you might try to
get rid some of them that way, by setting token precedences.
Reduce/reduce conflicts may call for the grammar to be rewritten - write a
grammar for a more general language without them, and cut it down in the
semantic actions. Or you may try to use the GLR parser - see the manual for an
example, from parsing C++ I recall. It is possible to use the first method for
that too, so it is part a matter of taste which method to choose.
I read the grammar file from the php sources. My problem is I want to
_preserve_ spaces. I only want to do add redundant braces around
single-statements under if/for/while. And completely dropping spaces and
comments from source code for such an operation is inappropriate. Not to
mention the real language is more complex than my approximation, and I
am not sure I can just copy the parser from another project..
After struggling with it for a day or more, I also began thinking my
grammar, with explicit spaces, is not really a one-token-only lookahead.
I tried using %glr-parser with no change in output.
Can bison really support GLR parsers with the C++ interface ?
Because the documentation says I should use '%skeleton "lalr.cc"', which
to me suggests it is LALR only. I tried %skeleton "glr.cc", but then I
got an error that my YYSTYPE (which I defined to std::string) is not a
legal member for a union.
I know 2.4.1 is not latest, but it is the latest version on my Linux
distribution, CentOS, here at work.
Thank you,
Timothy Madden
_______________________________________________
help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison