scan.l revision 1.143 1 1.1 cgd %{
2 1.143 rillig /* $NetBSD: scan.l,v 1.143 2024/12/08 17:12:01 rillig Exp $ */
3 1.2 cgd
4 1.1 cgd /*
5 1.9 cgd * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
7 1.1 cgd * All Rights Reserved.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.1 cgd * 3. All advertising materials mentioning features or use of this software
18 1.1 cgd * must display the following acknowledgement:
19 1.139 rillig * This product includes software developed by Jochen Pohl for
20 1.139 rillig * The NetBSD Project.
21 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
22 1.1 cgd * derived from this software without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.11 christos #include <sys/cdefs.h>
37 1.136 rillig #if defined(__RCSID)
38 1.143 rillig __RCSID("$NetBSD: scan.l,v 1.143 2024/12/08 17:12:01 rillig Exp $");
39 1.1 cgd #endif
40 1.1 cgd
41 1.1 cgd #include "lint1.h"
42 1.12 tv #include "cgram.h"
43 1.1 cgd
44 1.1 cgd %}
45 1.1 cgd
46 1.62 christos
47 1.138 rillig HEX [0-9A-Fa-f]
48 1.138 rillig EXP ([eE][+-]?[0-9]+)
49 1.138 rillig PEXP (p[+-]?[0-9A-Fa-f]+)
50 1.138 rillig FSUF ([fFlL]?[i]?)
51 1.1 cgd
52 1.143 rillig punctuator_1 [\[\](){}.]|->
53 1.143 rillig punctuator_2 {punctuator_1}|\+\+|--|[&*+\-~!]
54 1.143 rillig punctuator_3 {punctuator_2}|\/|%|<<|>>|<|>|<=|>=|==|!=|\^|\||&&|\|\|
55 1.143 rillig punctuator_4 {punctuator_3}|\?|:|::|;|\.\.\.
56 1.143 rillig punctuator_5 {punctuator_4}|=|\*=|\/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=
57 1.143 rillig punctuator_6 {punctuator_5}|,|#|##
58 1.143 rillig punctuator_7 {punctuator_6}|<:|:>|<%|%>|%:|%:%:
59 1.143 rillig punctuator {punctuator_7}|@
60 1.143 rillig
61 1.142 rillig %pointer
62 1.46 christos %option nounput
63 1.46 christos
64 1.143 rillig %x preprocessing
65 1.143 rillig
66 1.1 cgd %%
67 1.1 cgd
68 1.138 rillig [_A-Za-z][_A-Za-z0-9]* return lex_name(yytext, yyleng);
69 1.138 rillig 0[bB][01]+[lLuU]* return lex_integer_constant(yytext, yyleng, 2);
70 1.138 rillig 0[0-7]*[lLuU]* return lex_integer_constant(yytext, yyleng, 8);
71 1.138 rillig [1-9][0-9]*[lLuU]* return lex_integer_constant(yytext, yyleng, 10);
72 1.138 rillig 0[xX]{HEX}+[lLuU]* return lex_integer_constant(yytext, yyleng, 16);
73 1.138 rillig [0-9]+\.[0-9]*{EXP}?{FSUF} |
74 1.138 rillig [0-9]+{EXP}{FSUF} |
75 1.138 rillig 0[xX]{HEX}+\.{HEX}*{PEXP}{FSUF} |
76 1.138 rillig 0[xX]{HEX}+{PEXP}{FSUF} |
77 1.138 rillig \.[0-9]+{EXP}?{FSUF} return lex_floating_constant(yytext, yyleng);
78 1.132 rillig "=" return T_ASSIGN;
79 1.130 rillig "*=" return lex_operator(T_OPASSIGN, MULASS);
80 1.130 rillig "/=" return lex_operator(T_OPASSIGN, DIVASS);
81 1.130 rillig "%=" return lex_operator(T_OPASSIGN, MODASS);
82 1.130 rillig "+=" return lex_operator(T_OPASSIGN, ADDASS);
83 1.130 rillig "-=" return lex_operator(T_OPASSIGN, SUBASS);
84 1.130 rillig "<<=" return lex_operator(T_OPASSIGN, SHLASS);
85 1.130 rillig ">>=" return lex_operator(T_OPASSIGN, SHRASS);
86 1.130 rillig "&=" return lex_operator(T_OPASSIGN, ANDASS);
87 1.130 rillig "^=" return lex_operator(T_OPASSIGN, XORASS);
88 1.130 rillig "|=" return lex_operator(T_OPASSIGN, ORASS);
89 1.132 rillig "||" return T_LOGOR;
90 1.132 rillig "&&" return T_LOGAND;
91 1.132 rillig "|" return T_BITOR;
92 1.132 rillig "&" return T_AMPER;
93 1.133 rillig "^" return T_BITXOR;
94 1.130 rillig "==" return lex_operator(T_EQUALITY, EQ);
95 1.130 rillig "!=" return lex_operator(T_EQUALITY, NE);
96 1.130 rillig "<" return lex_operator(T_RELATIONAL, LT);
97 1.130 rillig ">" return lex_operator(T_RELATIONAL, GT);
98 1.130 rillig "<=" return lex_operator(T_RELATIONAL, LE);
99 1.130 rillig ">=" return lex_operator(T_RELATIONAL, GE);
100 1.130 rillig "<<" return lex_operator(T_SHIFT, SHL);
101 1.130 rillig ">>" return lex_operator(T_SHIFT, SHR);
102 1.140 rillig "++" return yylval.y_inc = true, T_INCDEC;
103 1.140 rillig "--" return yylval.y_inc = false, T_INCDEC;
104 1.134 rillig "->" return T_ARROW;
105 1.134 rillig "." return T_POINT;
106 1.130 rillig "+" return lex_operator(T_ADDITIVE, PLUS);
107 1.130 rillig "-" return lex_operator(T_ADDITIVE, MINUS);
108 1.132 rillig "*" return T_ASTERISK;
109 1.130 rillig "/" return lex_operator(T_MULTIPLICATIVE, DIV);
110 1.130 rillig "%" return lex_operator(T_MULTIPLICATIVE, MOD);
111 1.135 rillig "!" return T_LOGNOT;
112 1.135 rillig "~" return T_COMPLEMENT;
113 1.130 rillig "\"" return lex_string();
114 1.131 rillig "L\"" return lex_wide_string();
115 1.97 rillig ";" return T_SEMI;
116 1.97 rillig "{" return T_LBRACE;
117 1.97 rillig "}" return T_RBRACE;
118 1.97 rillig "," return T_COMMA;
119 1.97 rillig ":" return T_COLON;
120 1.97 rillig "?" return T_QUEST;
121 1.97 rillig "[" return T_LBRACK;
122 1.97 rillig "]" return T_RBRACK;
123 1.108 rillig "(" return T_LPAREN;
124 1.108 rillig ")" return T_RPAREN;
125 1.123 rillig "..." return T_ELLIPSIS;
126 1.141 rillig "::" return T_DCOLON;
127 1.131 rillig "'" return lex_character_constant();
128 1.131 rillig "L'" return lex_wide_character_constant();
129 1.131 rillig \n lex_next_line();
130 1.1 cgd \t|" "|\f|\v ;
131 1.130 rillig "/*" lex_comment();
132 1.131 rillig "//" lex_slash_slash_comment();
133 1.143 rillig
134 1.143 rillig ^# {
135 1.143 rillig BEGIN preprocessing;
136 1.143 rillig lex_pp_begin();
137 1.143 rillig }
138 1.143 rillig <preprocessing>[_A-Za-z][_A-Za-z0-9]* lex_pp_identifier(yytext);
139 1.143 rillig <preprocessing>\.?[0-9]('?[_A-Za-z0-9]|[EePp][-+][0-9]+|\.)* lex_pp_number(yytext);
140 1.143 rillig <preprocessing>\' lex_pp_character_constant();
141 1.143 rillig <preprocessing>\" lex_pp_string_literal();
142 1.143 rillig <preprocessing>{punctuator} lex_pp_punctuator(yytext);
143 1.143 rillig <preprocessing>\/\* lex_pp_comment();
144 1.143 rillig <preprocessing>[ \f\t\v]+ lex_pp_whitespace();
145 1.143 rillig <preprocessing>. lex_unknown_character(yytext[0]);
146 1.143 rillig <preprocessing>\n {
147 1.143 rillig lex_pp_end();
148 1.143 rillig lex_next_line();
149 1.143 rillig BEGIN INITIAL;
150 1.143 rillig }
151 1.143 rillig
152 1.131 rillig . lex_unknown_character(yytext[0]);
153 1.1 cgd
154 1.1 cgd %%
155 1.1 cgd
156 1.137 rillig /*
157 1.137 rillig * In the above list of regular expressions, the tokens for character
158 1.137 rillig * constants, string literals and comments are incomplete; they only match
159 1.137 rillig * a prefix. The remainder of these tokens is scanned by reading bytes
160 1.137 rillig * directly from the input stream.
161 1.137 rillig */
162 1.1 cgd int
163 1.130 rillig lex_input(void)
164 1.6 jpo {
165 1.130 rillig return input();
166 1.1 cgd }
167