-
Notifications
You must be signed in to change notification settings - Fork 0
/
grl.y
116 lines (95 loc) · 3.84 KB
/
grl.y
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
%{
#include <stdio.h>
#include <string.h>
char* defaultNodeShapeStart = "";
char* defaultNodeStyleStart = "";
char* defaultNodeShape = "";
char* defaultNodeStyle = "";
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
int main()
{
yyparse();
return 0;
}
%}
%union
{
char *str;
}
%token STYLE SHAPE BEGINDIGRAPH BEGINGRAPH NODE EDGE ENDGRAPH DIRECTED NONDIRECTED ENDLINE
%token <str> STRINGNAME ID DEFAULTNODESHAPE DEFAULTNODESTYLE
%%
commands: /* empty */
| commands command
;
command: startGraph
| buildNode
| buildEdge
| endGraph
| defaultNodeShape
| defaultNodeStyle
;
startGraph:
BEGINGRAPH ENDLINE { printf("graph {"); }
| BEGINGRAPH STRINGNAME ENDLINE { printf("graph %s {", $2); }
| BEGINDIGRAPH ENDLINE { printf("digraph {"); }
| BEGINDIGRAPH STRINGNAME ENDLINE { printf("digraph %s {", $2); }
;
buildNode:
buildNoLabel
| buildWithLabel
;
buildNoLabel:
NODE ID ENDLINE { printf("\t%s [%s%s%s%s];", $2, defaultNodeStyleStart, defaultNodeStyle, defaultNodeShapeStart, defaultNodeShape); }
| NODE ID STYLE STRINGNAME ENDLINE { printf("\t%s [%s%s style=%s];", $2, defaultNodeShapeStart, defaultNodeShape, $4); }
| NODE ID SHAPE STRINGNAME ENDLINE { printf("\t%s [shape=%s%s%s];", $2, $4, defaultNodeStyleStart, defaultNodeStyle); }
| NODE ID STYLE STRINGNAME SHAPE STRINGNAME ENDLINE { printf("\t%s [shape=%s style=%s];", $2, $6, $4); }
| NODE ID SHAPE STRINGNAME STYLE STRINGNAME ENDLINE { printf("\t%s [shape=%s style=%s];", $2, $4, $6); }
buildWithLabel:
NODE ID STRINGNAME ENDLINE { printf("\t%s [label=%s%s%s%s%s];", $2, $3, defaultNodeStyleStart, defaultNodeStyle, defaultNodeShapeStart, defaultNodeShape); }
| NODE ID STRINGNAME SHAPE STRINGNAME ENDLINE { printf("\t%s [label=%s shape=%s%s%s];", $2, $3, $5, defaultNodeStyleStart, defaultNodeStyle); }
| NODE ID STRINGNAME STYLE STRINGNAME ENDLINE { printf("\t%s [label=%s%s%s style=%s];", $2, $3, defaultNodeShapeStart, defaultNodeShape, $5); }
| NODE ID STRINGNAME STYLE STRINGNAME SHAPE STRINGNAME ENDLINE { printf("\t%s [label= %s shape=%s style=%s];", $2, $3, $7, $5); }
| NODE ID STRINGNAME SHAPE STRINGNAME STYLE STRINGNAME ENDLINE { printf("\t%s [label= %s shape=%s style=%s];", $2, $3, $5, $7); }
buildEdge:
buildNonDirected
|
buildDirected
;
buildDirected:
EDGE ID DIRECTED ID STRINGNAME ENDLINE { printf("\t%s -> %s [label=%s];", $2, $4, $5); }
| EDGE ID DIRECTED ID ENDLINE { printf("\t%s -> %s;", $2, $4); }
| EDGE ID DIRECTED ID STRINGNAME STYLE STRINGNAME ENDLINE { printf("\t%s -> %s [label=%s, style=%s];", $2, $4, $5, $7); }
| EDGE ID DIRECTED ID STYLE STRINGNAME ENDLINE { printf("\t%s -> %s [style=%s];", $2, $4, $6); }
buildNonDirected:
EDGE ID NONDIRECTED ID STRINGNAME ENDLINE { printf("\t%s -- %s [label=%s];", $2, $4, $5); }
| EDGE ID NONDIRECTED ID ENDLINE { printf("\t%s -- %s;", $2, $4); }
| EDGE ID NONDIRECTED ID STRINGNAME STYLE STRINGNAME ENDLINE { printf("\t%s -> %s [label=%s, style=%s];", $2, $4, $5, $7); }
| EDGE ID NONDIRECTED ID STYLE STRINGNAME ENDLINE { printf("\t%s -> %s [style=%s];", $2, $4, $6); }
endGraph:
ENDGRAPH ENDLINE
{
printf("}");
}
;
defaultNodeShape:
DEFAULTNODESHAPE STRINGNAME ENDLINE
{
defaultNodeShapeStart = " shape=";
defaultNodeShape = $2;
}
;
defaultNodeStyle:
DEFAULTNODESTYLE STRINGNAME ENDLINE
{
defaultNodeStyleStart = " style=";
defaultNodeStyle = $2;
}
;