xref: /minix3/external/bsd/flex/dist/yylex.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: yylex.c,v 1.4 2014/10/30 18:44:05 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.4 2014/10/30 18:44:05 christos Exp $");
37 
38 #include <ctype.h>
39 #include "parse.h"
40 
41 
42 /* yylex - scan for a regular expression token */
43 
44 extern char *yytext;
yylex()45 int     yylex ()
46 {
47 	int     toktype;
48 	static int beglin = false;
49 
50 	if (eofseen)
51 		toktype = EOF;
52 	else
53 		toktype = flexscan ();
54 
55 	if (toktype == EOF || toktype == 0) {
56 		eofseen = 1;
57 
58 		if (sectnum == 1) {
59 			synerr (_("premature EOF"));
60 			sectnum = 2;
61 			toktype = SECTEND;
62 		}
63 
64 		else
65 			toktype = 0;
66 	}
67 
68 	if (trace) {
69 		if (beglin) {
70 			fprintf (stderr, "%d\t", num_rules + 1);
71 			beglin = 0;
72 		}
73 
74 		switch (toktype) {
75 		case '<':
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 			(void) putc (toktype, stderr);
96 			break;
97 
98 		case '\n':
99 			(void) putc ('\n', stderr);
100 
101 			if (sectnum == 2)
102 				beglin = 1;
103 
104 			break;
105 
106 		case SCDECL:
107 			fputs ("%s", stderr);
108 			break;
109 
110 		case XSCDECL:
111 			fputs ("%x", stderr);
112 			break;
113 
114 		case SECTEND:
115 			fputs ("%%\n", stderr);
116 
117 			/* We set beglin to be true so we'll start
118 			 * writing out numbers as we echo rules.
119 			 * flexscan() has already assigned sectnum.
120 			 */
121 			if (sectnum == 2)
122 				beglin = 1;
123 
124 			break;
125 
126 		case NAME:
127 			fprintf (stderr, "'%s'", nmstr);
128 			break;
129 
130 		case CHAR:
131 			switch (yylval) {
132 			case '<':
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 				fprintf (stderr, "\\%c", yylval);
153 				break;
154 
155 			default:
156 				if (!isascii (yylval) || !isprint (yylval))
157 					fprintf (stderr,
158 						 "\\%.3o",
159 						 (unsigned int) yylval);
160 				else
161 					(void) putc (yylval, stderr);
162 				break;
163 			}
164 
165 			break;
166 
167 		case NUMBER:
168 			fprintf (stderr, "%d", yylval);
169 			break;
170 
171 		case PREVCCL:
172 			fprintf (stderr, "[%d]", yylval);
173 			break;
174 
175 		case EOF_OP:
176 			fprintf (stderr, "<<EOF>>");
177 			break;
178 
179 		case OPTION_OP:
180 			fprintf (stderr, "%s ", yytext);
181 			break;
182 
183 		case OPT_OUTFILE:
184 		case OPT_PREFIX:
185 		case CCE_ALNUM:
186 		case CCE_ALPHA:
187 		case CCE_BLANK:
188 		case CCE_CNTRL:
189 		case CCE_DIGIT:
190 		case CCE_GRAPH:
191 		case CCE_LOWER:
192 		case CCE_PRINT:
193 		case CCE_PUNCT:
194 		case CCE_SPACE:
195 		case CCE_UPPER:
196 		case CCE_XDIGIT:
197 			fprintf (stderr, "%s", yytext);
198 			break;
199 
200 		case 0:
201 			fprintf (stderr, _("End Marker\n"));
202 			break;
203 
204 		default:
205 			fprintf (stderr,
206 				 _
207 				 ("*Something Weird* - tok: %d val: %d\n"),
208 				 toktype, yylval);
209 			break;
210 		}
211 	}
212 
213 	return toktype;
214 }
215