summaryrefslogtreecommitdiff
path: root/src/cparse/cparse.ypp
blob: eef3d133c2da6f6ddf3b2275ba0c60b84704713b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* bison -v -t --defines=cparse.h -o cparse.cpp cparse.ypp */
%{
#include <stdio.h>
#include "cparse_def.h"

extern "C" {
int yylex();
}

void
yyerror(const char *s)
{
  printf("%s\n", s);
}

int level = 0;
int ndeact = 0;
char *ncomment = NULL;
char *nname = NULL;
char *nval = NULL;

void
print_node()
{
  int i = 0;
  if (ncomment) {
    for (i = 0; i <= level; i++) {
      printf("  ");
    }
    printf(" /*%s*/\n", ncomment);
  }
  printf("%s", ndeact ? "!" : " ");
  for (i = 0; i <= level; i++) {
    printf("  ");
  }
  printf("%s", nname);
  if (nval) {
    printf(" '%s'", nval);
  }
  printf("\n");
}

%}

%token NODE
%token VALUE
%token COMMENT
%token LEFTB
%token RIGHTB

%%

input:      forest
          | forest comment
;

forest:     /* empty */
          | forest tree
;

tree:       node {
              print_node();
            }
          | node {
              print_node();
            } LEFTB { ++level; } forest RIGHTB { --level; }
;

node:       nodec {
              nval = NULL;
            }
          | nodec VALUE {
              nval = $2.str;
            }
;

nodec:      NODE {
              ncomment = NULL;
              nname = $1.str;
              ndeact = $1.deactivated;
            }
          | COMMENT NODE {
              ncomment = $1.str;
              nname = $2.str;
              ndeact = $2.deactivated;
            }
;

comment:    COMMENT
          | comment COMMENT
;

%%

int
main()
{
  return yyparse();
}