dot.y 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* bison 3.7.5 glr code
  2. dot.tab.c:359:18: warning: leak of ‘<unknown>’ [CWE-401] [-Wanalyzer-malloc-leak]
  3. dot.tab.c:2906:10: warning: leak of ‘yystack.yyitems’ [CWE-401] [-Wanalyzer-malloc-leak]
  4. */
  5. /*
  6. * Copyright 2021
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * SPDX-License-Identifier: GPL-3.0+
  22. * License-Filename: LICENSE
  23. */
  24. %{
  25. #include "config.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <zlib.h>
  30. #include "splay-tree.h"
  31. #include "main.h"
  32. #include "dp.h"
  33. #include "dpus.h"
  34. #include "lex.yy.h"
  35. #include "dpmem.h"
  36. #define YY_DEBUG 1
  37. #define YYERROR_VERBOSE 1
  38. /* GNU Bison 3.7.4 bug wrongly uses this attribute not supported in C99 mode */
  39. #undef _Noreturn
  40. #define _Noreturn /**/
  41. /* memory usage wrapping feature */
  42. #ifndef YYFREE
  43. # define YYFREE free
  44. #endif
  45. #ifndef YYMALLOC
  46. # define YYMALLOC malloc
  47. #endif
  48. #ifndef YYREALLOC
  49. # define YYREALLOC realloc
  50. #endif
  51. /* utf8 code at start if 1 */
  52. static int utfseen = 0;
  53. /* graph is strict if 1 */
  54. static int isstrict = 0;
  55. extern int yylex (void);
  56. static void yyerror (const char *msg)
  57. {
  58. if (strlen(dp_errmsg)==0) {
  59. snprintf(dp_errmsg,256-1,"dot %s(): %s at line %d yytext is %s'\n",__func__,msg,yylineno,yytext);
  60. }
  61. printf("dot %s(): %s at line %d yytext is `%s'\n",__func__,msg,yylineno,yytext);
  62. fflush(stdout);
  63. fflush(stderr);
  64. return;
  65. }
  66. %}
  67. /* this is a glr parser. not a yacc lalr parser */
  68. %glr-parser
  69. %union { char *string; struct dpepoint *dp; }
  70. %debug
  71. %defines
  72. %start startdot
  73. %verbose
  74. %token TOKEN_BRACEOPEN "{" /* { */
  75. %token TOKEN_BRACECLOSE "}" /* } */
  76. %token TOKEN_PLUS "+" /* + */
  77. %token TOKEN_COMMA "," /* , */
  78. %token TOKEN_COLON ":" /* : */
  79. %token TOKEN_IS "=" /* = */
  80. %token TOKEN_SC ";" /* ; */
  81. %token TOKEN_BRACKETOPEN "[" /* [ */
  82. %token TOKEN_BRACKETCLOSE "]" /* ] */
  83. %token TOKEN_UTF8BOM "utf8code"
  84. %token TOKEN_STRICT "strict"
  85. %token TOKEN_GRAPH "graph"
  86. %token TOKEN_SUBGRAPH "subgraph"
  87. %token TOKEN_DIGRAPH "digraph"
  88. %token TOKEN_NODE "node"
  89. %token TOKEN_EDGE "edge"
  90. %token <string> TOKEN_TEXT "text"
  91. %token <string> TOKEN_NUM "number"
  92. %token <string> TOKEN_QTEXT "string"
  93. %token <string> TOKEN_HTEXT "<html-string>"
  94. %token <string> TOKEN_EOP "-> or --"
  95. %type <string> text
  96. %type <string> htext
  97. %type <string> ctext
  98. %type <string> thename
  99. %type <string> thetype
  100. %type <string> nidid
  101. %type <dp> nid
  102. %type <dp> sstatement
  103. %left TOKEN_SUBGRAPH
  104. %left TOKEN_BRACEOPEN
  105. %%
  106. startdot:
  107. utf thetype thename { dp_sg ($2,$3); } TOKEN_BRACEOPEN statements TOKEN_BRACECLOSE { dp_eg (); }
  108. | error { dp_eg (); }
  109. | /* empty */ { dp_eg (); }
  110. ;
  111. utf:
  112. TOKEN_UTF8BOM { utfseen = 1; }
  113. | /* empty */ { utfseen = 0; }
  114. ;
  115. thetype:
  116. TOKEN_STRICT TOKEN_GRAPH { isstrict = 1; $$ = (char *) dp_uniqstr((char *) "--"); }
  117. | TOKEN_GRAPH { isstrict = 0; $$ = (char *) dp_uniqstr( (char *) "--"); }
  118. | TOKEN_STRICT TOKEN_DIGRAPH { isstrict = 1; $$ = (char *) dp_uniqstr( (char *) "->"); }
  119. | TOKEN_DIGRAPH { isstrict = 0; $$ = (char *) dp_uniqstr( (char *) "->"); }
  120. ;
  121. /* graph name can be empty */
  122. thename:
  123. text { $$ = $1; }
  124. | /* empty */ { $$ = NULL; }
  125. ;
  126. /* example: "string" or "s"+"t"+"r"+"ing" */
  127. ctext:
  128. TOKEN_QTEXT { $$ = $1; }
  129. | ctext TOKEN_PLUS TOKEN_QTEXT { $$ = dp_ccat ($1,$3); }
  130. ;
  131. /* arg of attribute can be text, "string", "number" */
  132. text:
  133. TOKEN_TEXT { $$ = $1; }
  134. | TOKEN_NUM { $$ = $1; }
  135. | ctext { $$ = $1; }
  136. ;
  137. /* <<html label text special treatement>> */
  138. htext:
  139. TOKEN_HTEXT { $$ = $1; }
  140. ;
  141. statements:
  142. statements statement
  143. | statement
  144. ;
  145. /* the semicolon or comma after statement is optional */
  146. /* parse error if support routines have set a message */
  147. statement:
  148. statement2 { if (dp_chkerr()) { YYERROR; /* YYABORT; does not work as expected */ } }
  149. | statement2 TOKEN_SC { if (dp_chkerr()) { YYERROR; /* YYABORT; does not work as expected */ } }
  150. | statement2 TOKEN_COMMA { if (dp_chkerr()) { YYERROR; /* YYABORT; does not work as expected */ } }
  151. ;
  152. /* node, edge, attribute, subgraph statement
  153. * dot allows attributes after a subgraph statement
  154. * example: subgraph "huh" { ... } [ ... ]
  155. * but not when in a edge, example: {...}[...] -> ...
  156. * bug-or-feature?
  157. */
  158. statement2:
  159. nstatement
  160. | estatement
  161. | astatement
  162. | sstatement { /* $1 is not used here */ /* $1 is read-only object */ (void)dp_free((void *)$1); dp_atype_sgraph(); } oattrib { dp_atype_graph (); }
  163. ;
  164. nstatement:
  165. nidid { dp_mknode0 ($1); dp_atype_node(); } oattrib { dp_atype_graph (); }
  166. ;
  167. /*
  168. * only for node definition and port+compass is ignored
  169. * nodeid
  170. * nodeid:port
  171. * nodeid:compass
  172. * nodeid:port:compass (port as in record/html label)
  173. */
  174. nidid:
  175. text { $$=$1; }
  176. | text TOKEN_COLON text { $$=$1; /* ignore $3 */ }
  177. | text TOKEN_COLON text TOKEN_COLON text { $$=$1; /* ignore $3 $5 */ }
  178. ;
  179. /*
  180. * nodeid
  181. * nodeid:port
  182. * nodeid:compass
  183. * nodeid:port:compass (port as in record/html label)
  184. */
  185. nid:
  186. text { $$ = dp_mknid ($1,NULL,NULL); }
  187. | text TOKEN_COLON text { $$ = dp_mknid ($1,$3,NULL); }
  188. | text TOKEN_COLON text TOKEN_COLON text { $$ = dp_mknid ($1,$3,$5); }
  189. ;
  190. /* attribute value can be html string
  191. * as in label=<<value>>
  192. * the left side of attribute must be text and not a string
  193. */
  194. sattr:
  195. TOKEN_TEXT TOKEN_IS text { /* string as in "string" */ dp_aset ($1,$3,0); }
  196. | TOKEN_TEXT TOKEN_IS htext { /* html string as in <html-label> */ dp_aset ($1,$3,1); }
  197. ;
  198. sattr2:
  199. sattr
  200. | TOKEN_TEXT { dp_aset ((char *)$1,(char *)"true",0); }
  201. ;
  202. iattr:
  203. sattr2 iattr
  204. | sattr2 TOKEN_COMMA iattr
  205. | sattr2 TOKEN_SC iattr
  206. | /* empty */
  207. ;
  208. tattr:
  209. TOKEN_BRACKETOPEN iattr TOKEN_BRACKETCLOSE
  210. ;
  211. oattrib:
  212. tattr oattrib
  213. | /* empty */
  214. ;
  215. estatement:
  216. nid { dp_starte1 ($1); } erhs { dp_newe (); } oattrib { dp_ende (); dp_clrep(); }
  217. | sstatement { dp_starte2 ($1); } erhs { dp_newe (); } oattrib { dp_ende (); dp_clrep(); }
  218. ;
  219. /* right hand */
  220. erhs:
  221. TOKEN_EOP nid { dp_cke ($1); dp_ine ($2); }
  222. | TOKEN_EOP nid { dp_cke ($1); dp_ine ($2); } erhs
  223. | TOKEN_EOP sstatement { dp_cke ($1); dp_ine ($2); }
  224. | TOKEN_EOP sstatement { dp_cke ($1); dp_ine ($2); } erhs
  225. ;
  226. astatement:
  227. atype tattr { dp_atype_graph (); }
  228. | aset { dp_atype_graph (); }
  229. ;
  230. atype:
  231. TOKEN_GRAPH { dp_atype_graphdef (); }
  232. | TOKEN_NODE { dp_atype_nodedef (); }
  233. | TOKEN_EDGE { dp_atype_edgedef (); }
  234. ;
  235. /* attribute value with detection of html style label */
  236. aset:
  237. text TOKEN_IS text { dp_aset ($1,$3,0); }
  238. | text TOKEN_IS htext { dp_aset ($1,$3,1); }
  239. ;
  240. sstatement:
  241. shead TOKEN_BRACEOPEN statements TOKEN_BRACECLOSE %prec TOKEN_BRACEOPEN { $$=dp_endss (); }
  242. | TOKEN_BRACEOPEN { dp_namedsubg (NULL,DP_SG_CO); } statements TOKEN_BRACECLOSE { $$=dp_endss (); }
  243. ;
  244. /* allow subgraph foo {} or subgraph {} */
  245. shead:
  246. TOKEN_SUBGRAPH text { dp_namedsubg ($2,DP_SG_NM); }
  247. | TOKEN_SUBGRAPH { dp_namedsubg (NULL,DP_SG_NN); }
  248. ;
  249. %%
  250. /* end. */