⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 push.c

📁 a little DFA compiler.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Generated by re2c */#line 1 "push.re"/* *  A push-model scanner example for re2c -f *  Written Mon Apr 11 2005 by mgix@mgix.com *  This file is in the public domain. * */// ----------------------------------------------------------------------#include <fcntl.h>#include <stdio.h>#include <stddef.h>#include <stdlib.h>#include <string.h>#if defined(WIN32)    typedef signed char     int8_t;    typedef signed short    int16_t;    typedef signed int      int32_t;    typedef unsigned char   uint8_t;    typedef unsigned short  uint16_t;    typedef unsigned int    uint32_t;#else    #include <stdint.h>    #include <unistd.h>    #ifndef O_BINARY        #define O_BINARY 0    #endif#endif// ----------------------------------------------------------------------#define TOKENS              \                            \    TOK(kEOF)               \    TOK(kEOL)               \    TOK(kUnknown)           \    TOK(kIdentifier)        \    TOK(kDecimalConstant)   \                            \    TOK(kEqual)             \    TOK(kLeftParen)         \    TOK(kRightParen)        \    TOK(kMinus)             \    TOK(kPlus)              \    TOK(kStar)              \    TOK(kSlash)             \                            \    TOK(kIf)                \    TOK(kFor)               \    TOK(kElse)              \    TOK(kGoto)              \    TOK(kBreak)             \    TOK(kWhile)             \    TOK(kReturn)            \// ----------------------------------------------------------------------static const char *tokenNames[] ={    #define TOK(x) #x,        TOKENS    #undef TOK};// ----------------------------------------------------------------------class PushScanner{public:    enum Token    {        #define TOK(x) x,            TOKENS        #undef TOK    };private:    bool        eof;    int32_t     state;    uint8_t     *limit;    uint8_t     *start;    uint8_t     *cursor;    uint8_t     *marker;    uint8_t     *buffer;    uint8_t     *bufferEnd;    uint8_t     yych;    uint32_t    yyaccept;public:    // ----------------------------------------------------------------------    PushScanner()    {        limit = 0;        start = 0;        state = -1;        cursor = 0;        marker = 0;        buffer = 0;        eof = false;        bufferEnd = 0;    }    // ----------------------------------------------------------------------    ~PushScanner()    {    }    // ----------------------------------------------------------------------    void send(        Token token    )    {        size_t tokenSize = cursor-start;        const char *tokenName = tokenNames[token];        printf(            "scanner is pushing out a token of type %d (%s)",            token,            tokenName        );        if(token==kEOF) putchar('\n');        else        {            size_t tokenNameSize = strlen(tokenNames[token]);            size_t padSize = 20-(20<tokenNameSize ? 20 : tokenNameSize);            for(size_t i=0; i<padSize; ++i) putchar(' ');            printf(" : ---->");            fwrite(                start,                tokenSize,                1,                stdout            );            printf("<----\n");        }    }    // ----------------------------------------------------------------------    uint32_t push(        const void  *input,        ssize_t     inputSize    )    {        printf(            "scanner is receiving a new data batch of length %d\n"            "scanner continues with saved state = %d\n",            inputSize,            state        );        /*         * Data source is signaling end of file when batch size         * is less than maxFill. This is slightly annoying because         * maxFill is a value that can only be known after re2c does         * its thing. Practically though, maxFill is never bigger than         * the longest keyword, so given our grammar, 32 is a safe bet.         */        uint8_t null[64];        const ssize_t maxFill = 32;        if(inputSize<maxFill)        {            eof = true;            input = null;            inputSize = sizeof(null);            memset(null, 0, sizeof(null));        }        /*         * When we get here, we have a partially         * consumed buffer which is in the following state:         *                                                                last valid char        last valid buffer spot         *                                                                v                      v         * +-------------------+-------------+---------------+-------------+----------------------+         * ^                   ^             ^               ^             ^                      ^         * buffer              start         marker          cursor        limit                  bufferEnd         *          * We need to stretch the buffer and concatenate the new chunk of input to it         *         */        size_t used = limit-buffer;        size_t needed = used+inputSize;        size_t allocated = bufferEnd-buffer;        if(allocated<needed)        {            size_t limitOffset = limit-buffer;            size_t startOffset = start-buffer;            size_t markerOffset = marker-buffer;            size_t cursorOffset = cursor-buffer;                buffer = (uint8_t*)realloc(buffer, needed);                bufferEnd = needed+buffer;            marker = markerOffset + buffer;            cursor = cursorOffset + buffer;            start = buffer + startOffset;            limit = limitOffset + buffer;        }        memcpy(limit, input, inputSize);        limit += inputSize;        // The scanner starts here        #define YYLIMIT         limit        #define YYCURSOR        cursor        #define YYMARKER        marker        #define YYCTYPE         uint8_t        #define SKIP(x)         { start = cursor; goto yy0; }        #define SEND(x)         { send(x); SKIP();          }        #define YYFILL(n)       { goto fill;                }        #define YYGETSTATE()    state        #define YYSETSTATE(x)   { state = (x);  }    start:        #line 233 "<stdout>"{	YYCTYPE yych;yy0:	if((YYLIMIT - YYCURSOR) < 7) YYFILL(7);	yych = *YYCURSOR;	switch(yych) {	case 0x00:	goto yy32;	case 0x09:	case 0x0B:	case 0x0C:	case 0x0D:	case ' ':	goto yy30;	case 0x0A:	goto yy28;	case '(':	goto yy16;	case ')':	goto yy18;	case '*':	goto yy24;	case '+':	goto yy22;	case '-':	goto yy20;	case '/':	goto yy26;	case '0':	case '1':	case '2':	case '3':	case '4':	case '5':	case '6':	case '7':	case '8':	case '9':	goto yy12;	case '=':	goto yy14;	case 'A':	case 'B':	case 'C':	case 'D':	case 'E':	case 'F':	case 'G':	case 'H':	case 'I':	case 'J':	case 'K':	case 'L':	case 'M':	case 'N':	case 'O':	case 'P':	case 'Q':	case 'R':	case 'S':	case 'T':	case 'U':	case 'V':	case 'W':	case 'X':	case 'Y':	case 'Z':	case '_':	case 'a':	case 'c':	case 'd':	case 'j':	case 'k':	case 'l':	case 'm':	case 'n':	case 'o':	case 'p':	case 'q':	case 's':	case 't':	case 'u':	case 'v':	case 'x':	case 'y':	case 'z':	goto yy10;	case 'b':	goto yy7;	case 'e':	goto yy5;	case 'f':	goto yy4;	case 'g':	goto yy6;	case 'h':	goto yy11;	case 'i':	goto yy2;	case 'r':	goto yy9;	case 'w':	goto yy8;	default:	goto yy34;	}yy2:	++YYCURSOR;	switch((yych = *YYCURSOR)) {	case 'f':	goto yy67;	default:	goto yy39;	}yy3:#line 246 "push.re"	{ SEND(kIdentifier);     }#line 328 "<stdout>"yy4:	yych = *++YYCURSOR;	switch(yych) {	case 'o':	goto yy64;	default:	goto yy39;	}yy5:	yych = *++YYCURSOR;	switch(yych) {	case 'l':	goto yy60;	default:	goto yy39;	}yy6:	yych = *++YYCURSOR;	switch(yych) {	case 'o':	goto yy56;	default:	goto yy39;	}yy7:	yych = *++YYCURSOR;	switch(yych) {	case 'r':	goto yy51;	default:	goto yy39;	}yy8:	yych = *++YYCURSOR;	switch(yych) {	case 'h':	goto yy46;	default:	goto yy39;	}yy9:	yych = *++YYCURSOR;	switch(yych) {	case 'e':	goto yy40;	default:	goto yy39;	}yy10:	yych = *++YYCURSOR;	goto yy39;yy11:	yych = *++YYCURSOR;	goto yy39;yy12:	++YYCURSOR;	yych = *YYCURSOR;	goto yy37;yy13:#line 247 "push.re"	{ SEND(kDecimalConstant);}#line 378 "<stdout>"yy14:	++YYCURSOR;#line 249 "push.re"	{ SEND(kEqual);          }#line 383 "<stdout>"yy16:	++YYCURSOR;#line 250 "push.re"	{ SEND(kLeftParen);      }#line 388 "<stdout>"yy18:	++YYCURSOR;#line 251 "push.re"	{ SEND(kRightParen);     }#line 393 "<stdout>"yy20:	++YYCURSOR;#line 252 "push.re"	{ SEND(kMinus);          }#line 398 "<stdout>"yy22:	++YYCURSOR;#line 253 "push.re"	{ SEND(kPlus);           }#line 403 "<stdout>"yy24:	++YYCURSOR;#line 254 "push.re"	{ SEND(kStar);           }#line 408 "<stdout>"yy26:	++YYCURSOR;#line 255 "push.re"	{ SEND(kSlash);          }#line 413 "<stdout>"yy28:	++YYCURSOR;#line 257 "push.re"	{ SKIP();                }#line 418 "<stdout>"yy30:	++YYCURSOR;#line 258 "push.re"	{ SKIP();                }#line 423 "<stdout>"yy32:	++YYCURSOR;#line 259 "push.re"	{ send(kEOF); return 1;  }#line 428 "<stdout>"yy34:	++YYCURSOR;#line 260 "push.re"	{ SEND(kUnknown);        }#line 433 "<stdout>"yy36:	++YYCURSOR;	if(YYLIMIT == YYCURSOR) YYFILL(1);	yych = *YYCURSOR;yy37:	switch(yych) {	case '0':	case '1':	case '2':	case '3':	case '4':	case '5':	case '6':	case '7':	case '8':	case '9':	goto yy36;	default:	goto yy13;	}yy38:	++YYCURSOR;	if(YYLIMIT == YYCURSOR) YYFILL(1);	yych = *YYCURSOR;yy39:	switch(yych) {	case '0':	case '1':	case '2':	case '3':	case '4':	case '5':	case '6':	case '7':	case '8':	case '9':	case 'A':	case 'B':	case 'C':	case 'D':	case 'E':	case 'F':	case 'G':	case 'H':	case 'I':	case 'J':	case 'K':	case 'L':	case 'M':	case 'N':	case 'O':	case 'P':	case 'Q':	case 'R':	case 'S':	case 'T':	case 'U':	case 'V':	case 'W':	case 'X':	case 'Y':	case 'Z':	case '_':	case 'a':	case 'b':	case 'c':	case 'd':	case 'e':	case 'f':	case 'g':	case 'h':	case 'i':	case 'j':	case 'k':	case 'l':	case 'm':	case 'n':	case 'o':	case 'p':	case 'q':	case 'r':	case 's':	case 't':	case 'u':	case 'v':	case 'w':	case 'x':	case 'y':	case 'z':	goto yy38;	default:	goto yy3;	}yy40:	yych = *++YYCURSOR;	switch(yych) {	case 't':	goto yy41;	default:	goto yy39;	}yy41:	yych = *++YYCURSOR;	switch(yych) {	case 'u':	goto yy42;	default:	goto yy39;	}yy42:	yych = *++YYCURSOR;	switch(yych) {	case 'r':	goto yy43;	default:	goto yy39;	}yy43:	yych = *++YYCURSOR;	switch(yych) {	case 'n':	goto yy44;	default:	goto yy39;	}yy44:	++YYCURSOR;	switch((yych = *YYCURSOR)) {	case '0':	case '1':	case '2':	case '3':	case '4':	case '5':	case '6':	case '7':	case '8':	case '9':	case 'A':	case 'B':	case 'C':	case 'D':	case 'E':	case 'F':	case 'G':	case 'H':	case 'I':	case 'J':	case 'K':	case 'L':	case 'M':	case 'N':	case 'O':	case 'P':	case 'Q':	case 'R':	case 'S':	case 'T':	case 'U':	case 'V':	case 'W':	case 'X':	case 'Y':	case 'Z':	case '_':	case 'a':	case 'b':	case 'c':	case 'd':	case 'e':	case 'f':	case 'g':	case 'h':	case 'i':	case 'j':	case 'k':	case 'l':	case 'm':	case 'n':

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -