Re: x + (y) + z
Frank, >> Your grammar contained a single %merge. I thought at >> least two are required? > >All the involved (top-level) rules must have a `%merge'. In the >original example, both happened to be the same rule. Thanks for a great example. It looks like the implementation of %merge is different from that of %dprec (big mistaken assumption on my part). After reading the source I still don't understand why yyparse handles them differently. My expression parsing problems are now solved. >The second example works and shows all four trees (after fixing a >few precedences in the grammar) with another `%merge' -- in the >final grammar you might need some more. This is a great example (the attached version tries to be visually simpler). It ought to be included in the bison documentation to show how the details of %merge differ from those of %dprec. Or perhaps %dprec should be made as general as %merge. %glr-parser %token ID %{ #include #include static const char *input = "(x)+(y)+z"; #define YYSTYPE char * static void yyerror (const char *s) { printf ("%s\n", s); } static YYSTYPE stmtMerge (YYSTYPE a, YYSTYPE b) { char *s; asprintf (&s, "\n{ %s %s }\n", a, b); return s; } static int yylex (); %} %% main: add_expr { puts ($1); }; add_expr: mult_expr %merge | add_expr '+' mult_expr %merge { asprintf (&$$, "[b+ %s %s]", $1, $3); } | add_expr '-' mult_expr { asprintf (&$$, "[b- %s %s]", $1, $3); } ; mult_expr: factor | mult_expr '*' factor { asprintf (&$$, "[b* %s %s]", $1, $3); } | mult_expr '/' factor { asprintf (&$$, "[b/ %s %s]", $1, $3); } ; factor: '(' add_expr ')' { asprintf (&$$, "(%s)", $2); } | '(' ID ')' factor{ asprintf (&$$, "<%s>%s", $2, $4); } | '+' factor { asprintf (&$$, "u+%s", $2); } | '-' factor { asprintf (&$$, "u-%s", $2); } | ID ; %% static int yylex () { char c = *input ? *input++ : 0; asprintf (&yylval, "%c", c); return isalnum (c) ? ID : c; } int main () { printf ("%i\n", yyparse ()); } derek -- Derek M Jones tel: +44 (0) 1252 520 667 Knowledge Software Ltd mailto:[EMAIL PROTECTED] Applications Standards Conformance Testing http://www.knosof.co.uk ___ Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
ERROR: version file does not exist or are not readable
When I try to run bison I get folowing: ERROR: version file does not exist or are not readable ___ Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
Re: x + (y) + z
Derek M Jones wrote: > >> Your grammar contained a single %merge. I thought at > >> least two are required? > > > >All the involved (top-level) rules must have a `%merge'. In the > >original example, both happened to be the same rule. > > Thanks for a great example. It looks like the implementation of > %merge is different from that of %dprec (big mistaken assumption > on my part). After reading the source I still don't understand why > yyparse handles them differently. Is it really different? `%dprec' obviously needs two different precedences, therefore two different rules. `%merge' in contrast actually requires the same merge function in all affected places. So I can imagine yyparse actually handles them the same way -- until the point where the dprecs are compared (for inequality) or the merge functions are compared (for equality). So it really seems there are cases that `%dprec' can't handle. Attached is the simplest one I could come up with. It's obviously ambiguous, and even adding `%dprec' in every single rule does not resolve it. AFAICS, that's because the conflict is between v -> e e -> ('a' 'a') 'a' and v -> e e -> 'a' ('a' 'a') I.e., at the time the ambiguity is deteced the choice is between two applications of the same rule (v -> e e) which cannot have different `%dprec'. Whereas at the time the choice is between the two e rules, which have different `%dprec', the parser doesn't know yet that there will be an ambiguity. So it might indeed be useful to somehow look at the sub-productions when deciding dynamic precedences ... BTW, I'm interested in this case as well since it might occur in one of my grammars. `%merge' probably isn't an (easy) option for me because of semantic actions. Of course, I know that rearranging the grammar will help, and if it's the only way, I guess I'll have to do it, but it would be nice if there's some way without rearranging. > My expression parsing problems are now solved. > >The second example works and shows all four trees (after fixing a > >few precedences in the grammar) with another `%merge' -- in the > >final grammar you might need some more. > > This is a great example (the attached version tries to be visually > simpler). It ought to be included in the bison documentation to > show how the details of %merge differ from those of %dprec. I have no objections, and I have already signed a copyright assignment for bison, so no problem from my side. I don't know if your changes are considered non-trivial enough to require an assignment as well, and I don't know if any bison maintainers are reading this here. Otherwise you might want to post it to bug-bison, possibly already as a diff to the texinfo file with accompanying notes in the text. Frank -- Frank Heckenbach, [EMAIL PROTECTED] http://fjf.gnu.de/ GnuPG and PGP keys: http://fjf.gnu.de/plan (7977168E) dprec-problem.y Description: Binary data ___ Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
ERROR: version file does not exist or are not readable
When I try to run bison I get folowing: ERROR: version file does not exist or are not readable ___ Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison