1 /*	$NetBSD: yylex.c,v 1.2 2016/01/09 17:38:57 christos Exp $	*/
2 
3 /* yylex - scanner front-end for flex */
4 
5 /*  Copyright (c) 1990 The Regents of the University of California. */
6 /*  All rights reserved. */
7 
8 /*  This code is derived from software contributed to Berkeley by */
9 /*  Vern Paxson. */
10 
11 /*  The United States Government has rights in this work pursuant */
12 /*  to contract no. DE-AC03-76SF00098 between the United States */
13 /*  Department of Energy and the University of California. */
14 
15 /*  This file is part of flex. */
16 
17 /*  Redistribution and use in source and binary forms, with or without */
18 /*  modification, are permitted provided that the following conditions */
19 /*  are met: */
20 
21 /*  1. Redistributions of source code must retain the above copyright */
22 /*     notice, this list of conditions and the following disclaimer. */
23 /*  2. Redistributions in binary form must reproduce the above copyright */
24 /*     notice, this list of conditions and the following disclaimer in the */
25 /*     documentation and/or other materials provided with the distribution. */
26 
27 /*  Neither the name of the University nor the names of its contributors */
28 /*  may be used to endorse or promote products derived from this software */
29 /*  without specific prior written permission. */
30 
31 /*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34 /*  PURPOSE. */
35 #include "flexdef.h"
36 __RCSID("$NetBSD: yylex.c,v 1.2 2016/01/09 17:38:57 christos Exp $");
37 
38 #include <ctype.h>
39 #include "flexdef.h"
40 #include "parse.h"
41 
42 
43 /* yylex - scan for a regular expression token */
44 
45 extern char *yytext;
yylex(void)46 int     yylex (void)
47 {
48 	int     toktype;
49 	static int beglin = false;
50 
51 	if (eofseen)
52 		toktype = EOF;
53 	else
54 		toktype = flexscan ();
55 
56 	if (toktype == EOF || toktype == 0) {
57 		eofseen = 1;
58 
59 		if (sectnum == 1) {
60 			synerr (_("premature EOF"));
61 			sectnum = 2;
62 			toktype = SECTEND;
63 		}
64 
65 		else
66 			toktype = 0;
67 	}
68 
69 	if (trace) {
70 		if (beglin) {
71 			fprintf (stderr, "%d\t", num_rules + 1);
72 			beglin = 0;
73 		}
74 
75 		switch (toktype) {
76 		case '<':
77 		case '>':
78 		case '^':
79 		case '$':
80 		case '"':
81 		case '[':
82 		case ']':
83 		case '{':
84 		case '}':
85 		case '|':
86 		case '(':
87 		case ')':
88 		case '-':
89 		case '/':
90 		case '\\':
91 		case '?':
92 		case '.':
93 		case '*':
94 		case '+':
95 		case ',':
96 			(void) putc (toktype, stderr);
97 			break;
98 
99 		case '\n':
100 			(void) putc ('\n', stderr);
101 
102 			if (sectnum == 2)
103 				beglin = 1;
104 
105 			break;
106 
107 		case SCDECL:
108 			fputs ("%s", stderr);
109 			break;
110 
111 		case XSCDECL:
112 			fputs ("%x", stderr);
113 			break;
114 
115 		case SECTEND:
116 			fputs ("%%\n", stderr);
117 
118 			/* We set beglin to be true so we'll start
119 			 * writing out numbers as we echo rules.
120 			 * flexscan() has already assigned sectnum.
121 			 */
122 			if (sectnum == 2)
123 				beglin = 1;
124 
125 			break;
126 
127 		case NAME:
128 			fprintf (stderr, "'%s'", nmstr);
129 			break;
130 
131 		case CHAR:
132 			switch (yylval) {
133 			case '<':
134 			case '>':
135 			case '^':
136 			case '$':
137 			case '"':
138 			case '[':
139 			case ']':
140 			case '{':
141 			case '}':
142 			case '|':
143 			case '(':
144 			case ')':
145 			case '-':
146 			case '/':
147 			case '\\':
148 			case '?':
149 			case '.':
150 			case '*':
151 			case '+':
152 			case ',':
153 				fprintf (stderr, "\\%c", yylval);
154 				break;
155 
156 			default:
157 				if (!isascii (yylval) || !isprint (yylval)) {
158 					if(trace_hex)
159 						fprintf (stderr, "\\x%02x", (unsigned int) yylval);
160 					else
161 						fprintf (stderr, "\\%.3o", (unsigned int) yylval);
162 				} else
163 					(void) putc (yylval, stderr);
164 				break;
165 			}
166 
167 			break;
168 
169 		case NUMBER:
170 			fprintf (stderr, "%d", yylval);
171 			break;
172 
173 		case PREVCCL:
174 			fprintf (stderr, "[%d]", yylval);
175 			break;
176 
177 		case EOF_OP:
178 			fprintf (stderr, "<<EOF>>");
179 			break;
180 
181 		case OPTION_OP:
182 			fprintf (stderr, "%s ", yytext);
183 			break;
184 
185 		case OPT_OUTFILE:
186 		case OPT_PREFIX:
187 		case CCE_ALNUM:
188 		case CCE_ALPHA:
189 		case CCE_BLANK:
190 		case CCE_CNTRL:
191 		case CCE_DIGIT:
192 		case CCE_GRAPH:
193 		case CCE_LOWER:
194 		case CCE_PRINT:
195 		case CCE_PUNCT:
196 		case CCE_SPACE:
197 		case CCE_UPPER:
198 		case CCE_XDIGIT:
199 			fprintf (stderr, "%s", yytext);
200 			break;
201 
202 		case 0:
203 			fprintf (stderr, _("End Marker\n"));
204 			break;
205 
206 		default:
207 			fprintf (stderr,
208 				 _
209 				 ("*Something Weird* - tok: %d val: %d\n"),
210 				 toktype, yylval);
211 			break;
212 		}
213 	}
214 
215 	return toktype;
216 }
217