scan.l 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* scan.l - scanner for flex input */
  2. %{
  3. /*-
  4. * Copyright (c) 1990 The Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Vern Paxson.
  9. *
  10. * The United States Government has rights in this work pursuant
  11. * to contract no. DE-AC03-76SF00098 between the United States
  12. * Department of Energy and the University of California.
  13. *
  14. * Redistribution and use in source and binary forms are permitted provided
  15. * that: (1) source distributions retain this entire copyright notice and
  16. * comment, and (2) distributions including binaries display the following
  17. * acknowledgement: ``This product includes software developed by the
  18. * University of California, Berkeley and its contributors'' in the
  19. * documentation or other materials provided with the distribution and in
  20. * all advertising materials mentioning features or use of this software.
  21. * Neither the name of the University nor the names of its contributors may
  22. * be used to endorse or promote products derived from this software without
  23. * specific prior written permission.
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27. */
  28. #ifndef lint
  29. static char rcsid[] =
  30. "@(#) $Id$ (LBL)";
  31. #endif
  32. #undef yywrap
  33. #include "flexdef.h"
  34. #include "parse.h"
  35. #define ACTION_ECHO fprintf( temp_action_file, "%s", yytext )
  36. #define MARK_END_OF_PROLOG fprintf( temp_action_file, "%%%% end of prolog\n" );
  37. #undef YY_DECL
  38. #define YY_DECL \
  39. int flexscan()
  40. #define RETURNCHAR \
  41. yylval = yytext[0]; \
  42. return ( CHAR );
  43. #define RETURNNAME \
  44. (void) strcpy( nmstr, (char *) yytext ); \
  45. return ( NAME );
  46. #define PUT_BACK_STRING(str, start) \
  47. for ( i = strlen( (char *) (str) ) - 1; i >= start; --i ) \
  48. unput((str)[i])
  49. #define CHECK_REJECT(str) \
  50. if ( all_upper( str ) ) \
  51. reject = true;
  52. #define CHECK_YYMORE(str) \
  53. if ( all_lower( str ) ) \
  54. yymore_used = true;
  55. %}
  56. %x SECT2 SECT2PROLOG SECT3 CODEBLOCK PICKUPDEF SC CARETISBOL NUM QUOTE
  57. %x FIRSTCCL CCL ACTION RECOVER BRACEERROR C_COMMENT ACTION_COMMENT
  58. %x ACTION_STRING PERCENT_BRACE_ACTION USED_LIST CODEBLOCK_2 XLATION
  59. WS [ \t\f]+
  60. OPTWS [ \t\f]*
  61. NOT_WS [^ \t\f\n]
  62. NAME [a-z_][a-z_0-9-]*
  63. NOT_NAME [^a-z_\n]+
  64. SCNAME {NAME}
  65. ESCSEQ \\([^\n]|[0-9]{1,3}|x[0-9a-f]{1,2})
  66. %%
  67. static int bracelevel, didadef;
  68. int i, indented_code, checking_used, new_xlation;
  69. int doing_codeblock = false;
  70. Char nmdef[MAXLINE], myesc();
  71. ^{WS} indented_code = true; BEGIN(CODEBLOCK);
  72. ^#.*\n ++linenum; /* treat as a comment */
  73. ^"/*" ECHO; BEGIN(C_COMMENT);
  74. ^"%s"{NAME}? return ( SCDECL );
  75. ^"%x"{NAME}? return ( XSCDECL );
  76. ^"%{".*\n {
  77. ++linenum;
  78. line_directive_out( stdout );
  79. indented_code = false;
  80. BEGIN(CODEBLOCK);
  81. }
  82. {WS} return ( WHITESPACE );
  83. ^"%%".* {
  84. sectnum = 2;
  85. line_directive_out( stdout );
  86. BEGIN(SECT2PROLOG);
  87. return ( SECTEND );
  88. }
  89. ^"%used" {
  90. pinpoint_message( "warning - %%used/%%unused have been deprecated" );
  91. checking_used = REALLY_USED; BEGIN(USED_LIST);
  92. }
  93. ^"%unused" {
  94. checking_used = REALLY_NOT_USED; BEGIN(USED_LIST);
  95. pinpoint_message( "warning - %%used/%%unused have been deprecated" );
  96. checking_used = REALLY_NOT_USED; BEGIN(USED_LIST);
  97. }
  98. ^"%"[aeknopt]" ".*\n {
  99. #ifdef NOTDEF
  100. fprintf( stderr,
  101. "old-style lex command at line %d ignored:\n\t%s",
  102. linenum, yytext );
  103. #endif
  104. ++linenum;
  105. }
  106. ^"%"[cr]{OPTWS} /* ignore old lex directive */
  107. %t{OPTWS}\n {
  108. ++linenum;
  109. xlation =
  110. (int *) malloc( sizeof( int ) * (unsigned) csize );
  111. if ( ! xlation )
  112. flexfatal(
  113. "dynamic memory failure building %t table" );
  114. for ( i = 0; i < csize; ++i )
  115. xlation[i] = 0;
  116. num_xlations = 0;
  117. BEGIN(XLATION);
  118. }
  119. ^"%"[^sxanpekotcru{}]{OPTWS} synerr( "unrecognized '%' directive" );
  120. ^{NAME} {
  121. (void) strcpy( nmstr, (char *) yytext );
  122. didadef = false;
  123. BEGIN(PICKUPDEF);
  124. }
  125. {SCNAME} RETURNNAME;
  126. ^{OPTWS}\n ++linenum; /* allows blank lines in section 1 */
  127. {OPTWS}\n ++linenum; return ( '\n' );
  128. . synerr( "illegal character" ); BEGIN(RECOVER);
  129. <C_COMMENT>"*/" ECHO; BEGIN(INITIAL);
  130. <C_COMMENT>"*/".*\n ++linenum; ECHO; BEGIN(INITIAL);
  131. <C_COMMENT>[^*\n]+ ECHO;
  132. <C_COMMENT>"*" ECHO;
  133. <C_COMMENT>\n ++linenum; ECHO;
  134. <CODEBLOCK>^"%}".*\n ++linenum; BEGIN(INITIAL);
  135. <CODEBLOCK>"reject" ECHO; CHECK_REJECT(yytext);
  136. <CODEBLOCK>"yymore" ECHO; CHECK_YYMORE(yytext);
  137. <CODEBLOCK>{NAME}|{NOT_NAME}|. ECHO;
  138. <CODEBLOCK>\n {
  139. ++linenum;
  140. ECHO;
  141. if ( indented_code )
  142. BEGIN(INITIAL);
  143. }
  144. <PICKUPDEF>{WS} /* separates name and definition */
  145. <PICKUPDEF>{NOT_WS}.* {
  146. (void) strcpy( (char *) nmdef, (char *) yytext );
  147. for ( i = strlen( (char *) nmdef ) - 1;
  148. i >= 0 &&
  149. nmdef[i] == ' ' || nmdef[i] == '\t';
  150. --i )
  151. ;
  152. nmdef[i + 1] = '\0';
  153. ndinstal( nmstr, nmdef );
  154. didadef = true;
  155. }
  156. <PICKUPDEF>\n {
  157. if ( ! didadef )
  158. synerr( "incomplete name definition" );
  159. BEGIN(INITIAL);
  160. ++linenum;
  161. }
  162. <RECOVER>.*\n ++linenum; BEGIN(INITIAL); RETURNNAME;
  163. <USED_LIST>\n ++linenum; BEGIN(INITIAL);
  164. <USED_LIST>{WS}
  165. <USED_LIST>"reject" {
  166. if ( all_upper( yytext ) )
  167. reject_really_used = checking_used;
  168. else
  169. synerr( "unrecognized %used/%unused construct" );
  170. }
  171. <USED_LIST>"yymore" {
  172. if ( all_lower( yytext ) )
  173. yymore_really_used = checking_used;
  174. else
  175. synerr( "unrecognized %used/%unused construct" );
  176. }
  177. <USED_LIST>{NOT_WS}+ synerr( "unrecognized %used/%unused construct" );
  178. <XLATION>"%t"{OPTWS}\n ++linenum; BEGIN(INITIAL);
  179. <XLATION>^{OPTWS}[0-9]+ ++num_xlations; new_xlation = true;
  180. <XLATION>^. synerr( "bad row in translation table" );
  181. <XLATION>{WS} /* ignore whitespace */
  182. <XLATION>{ESCSEQ} {
  183. xlation[myesc( yytext )] =
  184. (new_xlation ? num_xlations : -num_xlations);
  185. new_xlation = false;
  186. }
  187. <XLATION>. {
  188. xlation[yytext[0]] =
  189. (new_xlation ? num_xlations : -num_xlations);
  190. new_xlation = false;
  191. }
  192. <XLATION>\n ++linenum;
  193. <SECT2PROLOG>.*\n/{NOT_WS} {
  194. ++linenum;
  195. ACTION_ECHO;
  196. MARK_END_OF_PROLOG;
  197. BEGIN(SECT2);
  198. }
  199. <SECT2PROLOG>.*\n ++linenum; ACTION_ECHO;
  200. <SECT2PROLOG><<EOF>> MARK_END_OF_PROLOG; yyterminate();
  201. <SECT2>^{OPTWS}\n ++linenum; /* allow blank lines in section 2 */
  202. <SECT2>^({WS}|"%{") {
  203. indented_code = (yytext[0] != '%');
  204. doing_codeblock = true;
  205. bracelevel = 1;
  206. if ( indented_code )
  207. ACTION_ECHO;
  208. BEGIN(CODEBLOCK_2);
  209. }
  210. <SECT2>"<" BEGIN(SC); return ( '<' );
  211. <SECT2>^"^" return ( '^' );
  212. <SECT2>\" BEGIN(QUOTE); return ( '"' );
  213. <SECT2>"{"/[0-9] BEGIN(NUM); return ( '{' );
  214. <SECT2>"{"[^0-9\n][^}\n]* BEGIN(BRACEERROR);
  215. <SECT2>"$"/[ \t\n] return ( '$' );
  216. <SECT2>{WS}"%{" {
  217. bracelevel = 1;
  218. BEGIN(PERCENT_BRACE_ACTION);
  219. return ( '\n' );
  220. }
  221. <SECT2>{WS}"|".*\n continued_action = true; ++linenum; return ( '\n' );
  222. <SECT2>{WS} {
  223. /* this rule is separate from the one below because
  224. * otherwise we get variable trailing context, so
  225. * we can't build the scanner using -{f,F}
  226. */
  227. bracelevel = 0;
  228. continued_action = false;
  229. BEGIN(ACTION);
  230. return ( '\n' );
  231. }
  232. <SECT2>{OPTWS}/\n {
  233. bracelevel = 0;
  234. continued_action = false;
  235. BEGIN(ACTION);
  236. return ( '\n' );
  237. }
  238. <SECT2>^{OPTWS}\n ++linenum; return ( '\n' );
  239. <SECT2>"<<EOF>>" return ( EOF_OP );
  240. <SECT2>^"%%".* {
  241. sectnum = 3;
  242. BEGIN(SECT3);
  243. return ( EOF ); /* to stop the parser */
  244. }
  245. <SECT2>"["([^\\\]\n]|{ESCSEQ})+"]" {
  246. int cclval;
  247. (void) strcpy( nmstr, (char *) yytext );
  248. /* check to see if we've already encountered this ccl */
  249. if ( (cclval = ccllookup( (Char *) nmstr )) )
  250. {
  251. yylval = cclval;
  252. ++cclreuse;
  253. return ( PREVCCL );
  254. }
  255. else
  256. {
  257. /* we fudge a bit. We know that this ccl will
  258. * soon be numbered as lastccl + 1 by cclinit
  259. */
  260. cclinstal( (Char *) nmstr, lastccl + 1 );
  261. /* push back everything but the leading bracket
  262. * so the ccl can be rescanned
  263. */
  264. PUT_BACK_STRING((Char *) nmstr, 1);
  265. BEGIN(FIRSTCCL);
  266. return ( '[' );
  267. }
  268. }
  269. <SECT2>"{"{NAME}"}" {
  270. register Char *nmdefptr;
  271. Char *ndlookup();
  272. (void) strcpy( nmstr, (char *) yytext );
  273. nmstr[yyleng - 1] = '\0'; /* chop trailing brace */
  274. /* lookup from "nmstr + 1" to chop leading brace */
  275. if ( ! (nmdefptr = ndlookup( nmstr + 1 )) )
  276. synerr( "undefined {name}" );
  277. else
  278. { /* push back name surrounded by ()'s */
  279. unput(')');
  280. PUT_BACK_STRING(nmdefptr, 0);
  281. unput('(');
  282. }
  283. }
  284. <SECT2>[/|*+?.()] return ( yytext[0] );
  285. <SECT2>. RETURNCHAR;
  286. <SECT2>\n ++linenum; return ( '\n' );
  287. <SC>"," return ( ',' );
  288. <SC>">" BEGIN(SECT2); return ( '>' );
  289. <SC>">"/"^" BEGIN(CARETISBOL); return ( '>' );
  290. <SC>{SCNAME} RETURNNAME;
  291. <SC>. synerr( "bad start condition name" );
  292. <CARETISBOL>"^" BEGIN(SECT2); return ( '^' );
  293. <QUOTE>[^"\n] RETURNCHAR;
  294. <QUOTE>\" BEGIN(SECT2); return ( '"' );
  295. <QUOTE>\n {
  296. synerr( "missing quote" );
  297. BEGIN(SECT2);
  298. ++linenum;
  299. return ( '"' );
  300. }
  301. <FIRSTCCL>"^"/[^-\n] BEGIN(CCL); return ( '^' );
  302. <FIRSTCCL>"^"/- return ( '^' );
  303. <FIRSTCCL>- BEGIN(CCL); yylval = '-'; return ( CHAR );
  304. <FIRSTCCL>. BEGIN(CCL); RETURNCHAR;
  305. <CCL>-/[^\]\n] return ( '-' );
  306. <CCL>[^\]\n] RETURNCHAR;
  307. <CCL>"]" BEGIN(SECT2); return ( ']' );
  308. <NUM>[0-9]+ {
  309. yylval = myctoi( yytext );
  310. return ( NUMBER );
  311. }
  312. <NUM>"," return ( ',' );
  313. <NUM>"}" BEGIN(SECT2); return ( '}' );
  314. <NUM>. {
  315. synerr( "bad character inside {}'s" );
  316. BEGIN(SECT2);
  317. return ( '}' );
  318. }
  319. <NUM>\n {
  320. synerr( "missing }" );
  321. BEGIN(SECT2);
  322. ++linenum;
  323. return ( '}' );
  324. }
  325. <BRACEERROR>"}" synerr( "bad name in {}'s" ); BEGIN(SECT2);
  326. <BRACEERROR>\n synerr( "missing }" ); ++linenum; BEGIN(SECT2);
  327. <PERCENT_BRACE_ACTION,CODEBLOCK_2>{OPTWS}"%}".* bracelevel = 0;
  328. <PERCENT_BRACE_ACTION,CODEBLOCK_2,ACTION>"reject" {
  329. ACTION_ECHO;
  330. CHECK_REJECT(yytext);
  331. }
  332. <PERCENT_BRACE_ACTION,CODEBLOCK_2,ACTION>"yymore" {
  333. ACTION_ECHO;
  334. CHECK_YYMORE(yytext);
  335. }
  336. <PERCENT_BRACE_ACTION,CODEBLOCK_2>{NAME}|{NOT_NAME}|. ACTION_ECHO;
  337. <PERCENT_BRACE_ACTION,CODEBLOCK_2>\n {
  338. ++linenum;
  339. ACTION_ECHO;
  340. if ( bracelevel == 0 ||
  341. (doing_codeblock && indented_code) )
  342. {
  343. if ( ! doing_codeblock )
  344. fputs( "\tYY_BREAK\n", temp_action_file );
  345. doing_codeblock = false;
  346. BEGIN(SECT2);
  347. }
  348. }
  349. /* Reject and YYmore() are checked for above, in PERCENT_BRACE_ACTION */
  350. <ACTION>"{" ACTION_ECHO; ++bracelevel;
  351. <ACTION>"}" ACTION_ECHO; --bracelevel;
  352. <ACTION>[^a-z_{}"'/\n]+ ACTION_ECHO;
  353. <ACTION>{NAME} ACTION_ECHO;
  354. <ACTION>"/*" ACTION_ECHO; BEGIN(ACTION_COMMENT);
  355. <ACTION>"'"([^'\\\n]|\\.)*"'" ACTION_ECHO; /* character constant */
  356. <ACTION>\" ACTION_ECHO; BEGIN(ACTION_STRING);
  357. <ACTION>\n {
  358. ++linenum;
  359. ACTION_ECHO;
  360. if ( bracelevel == 0 )
  361. {
  362. fputs( "\tYY_BREAK\n", temp_action_file );
  363. BEGIN(SECT2);
  364. }
  365. }
  366. <ACTION>. ACTION_ECHO;
  367. <ACTION_COMMENT>"*/" ACTION_ECHO; BEGIN(ACTION);
  368. <ACTION_COMMENT>[^*\n]+ ACTION_ECHO;
  369. <ACTION_COMMENT>"*" ACTION_ECHO;
  370. <ACTION_COMMENT>\n ++linenum; ACTION_ECHO;
  371. <ACTION_COMMENT>. ACTION_ECHO;
  372. <ACTION_STRING>[^"\\\n]+ ACTION_ECHO;
  373. <ACTION_STRING>\\. ACTION_ECHO;
  374. <ACTION_STRING>\n ++linenum; ACTION_ECHO;
  375. <ACTION_STRING>\" ACTION_ECHO; BEGIN(ACTION);
  376. <ACTION_STRING>. ACTION_ECHO;
  377. <ACTION,ACTION_COMMENT,ACTION_STRING><<EOF>> {
  378. synerr( "EOF encountered inside an action" );
  379. yyterminate();
  380. }
  381. <SECT2,QUOTE,CCL>{ESCSEQ} {
  382. yylval = myesc( yytext );
  383. return ( CHAR );
  384. }
  385. <FIRSTCCL>{ESCSEQ} {
  386. yylval = myesc( yytext );
  387. BEGIN(CCL);
  388. return ( CHAR );
  389. }
  390. <SECT3>.*(\n?) ECHO;
  391. %%
  392. int yywrap()
  393. {
  394. if ( --num_input_files > 0 )
  395. {
  396. set_input_file( *++input_files );
  397. return ( 0 );
  398. }
  399. else
  400. return ( 1 );
  401. }
  402. /* set_input_file - open the given file (if NULL, stdin) for scanning */
  403. void set_input_file( file )
  404. char *file;
  405. {
  406. if ( file )
  407. {
  408. infilename = file;
  409. yyin = fopen( infilename, "r" );
  410. if ( yyin == NULL )
  411. lerrsf( "can't open %s", file );
  412. }
  413. else
  414. {
  415. yyin = stdin;
  416. infilename = "<stdin>";
  417. }
  418. }