[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bison yyparse return array of values
From: |
Laura Morales |
Subject: |
Re: Bison yyparse return array of values |
Date: |
Sun, 2 Apr 2017 18:03:50 +0200 |
I'm looking at this example http://esr.ibiblio.org/?p=6341 which is using these
slightly different instructions
%define api.pure full
%lex-param {yyscan_t scanner}
%parse-param {yyscan_t scanner}
and then add another parameter to handle the output
%define api.pure full
%lex-param {yyscan_t scanner} {cvs_file *cvsfile}
%parse-param {yyscan_t scanner} {cvs_file *cvsfile}
Can somebody please explain the meaning of "yyscan_t scanner" and if I can omit
it?
================
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct elem {
char *val;
struct elem *next;
} elem_t;
elem_t *add_word(elem_t *words, 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;
return word;
}
%}
%output "parser.c"
%defines "parser.h"
%name-prefix "yy1"
%define api.pure full
%parse-param { elem_t **words };
%union {
char *str;
}
%token <str> WORD
%start Input
%%
Input
: WORD { *words = add_word(NULL, $1);
printf("word: %s\n", yylval.str); }
| Input WORD { *words = add_word(*words, $2);
printf("word: %s\n", yylval.str); }
;
%%
int main() {
elem_t *words = NULL;
yy1parse(&words);
int i = 0;
while (words != NULL) {
i++;
words = words->next;
}
fprintf(stdout, "Parsed %d WORDS\n", i);
}
void yy1error(char *error) {
fprintf(stderr, "%s", error);
}
int yy1lex(YYSTYPE *lvalp) {
static int i = 0;
while (i++ < 100) {
lvalp->str = "testWord";
return WORD;
}
return 0;
}
================