PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wxxt/src/x/wbuild/src/parse.y

http://github.com/mflatt/gr2
Happy | 829 lines | 779 code | 50 blank | 0 comment | 0 complexity | 12bc3acadb9fa75086edb70b580bf804 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, ISC, LGPL-2.0
  1. /*
  2. ** File: parse.y
  3. ** Date: 28 Apr 1992
  4. ** Description: YACC grammar for Widget Builder
  5. ** Author: Bert Bos <bert@let.rug.nl>
  6. ** Modified by: Joel N. Weber II <nemo@nautilus.sub>
  7. */
  8. %{
  9. #include <config.h>
  10. #include <libit/alloca.h>
  11. #include <libit/string.h>
  12. #include <libit/unistd.h>
  13. #if HAVE_STDARG_H
  14. #include <stdarg.h>
  15. #else
  16. #include <varargs.h>
  17. #endif
  18. #include <stdio.h>
  19. #include <wbuild.h>
  20. #include <libit/malloc.h>
  21. #define new(ptr) ptr = calloc(1, sizeof(*(ptr))) /* set to zeros */
  22. #ifdef YYBISON
  23. #define yerr(m) if (! YYRECOVERING()) yyerror(m)
  24. #else
  25. #define yerr(m) yyerror(m)
  26. #endif
  27. /* extern int lineno; */
  28. extern char *yytext;
  29. extern int yyleng;
  30. char *filename = "<undefined>";
  31. time_t filetime;
  32. /* #define alloca malloc */
  33. /* void *alloca (); */
  34. extern int yylex (void);
  35. extern void err (int fatal, char *format,...);
  36. static void yyerror (const char *s);
  37. static void add_section (Section *psect, Section s);
  38. static Section new_section (STRING, Decl, Section);
  39. static char *no_quotes(char *s);
  40. %}
  41. %union {
  42. STRING string;
  43. Doctype doctype;
  44. Class class;
  45. Section section;
  46. Decl decl;
  47. taglist tag;
  48. int i;
  49. struct { char *name; char *open; char *close; } onetag;
  50. struct { int i; char *s; time_t t; } finfo;
  51. }
  52. %token_table
  53. %token <finfo> ACTIONS
  54. %token <finfo> CLASS
  55. %token <finfo> CLASSVARS
  56. %token <finfo> CONSTRAINTS
  57. %token <finfo> DOCTYPE
  58. %token <finfo> PRIVCONSTRAINTS
  59. %token <finfo> DEF
  60. %token <finfo> EXPORTS
  61. %token <finfo> IMPORTS
  62. %token <finfo> INCL
  63. %token <finfo> METHODS
  64. %token <finfo> PRIVATE
  65. %token <finfo> PROC
  66. %token <finfo> PUBLIC
  67. %token <finfo> TAG
  68. %token <finfo> TRANS
  69. %token <finfo> TRANSLATIONS
  70. %token <finfo> TYPE
  71. %token <finfo> UTILITIES
  72. %token <finfo> VAR
  73. %token <string> BODY
  74. %token <string> BRACKETED
  75. %token <string> COMMENT
  76. %token <string> CSTRING
  77. %token <string> CTOK
  78. %token <string> IDENT
  79. %token <string> NUMBER
  80. %token <string> TEXT
  81. %token <string> DOTDOTDOT
  82. %token COLON
  83. %token COMMA
  84. %token DIRECTORY
  85. %token DOLLAR
  86. %token EQUALS
  87. %token EXCLAM
  88. %token EXTENSION
  89. %token FILE_OPT
  90. %token GUARD
  91. %token GUARDP
  92. %token ILL_BODY
  93. %token ILL_CHAR
  94. %token INCL
  95. %token LBRACK
  96. %token LPAR
  97. %token NOCODE
  98. %token NODOC
  99. %token PLUS
  100. %token RBRACK
  101. %token RPAR
  102. %token SEMI
  103. %token SHORTDOC
  104. %token SLASH
  105. %token STAR
  106. %token TILDE
  107. %token TYPE
  108. %token UNKNOWN
  109. %type <doctype> doctype
  110. %type <tag> docopts
  111. %type <onetag> docopt
  112. %type <class> class
  113. %type <class> sections
  114. %type <decl> actiondef
  115. %type <decl> classvardecl
  116. %type <decl> constraintdef
  117. %type <decl> privconstraintdef
  118. %type <decl> exportdef
  119. %type <decl> importdecl
  120. %type <decl> methoddef
  121. %type <decl> more_params
  122. %type <decl> paramlist
  123. %type <decl> params
  124. %type <decl> privatedecl
  125. %type <decl> pubvardef
  126. %type <decl> transline
  127. %type <decl> type_and_name
  128. %type <decl> utildef
  129. %type <section> actions
  130. %type <section> actionsections
  131. %type <section> actiontextsections
  132. %type <section> classvars
  133. %type <section> classvarsections
  134. %type <section> classvartextsections
  135. %type <section> constraints
  136. %type <section> constraintsections
  137. %type <section> constrainttextsections
  138. %type <section> privconstraints
  139. %type <section> privconstraintsections
  140. %type <section> privconstrainttextsections
  141. %type <section> exports
  142. %type <section> exportsections
  143. %type <section> exporttextsections
  144. %type <section> imports
  145. %type <section> importsections
  146. %type <section> importtextsections
  147. %type <section> methods
  148. %type <section> methodsections
  149. %type <section> methodtextsections
  150. %type <section> privatesections
  151. %type <section> privatetextsections
  152. %type <section> privatevars
  153. %type <section> publicvars
  154. %type <section> pubvarsections
  155. %type <section> pubvartextsections
  156. %type <section> translations
  157. %type <section> transsections
  158. %type <section> transtextsections
  159. %type <section> utilities
  160. %type <section> utilsections
  161. %type <section> utiltextsections
  162. %type <string> C_stuff
  163. %type <string> C_thing
  164. %type <string> actioncall
  165. %type <string> actioncalls
  166. %type <string> arguments
  167. %type <string> class_id
  168. %type <string> count
  169. %type <string> detail
  170. %type <string> event
  171. %type <string> macro_stuff
  172. %type <string> mod_syms
  173. %type <string> modifiers
  174. %type <string> modifier
  175. %type <string> more_arguments
  176. %type <string> more_events
  177. %type <string> stars
  178. %type <string> superclass
  179. %type <string> supername
  180. %type <string> symbol
  181. %%
  182. program
  183. : class program {add_class($1);}
  184. | doctype program {add_doctype($1);}
  185. | myerror program {yerr("error in class");}
  186. | /* empty */
  187. ;
  188. doctype
  189. : DOCTYPE SHORTDOC docopts {new($$); $$->shortdoc = 1;
  190. copy_taglist(&($$->tag), &($3));
  191. $$->next = 0;}
  192. | DOCTYPE docopts {new($$); $$->shortdoc = 0;
  193. copy_taglist(&($$->tag), &($2));
  194. $$->next = 0;}
  195. ;
  196. docopts
  197. : docopts docopt {copy_taglist(&($$), &($1));
  198. set_doctag(($$), $2.name, $2.open,
  199. $2.close);}
  200. | {zero_taglist(&($$));}
  201. ;
  202. docopt
  203. : TAG IDENT CSTRING CSTRING {$$.name = get($2);
  204. $$.open = get($3);
  205. $$.close = get($4); }
  206. ;
  207. class
  208. : CLASS IDENT superclass
  209. options TEXT sections {$$ = $6; $$->name = $2; $$->superclass = $3;
  210. $$->lineno = $1.i; $$->text = $5;
  211. $$->nocode = classnocode;
  212. $$->nodoc = classnodoc;
  213. $$->filename = hash($1.s);
  214. $$->filenamepart = classfilename;
  215. $$->filetime = $1.t;
  216. classnocode = classnodoc = 0;
  217. classfilename = 0;}
  218. | CLASS IDENT superclass
  219. options sections {$$ = $5; $$->name = $2; $$->superclass = $3;
  220. $$->lineno = $1.i;
  221. $$->nocode = classnocode;
  222. $$->nodoc = classnodoc;
  223. $$->filename = hash($1.s);
  224. $$->filenamepart = classfilename;
  225. $$->filetime = $1.t;
  226. classnocode = classnodoc = 0;
  227. classfilename = 0;}
  228. ;
  229. superclass
  230. : LPAR supername RPAR {$$ = $2;}
  231. | LPAR myerror RPAR {yerr("name of superclass expected");
  232. $$ = NULL;}
  233. | /* empty */ {$$ = NULL;}
  234. ;
  235. supername
  236. : IDENT {$$ = $1;}
  237. | supername SLASH IDENT {$$ = catstr(3, get($1), "/", get($3));
  238. delete($1); delete($3);}
  239. ;
  240. options
  241. : options option {;}
  242. | /* empty */ {;}
  243. ;
  244. option
  245. : NOCODE {classnocode = 1; }
  246. | NODOC {classnodoc = 1; }
  247. | FILE_OPT EQUALS IDENT {classfilename = hdup($3); }
  248. | FILE_OPT EQUALS CSTRING {char *s; s = no_quotes(get($3));
  249. classfilename = hash(s); free(s); delete($3);}
  250. ;
  251. sections
  252. : sections classvars {$$ = $1; add_section(&$$->classvars, $2);}
  253. | sections publicvars {$$ = $1; add_section(&$$->publicvars, $2);}
  254. | sections privatevars {$$ = $1; add_section(&$$->privatevars, $2);}
  255. | sections constraints {$$ = $1; add_section(&$$->constraints, $2);}
  256. | sections privconstraints {$$ = $1; add_section(&$$->privconstr, $2);}
  257. | sections methods {$$ = $1; add_section(&$$->methods, $2);}
  258. | sections actions {$$ = $1; add_section(&$$->actions, $2);}
  259. | sections translations {$$ = $1; add_section(&$$->translations, $2);}
  260. | sections imports {$$ = $1; add_section(&$$->imports, $2);}
  261. | sections exports {$$ = $1; add_section(&$$->exports, $2);}
  262. | sections utilities {$$ = $1; add_section(&$$->utilities, $2);}
  263. | /* empty */ {new($$);}
  264. ;
  265. publicvars
  266. : PUBLIC pubvarsections {$$ = $2;}
  267. ;
  268. pubvarsections
  269. : pubvardef pubvarsections {$$ = new_section(0, $1, $2);}
  270. | pubvartextsections
  271. ;
  272. pubvartextsections
  273. : TEXT pubvardef pubvarsections {$$ = new_section($1, $2, $3);}
  274. | TEXT pubvartextsections {$$ = new_section($1, (Decl) NULL, $2);}
  275. | /* empty */ {$$ = NULL;}
  276. ;
  277. pubvardef
  278. : VAR symbol type_and_name
  279. symbol EQUALS symbol
  280. C_stuff {$$ = $3; $$->typesym = $2; $$->valuesym = $6;
  281. $$->value = $7; $$->namesym = $4; $$->lineno =
  282. $1.i; $$->tp = Var;}
  283. | DEF IDENT params EQUALS
  284. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  285. $$->body = $5; $$->params = $3; $$->lineno =
  286. $1.i;}
  287. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  288. ;
  289. opt_semi
  290. : SEMI
  291. | /* empty */
  292. ;
  293. symbol
  294. : BRACKETED {$$ = $1;}
  295. | /* empty */ {$$ = NULL;}
  296. ;
  297. constraints
  298. : CONSTRAINTS
  299. constraintsections {$$ = $2;}
  300. ;
  301. constraintsections
  302. : constraintdef
  303. constraintsections {$$ = new_section(0, $1, $2);}
  304. | constrainttextsections
  305. ;
  306. constrainttextsections
  307. : TEXT constraintdef
  308. constraintsections {$$ = new_section($1, $2, $3);}
  309. | TEXT constrainttextsections {$$ = new_section($1, (Decl) NULL, $2);}
  310. | /* empty */ {$$ = NULL;}
  311. ;
  312. constraintdef
  313. : VAR symbol type_and_name
  314. symbol EQUALS symbol
  315. C_stuff {$$ = $3; $$->typesym = $2; $$->valuesym = $6;
  316. $$->value = $7; $$->namesym = $4; $$->lineno =
  317. $1.i; $$->tp = Var;}
  318. | DEF IDENT params EQUALS
  319. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  320. $$->body = $5; $$->params = $3; $$->lineno =
  321. $1.i;}
  322. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  323. ;
  324. privconstraints
  325. : PRIVCONSTRAINTS
  326. privconstraintsections {$$ = $2;}
  327. ;
  328. privconstraintsections
  329. : privconstraintdef
  330. privconstraintsections {$$ = new_section(0, $1, $2);}
  331. | privconstrainttextsections
  332. ;
  333. privconstrainttextsections
  334. : TEXT privconstraintdef
  335. privconstraintsections {$$ = new_section($1, $2, $3);}
  336. | TEXT
  337. privconstrainttextsections {$$ = new_section($1, (Decl) NULL, $2);}
  338. | /* empty */ {$$ = NULL;}
  339. ;
  340. privconstraintdef
  341. : VAR type_and_name opt_semi {$$ = $2; $$->lineno = $1.i; $$->tp = Var;}
  342. | DEF IDENT params EQUALS
  343. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  344. $$->body = $5; $$->params = $3; $$->lineno =
  345. $1.i;}
  346. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  347. ;
  348. actions
  349. : ACTIONS actionsections {$$ = $2;}
  350. ;
  351. actionsections
  352. : actiondef actionsections {$$ = new_section(0, $1, $2);}
  353. | actiontextsections
  354. ;
  355. actiontextsections
  356. : TEXT actiondef
  357. actionsections {$$ = new_section($1, $2, $3);}
  358. | TEXT actiontextsections {$$ = new_section($1, (Decl) NULL, $2);}
  359. | /* empty */ {$$ = NULL;}
  360. ;
  361. actiondef
  362. : PROC IDENT BODY {new($$); $$->name = $2; $$->body = $3;
  363. $$->lineno = $1.i; $$->tp = Proc;}
  364. | PROC myerror BODY {yerr("error in action name"); $$ = NULL;}
  365. | DEF IDENT params EQUALS
  366. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  367. $$->body = $5; $$->params = $3; $$->lineno =
  368. $1.i;}
  369. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  370. ;
  371. translations
  372. : TRANSLATIONS transsections {$$ = $2;}
  373. ;
  374. transsections
  375. : transline transsections {$$ = new_section(0, $1, $2);}
  376. | transtextsections
  377. ;
  378. transtextsections
  379. : TEXT transline transsections {$$ = new_section($1, $2, $3);}
  380. | TEXT transtextsections {$$ = new_section($1, (Decl) NULL, $2);}
  381. | /* empty */ {$$ = NULL;}
  382. ;
  383. transline
  384. : TRANS modifiers
  385. event more_events count
  386. COLON actioncalls opt_semi {new($$); $$->tp = Trans; $$->type =
  387. catstr(4, get($2), get($3), get($4), get($5));
  388. $$->value = $7; $$->lineno = $1.i;
  389. delete($2); delete($3);
  390. delete($4); delete($5);}
  391. | TRANS myerror COLON
  392. actioncalls opt_semi {yerr("error before ':' in translation"); $$ =
  393. NULL;}
  394. ;
  395. mod_syms
  396. : COLON {$$ = hash(":");}
  397. | EXCLAM {$$ = hash("!");}
  398. | TILDE {$$ = hash("~");}
  399. | /* empty */ {$$ = 0;}
  400. ;
  401. modifiers
  402. : modifier modifiers {$$ = catstr(2, get($1), get($2)); delete($1);
  403. delete($2);}
  404. | /* empty */ {$$ = 0;}
  405. ;
  406. modifier
  407. : mod_syms IDENT {$$ = catstr(2, get($1), get($2)); delete($1);
  408. delete($2);}
  409. ;
  410. event
  411. : BRACKETED detail {$$ = catstr(4, "<", get($1), ">", get($2));
  412. delete($1); delete($2);}
  413. | CSTRING {$$ = $1;}
  414. ;
  415. more_events
  416. : COMMA event more_events {$$ = catstr(3, ",", get($2), get($3));
  417. delete($2); delete($3);}
  418. | /* empty */ {$$ = 0;}
  419. ;
  420. count
  421. : LPAR NUMBER RPAR {$$ = catstr(3, "(", get($2), ")");
  422. delete($2);}
  423. | LPAR NUMBER PLUS RPAR {$$ = catstr(3, "(", get($2), "+)");
  424. delete($2);}
  425. | LPAR myerror RPAR {yerr("count expected after '('");
  426. $$ = 0;}
  427. | /* empty */ {$$ = 0;}
  428. ;
  429. detail
  430. : IDENT {$$ = $1;}
  431. | /* empty */ {$$ = 0;}
  432. ;
  433. actioncalls
  434. : actioncall actioncalls {$$ = catstr(2, get($1), get($2)); delete($1);
  435. delete($2);}
  436. | actioncall {$$ = $1;}
  437. ;
  438. actioncall
  439. : IDENT LPAR arguments RPAR {$$ = catstr(4, get($1), "(", get($3), ") ");
  440. delete($1); delete($3);}
  441. | IDENT LPAR myerror RPAR {yerr("error in argument");
  442. $$ = NULL;}
  443. ;
  444. arguments
  445. : CSTRING more_arguments {char *s; s = no_quotes(get($1)); delete($1);
  446. $$ = catstr(2, s, get($2)); free(s);
  447. delete($2);}
  448. | /* empty */ {$$ = NULL;}
  449. ;
  450. more_arguments
  451. : COMMA CSTRING more_arguments {char *s; s = no_quotes(get($2)); delete($2);
  452. $$ = catstr(3, ",", s, get($3)); free(s);
  453. delete($3);}
  454. /* The arguments shouldn't be quoted... */
  455. | /* empty */ {$$ = NULL;}
  456. ;
  457. methods
  458. : METHODS methodsections {$$ = $2;}
  459. ;
  460. methodsections
  461. : methoddef methodsections {$$ = new_section(0, $1, $2);}
  462. | methodtextsections
  463. ;
  464. methodtextsections
  465. : TEXT methoddef
  466. methodsections {$$ = new_section($1, $2, $3);}
  467. | TEXT methodtextsections {$$ = new_section($1, (Decl) NULL, $2);}
  468. | /* empty */ {$$ = NULL;}
  469. ;
  470. methoddef
  471. : PROC type_and_name
  472. params BODY {$$ = $2; $$->params = $3; $$->body = $4;
  473. $$->lineno = $1.i; $$->tp = Proc;}
  474. | PROC myerror {$$ = NULL; yerr("error in method");}
  475. | PROC type_and_name
  476. params ILL_BODY {$$ = NULL; yerr("missing closing brace");}
  477. | DEF IDENT params EQUALS
  478. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  479. $$->body = $5; $$->params = $3; $$->lineno =
  480. $1.i;}
  481. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  482. ;
  483. type_and_name
  484. : type_and_name stars
  485. class_id IDENT {STRING h; $$ = $1;
  486. if ($$->type) {
  487. h = catstr(4, get($$->type), " ",
  488. get($$->name), get($2)); delete($$->type);
  489. $$->type = h; delete($$->name);
  490. } else {
  491. $$->type = catstr(2, get($$->name), get($2));
  492. delete($$->name);
  493. }
  494. $$->class_id = $3; $$->name = $4;}
  495. | type_and_name LBRACK RBRACK {STRING h; $$ = $1; h = catstr(2,
  496. get($$->suffix), "[]"); delete($$->suffix);
  497. $$->suffix = h;}
  498. | type_and_name
  499. LBRACK IDENT RBRACK {STRING h; $$ = $1; h = catstr(4,
  500. get($$->suffix), "[", get($3), "]");
  501. delete($3); delete($$->suffix);
  502. $$->suffix = h;}
  503. | type_and_name
  504. LBRACK NUMBER RBRACK {STRING h; $$ = $1; h = catstr(4,
  505. get($$->suffix), "[", get($3), "]");
  506. delete($3); delete($$->suffix);
  507. $$->suffix = h;}
  508. | class_id IDENT {new($$); $$->name = $2; $$->class_id = $1;
  509. $$->suffix = 0;}
  510. | myerror IDENT {yerr("error in type expression"); new($$);}
  511. ;
  512. stars
  513. : stars STAR {$$ = catstr(2, get($1), "*");}
  514. | /* empty */ {$$ = hash(" ");}
  515. ;
  516. class_id
  517. : DOLLAR LPAR IDENT RPAR {$$ = $3;}
  518. | DOLLAR {$$ = hash("$");}
  519. | /* empty */ {$$ = 0;}
  520. ;
  521. exports
  522. : EXPORTS exportsections {$$ = $2;}
  523. ;
  524. exportsections
  525. : exportdef exportsections {$$ = new_section(0, $1, $2);}
  526. | exporttextsections
  527. ;
  528. exporttextsections
  529. : TEXT exportdef
  530. exportsections {$$ = new_section($1, $2, $3);}
  531. | TEXT exporttextsections {$$ = new_section($1, (Decl) NULL, $2);}
  532. | /* empty */ {$$ = NULL;}
  533. ;
  534. exportdef
  535. : INCL BRACKETED {new($$); $$->lineno = $1.i; $$->name =
  536. catstr(3, "<", get($2), ">"); $$->tp = Incl;
  537. delete($2);}
  538. | INCL CSTRING {new($$); $$->lineno = $1.i; $$->name = $2;
  539. $$->tp = Incl;}
  540. | TYPE IDENT EQUALS C_stuff {new($$); $$->lineno = $1.i; $$->type = $4;
  541. $$->name = $2; $$->tp = Type;}
  542. | TYPE myerror EQUALS C_stuff {$4 = NULL;
  543. yerr("should be one identifier before '='");}
  544. | VAR type_and_name opt_semi {$$ = $2; $$->lineno = $1.i; $$->tp = Var;}
  545. | VAR type_and_name EQUALS
  546. C_stuff {$$ = $2; $$->lineno = $1.i; $$->tp = Var;
  547. $$->value = $4;}
  548. | PROC type_and_name
  549. params BODY {$$ = $2; $$->params = $3; $$->body = $4;
  550. $$->lineno = $1.i; $$->tp = Proc;}
  551. | PROC myerror BODY {$$ = NULL; yerr("error in function heading");}
  552. | DEF IDENT params EQUALS
  553. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  554. $$->body = $5; $$->params = $3; $$->lineno =
  555. $1.i;}
  556. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  557. ;
  558. macro_stuff
  559. : LBRACK macro_stuff {$$ = catstr(2, "[", get($2)); delete($2);}
  560. | LPAR macro_stuff {$$ = catstr(2, "(", get($2)); delete($2);}
  561. | C_stuff {$$ = $1;}
  562. | /* empty */ {$$ = 0;}
  563. ;
  564. C_stuff
  565. : C_stuff C_thing {$$ = catstr(2, get($1), get($2)); delete($1);
  566. delete($2);}
  567. | C_thing {$$ = $1;}
  568. ;
  569. C_thing
  570. : C_thing LPAR {$$ = catstr(2, get($1), "("); delete($1);}
  571. | IDENT LBRACK {$$ = catstr(2, get($1), "["); delete($1);}
  572. | IDENT {$$ = catstr(2, get($1), " "); delete($1);}
  573. | STAR {$$ = hash("*");}
  574. | TILDE {$$ = hash("~");}
  575. | EXCLAM {$$ = hash("!");}
  576. | COLON {$$ = hash(":");}
  577. | SEMI {$$ = hash(";");}
  578. | RBRACK {$$ = hash("]");}
  579. | BODY {$$ = $1;}
  580. | RPAR {$$ = hash(")");}
  581. | RPAR LBRACK {$$ = hash(")[");}
  582. | COMMA {$$ = hash(",");}
  583. | NUMBER {$$ = catstr(2, get($1), " "); delete($1);}
  584. | CSTRING {$$ = $1;}
  585. | EQUALS {$$ = hash("=");}
  586. | PLUS {$$ = hash("+");}
  587. | SLASH {$$ = hash("/");}
  588. | CTOK {$$ = $1;}
  589. | COMMENT {$$ = $1;}
  590. | DOLLAR {$$ = hash("$");}
  591. ;
  592. params
  593. : LPAR paramlist RPAR {$$ = $2;}
  594. | LPAR myerror RPAR {yerr("error in parameter list");$$ = NULL;}
  595. | /* empty */ {$$ = NULL;}
  596. ;
  597. paramlist
  598. : type_and_name more_params {$$ = $1; $$->next = $2;}
  599. | DOLLAR more_params {new($$); $$->name = hash("$"); $$->next = $2;}
  600. | /* empty */ {$$ = NULL;}
  601. ;
  602. more_params
  603. : COMMA type_and_name
  604. more_params {$$ = $2; $$->next = $3;}
  605. | COMMA DOLLAR more_params {new($$); $$->name = hash("$"); $$->next = $3;}
  606. | COMMA DOTDOTDOT {new($$); $$->name = hash("...");}
  607. | /* empty */ {$$ = NULL;}
  608. ;
  609. imports
  610. : IMPORTS importsections {$$ = $2;}
  611. ;
  612. importsections
  613. : importdecl importsections {$$ = new_section(0, $1, $2);}
  614. | importtextsections
  615. ;
  616. importtextsections
  617. : TEXT importdecl
  618. importsections {$$ = new_section($1, $2, $3);}
  619. | TEXT importtextsections {$$ = new_section($1, (Decl) NULL, $2);}
  620. | /* empty */ {$$ = NULL;}
  621. ;
  622. importdecl
  623. : INCL BRACKETED {new($$); $$->lineno = $1.i; $$->name =
  624. catstr(3, "<", get($2), ">"); $$->tp = Incl;
  625. delete($2);}
  626. | INCL CSTRING {new($$); $$->lineno = $1.i; $$->name = $2;
  627. $$->tp = Incl;}
  628. | VAR type_and_name opt_semi {$$ = $2; $$->lineno = $1.i; $$->tp = Var;}
  629. | PROC type_and_name
  630. params opt_semi {$$ = $2; $$->params = $3; $$->lineno = $1.i;
  631. $$->tp = Proc;}
  632. ;
  633. utilities
  634. : UTILITIES utilsections {$$ = $2;}
  635. ;
  636. utilsections
  637. : utildef utilsections {$$ = new_section(0, $1, $2);}
  638. | utiltextsections
  639. ;
  640. utiltextsections
  641. : TEXT utildef utilsections {$$ = new_section($1, $2, $3);}
  642. | TEXT utiltextsections {$$ = new_section($1, (Decl) NULL, $2);}
  643. | /* empty */ {$$ = NULL;}
  644. ;
  645. utildef
  646. : TYPE IDENT EQUALS C_stuff {new($$); $$->lineno = $1.i; $$->type = $4;
  647. $$->tp = Type; $$->name = $2;}
  648. | TYPE myerror EQUALS C_stuff {$4 = NULL;
  649. yerr("should be one identifier before '='");}
  650. | VAR type_and_name opt_semi {$$ = $2; $$->lineno = $1.i; $$->tp = Var;}
  651. | VAR type_and_name EQUALS
  652. C_stuff {$$ = $2; $$->lineno = $1.i; $$->tp = Var;
  653. $$->value = $4;}
  654. | PROC type_and_name
  655. params BODY {$$ = $2; $$->params = $3; $$->body = $4;
  656. $$->lineno = $1.i; $$->tp = Proc;}
  657. | PROC myerror BODY {$$ = NULL; yerr("error in function heading");}
  658. | DEF IDENT params EQUALS
  659. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  660. $$->body = $5; $$->params = $3; $$->lineno =
  661. $1.i;}
  662. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  663. ;
  664. classvars
  665. : CLASSVARS classvarsections {$$ = $2;}
  666. ;
  667. classvarsections
  668. : classvardecl
  669. classvarsections {$$ = new_section(0, $1, $2);}
  670. | classvartextsections
  671. ;
  672. classvartextsections
  673. : TEXT classvardecl
  674. classvarsections {$$ = new_section($1, $2, $3);}
  675. | TEXT classvartextsections {$$ = new_section($1, (Decl) NULL, $2);}
  676. | /* empty */ {$$ = NULL;}
  677. ;
  678. classvardecl
  679. : VAR type_and_name EQUALS
  680. C_stuff {$$ = $2; $$->value = $4; $$->lineno = $1.i;
  681. $$->tp = Var;}
  682. | VAR myerror EQUALS C_stuff {$$ = NULL; yerr("error in class variable");}
  683. | DEF IDENT params EQUALS
  684. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  685. $$->body = $5; $$->params = $3; $$->lineno =
  686. $1.i;}
  687. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  688. | TYPE IDENT EQUALS C_stuff {new($$); $$->lineno = $1.i; $$->type = $4;
  689. $$->tp = Type; $$->name = $2;}
  690. ;
  691. privatevars
  692. : PRIVATE privatesections {$$ = $2;}
  693. ;
  694. privatesections
  695. : privatedecl privatesections {$$ = new_section(0, $1, $2);}
  696. | privatetextsections
  697. ;
  698. privatetextsections
  699. : TEXT privatedecl
  700. privatesections {$$ = new_section($1, $2, $3);}
  701. | TEXT privatetextsections {$$ = new_section($1, (Decl) NULL, $2);}
  702. | /* empty */ {$$ = NULL;}
  703. ;
  704. privatedecl
  705. : VAR type_and_name opt_semi {$$ = $2; $$->lineno = $1.i; $$->tp = Var;}
  706. | DEF IDENT params EQUALS
  707. macro_stuff {new($$); $$->name = $2; $$->tp = Def;
  708. $$->body = $5; $$->params = $3; $$->lineno =
  709. $1.i;}
  710. | DEF myerror macro_stuff {$$ = NULL;yerr("incorrect macro definition");}
  711. | TYPE IDENT EQUALS C_stuff {new($$); $$->lineno = $1.i; $$->type = $4;
  712. $$->tp = Type; $$->name = $2;}
  713. ;
  714. myerror
  715. : error ILL_CHAR {yerr("illegal character");}
  716. | error UNKNOWN {yerr("unknown keyword");}
  717. | error ILL_BODY {yerr("missing closing brace");}
  718. | error {yerr("syntax error");}
  719. ;
  720. %%
  721. /*
  722. * new_section -- allocate a new section and initialize it
  723. */
  724. static Section new_section(STRING text, Decl decl, Section next)
  725. {
  726. Section h;
  727. new(h);
  728. h->text = text;
  729. h->decl = decl;
  730. h->next = next;
  731. return h;
  732. }
  733. /*
  734. * yyerror -- write error message to screen
  735. * for this to work, the parser must have been generated with option -t
  736. */
  737. #ifdef __STDC__
  738. static void yyerror(const char *s)
  739. #else
  740. static void yyerror(s)
  741. char *s;
  742. #endif
  743. {
  744. # if defined(YYBISON) && defined(YYDEBUG)
  745. char temp[20];
  746. (void) strncpy(temp, yytext, 15);
  747. if (yyleng > 15) (void) strcpy(temp + 15, "...");
  748. err(0, "%s at `%s' (token = %s)\n",
  749. s, temp, yytname[YYTRANSLATE(yychar)]);
  750. # else /* YYBISON && YYDEBUG*/
  751. err(0, "%s\n", s);
  752. # endif /* YYBISON && YYDEBUG */
  753. }
  754. /*
  755. * add_section -- add a section to a class, append if already set
  756. */
  757. static void add_section(psect, s)
  758. Section *psect;
  759. Section s;
  760. {
  761. Section p;
  762. if (*psect == NULL) {
  763. *psect = s;
  764. } else {
  765. for (p = *psect; p->next; p = p->next) ; /* skip */
  766. p->next = s;
  767. }
  768. }
  769. /*
  770. * no_quotes -- make a copy of a string, but remove the quotes
  771. */
  772. static char * no_quotes(s)
  773. char *s;
  774. {
  775. char *t;
  776. t = strdup(s + 1);
  777. t[strlen(t)-1] = '\0';
  778. return t;
  779. }