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