Guest User

Untitled

a guest
May 27th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. YACC=bison
  2. YFLAGS=-t
  3. CFLAGS=-g
  4. LEX=flex
  5.  
  6. .SUFFIXES: _lex.c _yacc.c
  7.  
  8.  
  9. .l_lex.c :
  10. $(LEX) $(LFLAGS) -t $< > $@
  11.  
  12. %.c : %.y
  13. # Cancel this rule, and create our own
  14.  
  15. .y_yacc.c :
  16. $(YACC) $(YFLAGS) -d $< -o $@
  17.  
  18. all: olmenu
  19.  
  20. olmenu_lex_test.c : olmenu.l olmenu_tokens.y
  21. $(YACC) $(YFLAGS) -d olmenu_tokens.y
  22. mv olmenu_tokens.tab.h olmenu_yacc.h
  23. $(LEX) -d -t olmenu.l > $@
  24.  
  25. olmenu_lex_test : olmenu_lex_test.c
  26. $(CC) $(CFLAGS) -DTEST_LEX $< -o $@ -ll
  27.  
  28. olmenu-proto1_yacc.c : olmenu-proto1.y
  29. $(YACC) $(YFLAGS) -d $< -o $@
  30. mv olmenu-proto1_yacc.h olmenu_yacc.h
  31.  
  32. olmenu-proto1 : olmenu-proto1_yacc.o olmenu_lex.o
  33. $(CC) -o $@ olmenu-proto1_yacc.o olmenu_lex.o
  34.  
  35. olmenu-proto2_yacc.c : olmenu-proto2.y
  36. $(YACC) $(YFLAGS) -d $< -o $@
  37. mv olmenu-proto2_yacc.h olmenu_yacc.h
  38.  
  39. olmenu-proto2 : olmenu-proto2_yacc.o olmenu_lex.o
  40. $(CC) -o $@ olmenu-proto2_yacc.o olmenu_lex.o
  41.  
  42. olmenu : olmenu_yacc.o olmenu_lex.o dtwmrc.o
  43. $(CC) -o $@ olmenu_yacc.o olmenu_lex.o dtwmrc.o
  44.  
  45. olmenu_yacc.o : olmenu_yacc.c olmenu.h
  46.  
  47. olmenu_lex.o : olmenu_lex.c olmenu_yacc.h
  48.  
  49. dtwmrc.o : dtwmrc.c olmenu.h olmenu_yacc.h
  50.  
  51. clean :
  52. rm -f *_yacc.[ch] *_lex.c *.o *.tab.[ch] *_yacc.output olmenu_lex_test.c
  53.  
  54. ```
  55.  
  56. when invoking ```make olmenu-proto1```, it prints:
  57. ```
  58. bison -t -d olmenu-proto1.y -o olmenu-proto1_yacc.c
  59. mv olmenu-proto1_yacc.h olmenu_yacc.h
  60. cc -g -c -o olmenu-proto1_yacc.o olmenu-proto1_yacc.c
  61. olmenu-proto1.y: In function ‘main’:
  62. olmenu-proto1.y:154:25: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
  63. exit(1);
  64. ^
  65. flex -t olmenu.l > olmenu_lex.c
  66. cc -g -c -o olmenu_lex.o olmenu_lex.c
  67. olmenu.l: In function ‘yylex’:
  68. olmenu.l:109:8: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  69. <ENV>[^$]+ return((int)yytext);
  70. ^
  71. olmenu.l:119:27: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  72. if (s!=NULL) return((int)s);
  73. ^
  74. olmenu.l:125:27: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  75. if (s!=NULL) return((int)s);
  76. ^
  77. olmenu.l: In function ‘expand_env’:
  78. olmenu.l:185:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  79. seglen = strlen(segment);
  80. ^
  81. cc -o olmenu-proto1 olmenu-proto1_yacc.o olmenu_lex.o
  82.  
  83. /*olmenu.l*/
  84. %{
  85. #include "olmenu_yacc.h"
  86. #include <math.h>
  87. #include <string.h>
  88.  
  89. #ifndef YYLTYPE_IS_DECLARED
  90. typedef struct { int first_line; } yyltype;
  91. #define YYLTYPE yyltype
  92. #endif
  93.  
  94. #ifdef TEST_LEX
  95. YYSTYPE yylval;
  96. YYLTYPE yylloc;
  97. #else
  98. extern YYLTYPE yylloc;
  99. #define YY_USER_INIT yylloc.first_line=1;
  100. #endif
  101. static char rcsid[] = "$Id: olmenu.l,v 1.1.1.1 1999/09/26 12:34:39 mib Exp $";
  102. %}
  103. %x CMD
  104. %s ACT
  105. %x ENV ENV1 ENV2
  106. ID ([A-Za-z_$/.0-9-]+)
  107. QSTR ("[^n"]*")
  108. %%
  109. BACK_SELN return(BACK_SELN);
  110. COLUMNS return(COLUMNS);
  111. DEFAULT return(DEFAULT);
  112. DIRMENU return(DIRMENU);
  113. END return(END);
  114. EXIT return(EXIT);
  115. EXIT_NO_CONFIRM return(EXIT_NO_CONFIRM);
  116. FLIPDRAG return(FLIPDRAG);
  117. FLIPFOCUS return(FLIPFOCUS);
  118. FULL_RESTORE_SIZE_SELN return(FULL_RESTORE_SIZE_SELN);
  119. INCLUDE return(INCLUDE);
  120. MENU return(MENU);
  121. MOVE_DESKTOP return(MOVE_DESKTOP);
  122. NOP return(NOP);
  123. OPEN_CLOSE_SELN return(OPEN_CLOSE_SELN);
  124. QUIT_SELN return(QUIT_SELN);
  125. PIN return(PIN);
  126. PROPERTIES return(PROPERTIES);
  127. REFRESH return(REFRESH);
  128. REREAD_MENU_FILE return(REREAD_MENU_FILE);
  129. RESTART return(RESTART);
  130. SAVE_WORKSPACE return(SAVE_WORKSPACE);
  131. SEPARATOR return(SEPARATOR);
  132. START_DSDM return(START_DSDM);
  133. STICK_UNSTICK_SELN return(STICK_UNSTICK_SELN);
  134. STOP_DSDM return(STOP_DSDM);
  135. TITLE return(TITLE);
  136. WINMENU return(WINMENU);
  137. WMEXIT return(WMEXIT);
  138. [0-9]+ {
  139. yylval.num = atoi(yytext);
  140. return(INT);
  141. }
  142.  
  143. <ACT>({ID}|{QSTR})+ {
  144. BEGIN CMD;
  145. yymore();
  146. }
  147.  
  148. ({ID}|{QSTR})+ {
  149. BEGIN ACT;
  150. yylval.str = strdup(yytext);
  151. return(LABEL); /* Label, with quotes */
  152. }
  153.  
  154. <CMD>\n {
  155. yylloc.first_line++;
  156. yymore();
  157. }
  158.  
  159. <CMD>n {
  160. yylval.str = strdup(yytext);
  161. yylval.str[yyleng-1] = '';
  162. yyless(yyleng-1);
  163. BEGIN 0;
  164. return(EXEC);
  165. }
  166.  
  167. <CMD>.$ {
  168. yylval.str = strdup(yytext);
  169. BEGIN 0;
  170. return(EXEC);
  171. }
  172.  
  173. <CMD>. yymore();
  174.  
  175. "<" return(*yytext);
  176.  
  177. ">" { BEGIN ACT; return(*yytext); }
  178.  
  179. #.* ;
  180.  
  181. " " |
  182.  
  183. t ;
  184.  
  185. n { yylloc.first_line++; BEGIN 0; return(*yytext); }
  186.  
  187. . {
  188. BEGIN CMD;
  189. yymore();
  190. }
  191.  
  192. <ENV>[^$]+ return((int)yytext);
  193.  
  194. <ENV>"$"/[A-Za-z0-9_] BEGIN ENV1;
  195.  
  196. <ENV>"${" BEGIN ENV2;
  197.  
  198. <ENV1>[A-Za-z0-9_]+ {
  199. char *s;
  200. s = getenv(yytext);
  201. BEGIN ENV;
  202. if (s!=NULL) return((int)s);
  203. }
  204.  
  205. <ENV2>[^}]+ {
  206. char *s;
  207. s = getenv(yytext);
  208. if (s!=NULL) return((int)s);
  209. }
  210.  
  211. <ENV2>"}" BEGIN ENV;
  212.  
  213. <ENV,ENV1,ENV2>.|n { BEGIN ENV; yymore(); }
  214.  
  215. %%
  216.  
  217. /* This lexer is nice and simple, but it does have a few
  218. limitations:
  219. (a) You cannot use any keywords as labels, eg:
  220. EXIT EXIT
  221. would fail, though
  222. "EXIT" EXIT
  223. is valid
  224. (b) A command consisting only of digits will cause a
  225. syntax error. Again, quoting will help.
  226. The 1st rule that will be matched should be
  227. one which returns LABEL. This activates the
  228. state ACT.
  229. Next, we expect an "action": one or more keywords, or an
  230. arbitrary command string, or both.
  231. We scan for keywords, but if we find something else,
  232. we assume it is a command, and go into the CMD state.
  233. Since CMD is exclusive, we build up the rest of the
  234. string, one char at a time.
  235.  
  236. The lexer is designed to be fairly flexible, and will
  237. treat, say:
  238. My" "Menu
  239. as a valid, single label (without processing the quotes).
  240.  
  241. One of the more subtle points of the lex-specification is the
  242. handling of INT. It relies on Lex's "first, best match" rule.
  243. It appears BEFORE the definitions of {ID}, so it RELIES on the
  244. fact that these definitions do NOT include spaces or "#",
  245. or anything that would make them read more than a single integer.
  246. (The same thing applies for the keywords).
  247. It is part of the reason for reading the rest of EXEC string using yymore()
  248. The other reason for using yymore() is that it lets us pass the
  249. command-string as a single token (EXEC), and so we don't have to
  250. reassemble the command-string within the parser from several smaller
  251. tokens.
  252. */
  253.  
  254. char *expand_env(char *src) {
  255. YY_BUFFER_STATE yybuff_env;
  256. YY_BUFFER_STATE yybuff_old;
  257. char *segment, *result, *bigger;
  258. int size, seglen, reslen;
  259. size = strlen(src) + 80;
  260. result=(char *)malloc(size);
  261. *result='';
  262. reslen = 0;
  263. BEGIN ENV;
  264. yybuff_old = YY_CURRENT_BUFFER;
  265. yybuff_env = yy_scan_string(src);
  266. yy_switch_to_buffer(yybuff_env);
  267. while(( segment = (char*)yylex() )) {
  268. seglen = strlen(segment);
  269. if( size <= reslen + seglen ) {
  270. size = reslen + seglen + 1;
  271. bigger = (char *)malloc(size);
  272. strcpy(bigger,result);
  273. free(result);
  274. result=bigger;
  275. }
  276. strcpy(result+reslen,segment);
  277. reslen += seglen;
  278. }
  279. yy_switch_to_buffer(yybuff_old);
  280. yy_delete_buffer(yybuff_env);
  281. BEGIN 0;
  282. return(result);
  283. }
Add Comment
Please, Sign In to add comment