summaryrefslogtreecommitdiff
path: root/c-flex-bison/parser.y
blob: 57303413d2baa7c27dc61fc2d253e1237c3ed48c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
%{
  #include <stdio.h>
  #include "tree.h"
  #include "parser.h"
  #include "lexer.h"
  void yyerror (struct forest **root, yyscan_t scanner, char const *);
%}

%code requires {
  typedef void* yyscan_t;
}

%header "parser.h"
%define parse.trace
%define api.pure
%token
  TREE_START
  TREE_END
  <exp> VAL
  FAILURE
%parse-param {struct forest **root}
%param   { yyscan_t scanner }

%union {
  struct tree* exp;
  struct forest* forest;
}
%type <exp> tree
%type <forest> forest
%type <exp> tree_or_val

%%
 /* The grammar */

forest:
%empty { $$ = NULL; }
| forest[F] tree_or_val[T]
{
  if ($F != NULL) {
    plant_tree($F, $T);
    $$ = $F;
  } else {
    $$ = forest_alloc();
    plant_tree($$, $T);
  }
  *root = $$;
};

tree: TREE_START forest[F] TREE_END
{
  $$ = tree_alloc(parenthesized, NULL, 0);
  $$->children = $F;
};

tree_or_val: tree | VAL;