[BACK]Return to scan.l CVS log [TXT][DIR] Up to [local] / src / usr.bin / bc

Annotation of src/usr.bin/bc/scan.l, Revision 1.4

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