Hi Laura, I would start with a global linked list that is filled in the semantic actions. Building on your example:
================ %{ #include <stdio.h> #include <stdlib.h> typedef struct elem { char *val; struct elem *next; } elem_t; elem_t *words = NULL; void add_word(char *val) { elem_t *word = (elem_t *)malloc(sizeof(elem_t)); if (word == NULL) { fprintf (stderr, "%s", "malloc failed"); exit(1); } word->val = val; word->next = words; words = word; } %} %output "parser.c" %defines "parser.h" %union { char *str; } %token <str> WORD %start Input %% Input : WORD { add_word(yylval.str); printf("word: %s\n", yylval.str); } | Input WORD { add_word(yylval.str); printf("word: %s\n", yylval.str); } ; %% ================ Yours, Alex On 04/02/2017 11:20 AM, Laura Morales wrote: > I have this simple grammar which basically simply split symbols at white space > > ================ > %{ > #include <stdio.h> > #include <stdlib.h> > %} > > %output "parser.c" > %defines "parser.h" > > %union { char *str; } > > %token <str> WORD > > %start Input > > %% > > Input > : WORD { printf("word: %s\n", yylval.str); } > | Input WORD { printf("word: %s\n", yylval.str); } > ; > > %% > ================ > > I'm trying to put this into a library, so the main() is in another file. This > works, it's printing all the words. However what I want to do instead is > return some data structure such as an array with all the WORDs (printf() is > only there for testing). So... how can I achieve this? > > _______________________________________________ > help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison > _______________________________________________ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison