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