xref: /386bsd/usr/src/usr.bin/lex/yylex.c (revision a2142627)
1 /* yylex - scanner front-end for flex */
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 
29 /* $Header: /home/daffy/u0/vern/flex/RCS/yylex.c,v 2.10 93/09/16 20:31:48 vern Exp $ */
30 
31 #include <ctype.h>
32 #include "flexdef.h"
33 #include "parse.h"
34 
35 
36 /* yylex - scan for a regular expression token */
37 
yylex()38 int yylex()
39 	{
40 	int toktype;
41 	static int beglin = false;
42 
43 	if ( eofseen )
44 		toktype = EOF;
45 	else
46 		toktype = flexscan();
47 
48 	if ( toktype == EOF || toktype == 0 )
49 		{
50 		eofseen = 1;
51 
52 		if ( sectnum == 1 )
53 			{
54 			synerr( "premature EOF" );
55 			sectnum = 2;
56 			toktype = SECTEND;
57 			}
58 
59 		else
60 			toktype = 0;
61 		}
62 
63 	if ( trace )
64 		{
65 		if ( beglin )
66 			{
67 			fprintf( stderr, "%d\t", num_rules + 1 );
68 			beglin = 0;
69 			}
70 
71 		switch ( toktype )
72 			{
73 			case '<':
74 			case '>':
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 				(void) putc( toktype, stderr );
94 				break;
95 
96 			case '\n':
97 				(void) putc( '\n', stderr );
98 
99 				if ( sectnum == 2 )
100 				beglin = 1;
101 
102 				break;
103 
104 			case SCDECL:
105 				fputs( "%s", stderr );
106 				break;
107 
108 			case XSCDECL:
109 				fputs( "%x", stderr );
110 				break;
111 
112 			case WHITESPACE:
113 				(void) putc( ' ', stderr );
114 				break;
115 
116 			case SECTEND:
117 				fputs( "%%\n", stderr );
118 
119 				/* We set beglin to be true so we'll start
120 				 * writing out numbers as we echo rules.
121 				 * flexscan() has already assigned sectnum.
122 				 */
123 
124 				if ( sectnum == 2 )
125 				beglin = 1;
126 
127 				break;
128 
129 			case NAME:
130 				fprintf( stderr, "'%s'", nmstr );
131 				break;
132 
133 			case CHAR:
134 				switch ( yylval )
135 					{
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 					case '*':
154 					case '+':
155 					case ',':
156 						fprintf( stderr, "\\%c",
157 							yylval );
158 						break;
159 
160 					default:
161 						if ( ! isascii( yylval ) ||
162 						     ! isprint( yylval ) )
163 							fprintf( stderr,
164 								"\\%.3o",
165 							(unsigned int) yylval );
166 						else
167 							(void) putc( yylval,
168 								stderr );
169 					break;
170 					}
171 
172 				break;
173 
174 			case NUMBER:
175 				fprintf( stderr, "%d", yylval );
176 				break;
177 
178 			case PREVCCL:
179 				fprintf( stderr, "[%d]", yylval );
180 				break;
181 
182 			case EOF_OP:
183 				fprintf( stderr, "<<EOF>>" );
184 				break;
185 
186 			case 0:
187 				fprintf( stderr, "End Marker" );
188 				break;
189 
190 			default:
191 				fprintf( stderr,
192 					"*Something Weird* - tok: %d val: %d\n",
193 					toktype, yylval );
194 				break;
195 			}
196 		}
197 
198 	return toktype;
199 	}
200