help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bison yyparse return array of values


From: Martin Alexander Neumann
Subject: Re: Bison yyparse return array of values
Date: Sun, 2 Apr 2017 11:47:47 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0

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?
> 
> _______________________________________________
> address@hidden https://lists.gnu.org/mailman/listinfo/help-bison
> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]