Annotation of src/usr.bin/bc/scan.l, Revision 1.11
1.1 otto 1: %{
1.11 ! otto 2: /* $OpenBSD: scan.l,v 1.10 2003/11/09 19:27:27 otto Exp $ */
1.1 otto 3:
4: /*
5: * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
6: *
7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18: */
19:
20: #ifndef lint
1.11 ! otto 21: static const char rcsid[] = "$OpenBSD: scan.l,v 1.10 2003/11/09 19:27:27 otto Exp $";
1.1 otto 22: #endif /* not lint */
23:
24: #include <err.h>
25: #include <stdbool.h>
26: #include <string.h>
27:
28: #include "extern.h"
29: #include "y.tab.h"
30:
31: int lineno;
32:
1.2 deraadt 33: static char *strbuf = NULL;
1.1 otto 34: static size_t strbuf_sz = 1;
35: static bool dot_seen;
36:
37: static void init_strbuf(void);
38: static void add_str(const char *);
39:
40: %}
41:
42: DIGIT [0-9A-F]
43: %x comment string number
44:
45: %%
46:
47: "/*" BEGIN(comment);
48: <comment>{
49: "*/" BEGIN(INITIAL);
50: \n lineno++;
51: \* ;
52: [^*\n]+ ;
1.5 otto 53: <<EOF>> fatal("end of file in comment");
1.1 otto 54: }
55:
56: \" BEGIN(string); init_strbuf();
57: <string>{
1.7 otto 58: [^"\n\\\[\]]+ add_str(yytext);
59: \[ add_str("\\[");
60: \] add_str("\\]");
61: \\ add_str("\\\\");
1.1 otto 62: \n add_str("\n"); lineno++;
63: \" BEGIN(INITIAL); yylval.str = strbuf; return STRING;
1.5 otto 64: <<EOF>> fatal("end of file in string");
1.1 otto 65: }
66:
67: {DIGIT}+ {
68: BEGIN(number);
69: dot_seen = false;
70: init_strbuf();
71: add_str(yytext);
72: }
73: \. {
74: BEGIN(number);
75: dot_seen = true;
76: init_strbuf();
77: add_str(".");
78: }
79: <number>{
80: {DIGIT}+ add_str(yytext);
81: \. {
82: if (dot_seen) {
83: BEGIN(INITIAL);
84: yylval.str = strbuf;
85: unput('.');
86: return NUMBER;
87: } else {
88: dot_seen = true;
89: add_str(".");
90: }
91: }
92: \\\n[ \t]* lineno++;
93: [^0-9A-F\.] {
1.6 otto 94: BEGIN(INITIAL);
95: unput(yytext[0]);
96: if (strcmp(strbuf, ".") == 0)
97: return DOT;
1.1 otto 98: else {
99: yylval.str = strbuf;
100: return NUMBER;
101: }
102: }
103: }
104:
105: "auto" return AUTO;
1.2 deraadt 106: "break" return BREAK;
1.8 otto 107: "continue" return CONTINUE;
1.1 otto 108: "define" return DEFINE;
1.8 otto 109: "else" return ELSE;
1.1 otto 110: "ibase" return IBASE;
111: "if" return IF;
1.10 otto 112: "last" return DOT;
1.1 otto 113: "for" return FOR;
114: "length" return LENGTH;
115: "obase" return OBASE;
1.11 ! otto 116: "print" return PRINT;
1.1 otto 117: "quit" return QUIT;
118: "return" return RETURN;
119: "scale" return SCALE;
120: "sqrt" return SQRT;
121: "while" return WHILE;
122:
123: "^" return EXPONENT;
124: "*" return MULTIPLY;
125: "/" return DIVIDE;
126: "%" return REMAINDER;
127:
128: "+" return PLUS;
129: "-" return MINUS;
130:
131: "++" return INCR;
132: "--" return DECR;
133:
1.4 deraadt 134: "=" yylval.str = ""; return ASSIGN_OP;
1.1 otto 135: "+=" yylval.str = "+"; return ASSIGN_OP;
136: "-=" yylval.str = "-"; return ASSIGN_OP;
137: "*=" yylval.str = "*"; return ASSIGN_OP;
138: "/=" yylval.str = "/"; return ASSIGN_OP;
139: "%=" yylval.str = "%"; return ASSIGN_OP;
140: "^=" yylval.str = "^"; return ASSIGN_OP;
141:
142: "==" return EQUALS;
143: "<=" return LESS_EQ;
144: ">=" return GREATER_EQ;
145: "!=" return UNEQUALS;
146: "<" return LESS;
147: ">" return GREATER;
148:
149: "," return COMMA;
150: ";" return SEMICOLON;
151:
152: "(" return LPAR;
153: ")" return RPAR;
154:
155: "[" return LBRACKET;
156: "]" return RBRACKET;
157:
158: "{" return LBRACE;
159: "}" return RBRACE;
160:
161: [a-z] yylval.str = yytext; return LETTER;
162:
163: \\\n lineno++;
164: \n lineno++; return NEWLINE;
165:
166: [ \t] ;
1.5 otto 167: . yyerror("illegal character");
1.1 otto 168:
169: %%
170:
171: static void
172: init_strbuf(void)
173: {
174: if (strbuf == NULL) {
175: strbuf = malloc(strbuf_sz);
176: if (strbuf == NULL)
1.9 otto 177: err(1, NULL);
1.1 otto 178: }
179: strbuf[0] = '\0';
180: }
181:
182: static void
183: add_str(const char *str)
184: {
185: size_t arglen;
186:
187: arglen = strlen(str);
188:
189: if (strlen(strbuf) + arglen + 1> strbuf_sz) {
190: size_t newsize;
1.3 deraadt 191: char *p;
1.1 otto 192:
193: newsize = strbuf_sz + arglen + 1;
194: p = realloc(strbuf, newsize);
195: if (p == NULL) {
196: free(strbuf);
1.9 otto 197: err(1, NULL);
1.1 otto 198: }
199: strbuf_sz = newsize;
200: strbuf = p;
201: }
202: strlcat(strbuf, str, strbuf_sz);
203: }
204:
205: void
206: abort_line(int sig)
207: {
208: if (isatty(fileno(yyin))) {
209: YY_FLUSH_BUFFER;
210: printf("[\n]P\n");
211: }
212: }