Re: How to decide what to put in the lexer and the grammar respectively?
> On 18 Feb 2019, at 06:44, Akim Demaille wrote: > >> Le 18 févr. 2019 à 00:10, Hans Åberg a écrit : >> >>> On 17 Feb 2019, at 23:10, Peng Yu wrote: >>> >>> This lexical tie-in creates feedback from the parser to the lexer. So >>> the lexer cannot be tested standalone. >>> >>> But the principle of separating lexer and parser is to make parser >>> builtin on top of the parser. Is there something that can avoid the >>> feedback yet still allow context-dependent parsing? Alternatively, how >>> to just testing the lexer without having to get the parser involved? >> >> The LARL(1) that Bison uses is for context free grammars only, so contexts >> must involve switches somehow. > > I don't think Peng was referring to context-sensitivity here in the > technical sense. He just means "lexical context depending on the > current state of the parser", which is still CFG. Let's not confuse > people by referring to something unrelated. Subsequent discussions might clarify that. ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: bison info doc - precedence in recursive parsing
> On 18 Feb 2019, at 08:28, Akim Demaille wrote: > >> Le 17 févr. 2019 à 23:00, Hans Åberg a écrit : >> >>> On 17 Feb 2019, at 16:19, Akim Demaille wrote: >>> Le 10 févr. 2019 à 15:20, Hans Åberg a écrit : > On 10 Feb 2019, at 11:07, Akim Demaille wrote: > > [*.dot vs. *.gv] > But it's too late to change the default behavior. You might change it, as it is not usable on real life grammars. >>> >>> You have a point :) >> >> Or a dot! :-) > > Yes, I'll gv you that :) :-) ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
On Sonntag, 17. Februar 2019 21:37:26 CET Peng Yu wrote: > Because I want to use the previously allocated memory, I don't want to > call "rs_init(&yylval->str)" in any action. So YYSTYPE must be a > struct instead of a union. Is it a good practice to use struct instead > of union? Yes, it is. The only advantage by using a union as a type is that you save some tiny amount of memory, but a union is also less safe and harder to debug. So personally I would barely ever use union as fundamental type with Bison. > Since %union cannot be used in this case, how to deal with this > scenario in bison? Thanks. There is actually no difference in usage, since the Bison generated parser code will access the fields in the same way: simply by using the union/struct member names you defined (e.g val.num, val.str in your example). So the actual difference between struct or union is handled by the C/C++ compiler, not by Bison. Any reason that you don't want to use C++ and e.g. std::string instead of your pure C and garbage collector solution? Best regards, Christian Schoenebeck ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
On Montag, 18. Februar 2019 12:22:27 CET Christian Schoenebeck wrote: > On Sonntag, 17. Februar 2019 21:37:26 CET Peng Yu wrote: > > Because I want to use the previously allocated memory, I don't want to > > call "rs_init(&yylval->str)" in any action. So YYSTYPE must be a > > struct instead of a union. Is it a good practice to use struct instead > > of union? > > Yes, it is. The only advantage by using a union as a type is that you save > some tiny amount of memory, but a union is also less safe and harder to > debug. So personally I would barely ever use union as fundamental type with > Bison. Btw, you can also mix both approaches, e.g.: struct _YYSTYPE { union { int inum; float fnum; }; rapidstring str; Foo_t foo; }; typedef it appropriately, I usually use C++ code, but you get the idea. Best regards, Christian Schoenebeck ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
> > C++ is too verbose, generate bloated binary and takes long time to compile. Any reason that you don't want to use C++ and e.g. std::string instead of > your > pure C and garbage collector solution? > -- Regards, Peng ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
> Le 18 févr. 2019 à 04:37, Peng Yu a écrit : > > Since %union cannot be used in this case, how to deal with this > scenario in bison? Thanks. You want to read the documentation of api.value.type. For instance: ‘{TYPE}’ Use this TYPE as semantic value. %code requires { struct my_value { enum { is_int, is_str } kind; union { int ival; char *sval; } u; }; } %define api.value.type {struct my_value} %token INT "integer" %token STR "string" ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
%token-table doesn't seem to be working for C++?
Perhaps I’m doing something wrong, but it appears that, in C++, %token-table doesn’t work: instead of yytname, I only see yytname_. It also appears that YYNTOKENS, YYNNTS, YYNRULES, and YYNSTATES are not defined, contrary to the documentation. Am I missing something, or is this broken? Thanks, Derek ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
> On 18 Feb 2019, at 04:37, Peng Yu wrote: > > I use rapidstring to make the string operations use and use the Boehm > garbage collector so that I don't have to always remember to release > the memory. > > https://github.com/boyerjohn/rapidstring > > Because I want to use the previously allocated memory, I don't want to > call "rs_init(&yylval->str)" in any action. So YYSTYPE must be a > struct instead of a union. The Boehm GC probably can find pointers in unions, a link suggested there might be a problem with guessing correctly for packed structs. ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
> On 18 Feb 2019, at 04:37, Peng Yu wrote: > > I use rapidstring to make the string operations use and use the Boehm > garbage collector so that I don't have to always remember to release > the memory. > > https://github.com/boyerjohn/rapidstring > > Because I want to use the previously allocated memory, I don't want to > call "rs_init(&yylval->str)" in any action. That is probably not needed (Akim may clarify): Without a GC, just hand over the lexer allocated pointer to the parser marked with a type, and set %destructor for this type to handle error cleanup when the parser stack unwinds; otherwise deallocate in the rules. ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
> Le 18 févr. 2019 à 23:06, Hans Åberg a écrit : > > >> On 18 Feb 2019, at 04:37, Peng Yu wrote: >> >> I use rapidstring to make the string operations use and use the Boehm >> garbage collector so that I don't have to always remember to release >> the memory. >> >> https://github.com/boyerjohn/rapidstring >> >> Because I want to use the previously allocated memory, I don't want to >> call "rs_init(&yylval->str)" in any action. > > That is probably not needed (Akim may clarify): Without a GC, just hand over > the lexer allocated pointer to the parser marked with a type, and set > %destructor for this type to handle error cleanup when the parser stack > unwinds; otherwise deallocate in the rules. I don't know enough of how this is supposed to work to answer. Of course, you have to not use the same memory space for returning two strings in a row. https://www.gnu.org/software/bison/manual/bison.html#Strings-are-Destroyed Also, Peng writes: >> I don't want to call "rs_init(&yylval->str)" in any action. You don't have to do that in every action, just the ones returning strings. ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: %token-table doesn't seem to be working for C++?
Hi Derek, > Le 18 févr. 2019 à 21:07, Derek Clegg a écrit : > > Perhaps I’m doing something wrong, but it appears that, in C++, %token-table > doesn’t work: instead of yytname, I only see yytname_. It also appears that > YYNTOKENS, YYNNTS, YYNRULES, and YYNSTATES are not defined, contrary to the > documentation. Am I missing something, or is this broken? Let me answer with a question: what are you trying to achieve? What do you need these for? ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
>>> I don't want to call "rs_init(&yylval->str)" in any action. > You don't have to do that in every action, just the ones returning strings. I don't get the point. Is there a problem to not call "rs_init(&yylval->str)" in any action (this include the actions that return strings)? -- Regards, Peng ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison
Re: Not able to use %union?
Le 19 fA(c)vr. 2019 A 07:01, Peng Yu a A(c)crit : I don't want to call "rs_init(&yylval->str)" in any action. You don't have to do that in every action, just the ones returning strings. I don't get the point. Because I misread, sorry. I read "in every action". Is there a problem to not call "rs_init(&yylval->str)" in any action (this include the actions that return strings)? Well, I don't see how you would store several strings (say echo "foo" "bar", that's probably three strings) with just one allocation. But then again, I don't know enough about your situation, and your parser. That's what this link is about. https://www.gnu.org/software/bison/manual/bison.html#Strings-are-Destro yed ___ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison