Annotation of src/usr.bin/bc/scan.l, Revision 1.5
1.1 otto 1: %{
1.5 ! otto 2: /* $OpenBSD: scan.l,v 1.4 2003/09/26 19:00:38 deraadt 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.5 ! otto 21: static const char rcsid[] = "$OpenBSD: scan.l,v 1.4 2003/09/26 19:00:38 deraadt 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>{
58: [^"\n]+ add_str(yytext);
59: \n add_str("\n"); lineno++;
60: \" BEGIN(INITIAL); yylval.str = strbuf; return STRING;
1.5 ! otto 61: <<EOF>> fatal("end of file in string");
1.1 otto 62: }
63:
64: {DIGIT}+ {
65: BEGIN(number);
66: dot_seen = false;
67: init_strbuf();
68: add_str(yytext);
69: }
70: \. {
71: BEGIN(number);
72: dot_seen = true;
73: init_strbuf();
74: add_str(".");
75: }
76: <number>{
77: {DIGIT}+ add_str(yytext);
78: \. {
79: if (dot_seen) {
80: BEGIN(INITIAL);
81: yylval.str = strbuf;
82: unput('.');
83: return NUMBER;
84: } else {
85: dot_seen = true;
86: add_str(".");
87: }
88: }
89: \\\n[ \t]* lineno++;
90: [^0-9A-F\.] {
91: if (strcmp(strbuf, ".") == 0) {
1.5 ! otto 92: yyerror("syntax error");
1.1 otto 93: BEGIN(INITIAL);
94: REJECT;
95: }
96: else {
97: BEGIN(INITIAL);
98: yylval.str = strbuf;
99: unput(yytext[0]);
100: return NUMBER;
101: }
102: }
103: }
104:
105: "auto" return AUTO;
1.2 deraadt 106: "break" return BREAK;
1.1 otto 107: "define" return DEFINE;
108: "ibase" return IBASE;
109: "if" return IF;
110: "for" return FOR;
111: "length" return LENGTH;
112: "obase" return OBASE;
113: "quit" return QUIT;
114: "return" return RETURN;
115: "scale" return SCALE;
116: "sqrt" return SQRT;
117: "while" return WHILE;
118:
119: "^" return EXPONENT;
120: "*" return MULTIPLY;
121: "/" return DIVIDE;
122: "%" return REMAINDER;
123:
124: "+" return PLUS;
125: "-" return MINUS;
126:
127: "++" return INCR;
128: "--" return DECR;
129:
1.4 deraadt 130: "=" yylval.str = ""; return ASSIGN_OP;
1.1 otto 131: "+=" yylval.str = "+"; return ASSIGN_OP;
132: "-=" yylval.str = "-"; return ASSIGN_OP;
133: "*=" yylval.str = "*"; return ASSIGN_OP;
134: "/=" yylval.str = "/"; return ASSIGN_OP;
135: "%=" yylval.str = "%"; return ASSIGN_OP;
136: "^=" yylval.str = "^"; return ASSIGN_OP;
137:
138: "==" return EQUALS;
139: "<=" return LESS_EQ;
140: ">=" return GREATER_EQ;
141: "!=" return UNEQUALS;
142: "<" return LESS;
143: ">" return GREATER;
144:
145: "," return COMMA;
146: ";" return SEMICOLON;
147:
148: "(" return LPAR;
149: ")" return RPAR;
150:
151: "[" return LBRACKET;
152: "]" return RBRACKET;
153:
154: "{" return LBRACE;
155: "}" return RBRACE;
156:
157: [a-z] yylval.str = yytext; return LETTER;
158:
159: \\\n lineno++;
160: \n lineno++; return NEWLINE;
161:
162: [ \t] ;
1.5 ! otto 163: . yyerror("illegal character");
1.1 otto 164:
165: %%
166:
167: static void
168: init_strbuf(void)
169: {
170: if (strbuf == NULL) {
171: strbuf = malloc(strbuf_sz);
172: if (strbuf == NULL)
173: err(1, "cannot allocate string buffer");
174: }
175: strbuf[0] = '\0';
176: }
177:
178: static void
179: add_str(const char *str)
180: {
181: size_t arglen;
182:
183: arglen = strlen(str);
184:
185: if (strlen(strbuf) + arglen + 1> strbuf_sz) {
186: size_t newsize;
1.3 deraadt 187: char *p;
1.1 otto 188:
189: newsize = strbuf_sz + arglen + 1;
190: p = realloc(strbuf, newsize);
191: if (p == NULL) {
192: free(strbuf);
193: err(1, "cannot realloc string buffer");
194: }
195: strbuf_sz = newsize;
196: strbuf = p;
197: }
198: strlcat(strbuf, str, strbuf_sz);
199: }
200:
201: void
202: abort_line(int sig)
203: {
204: if (isatty(fileno(yyin))) {
205: YY_FLUSH_BUFFER;
206: printf("[\n]P\n");
207: }
208: }