📄 lexer.c
字号:
/* No guarantee is expressed or implied by the distribution of this code. *//* Software written by Gerard J. Holzmann based on the public domain *//* ANSI-C parser Ctree Version 0.14 from Shaun Flisakowski *//* small tweask by Russ Cox to allow wide characters and string consts *//************************************************* * Shaun Flisakowski * revised 1996 Kurt Cockrum <kurt@grogatch.seaslug.org> * revised 2000 Gerard Holzmann <gholzmann [atsignhere] acm.org> * revised 2003 Russ Cox <rsc@achille.cs.bell-labs.com< *************************************************/#define DBG 0 /* debugging */#define ECHO /* flex on PC inserts an unwanted ECHO */#undef yywrapint yywrap(void);#define YY_BUFFER_STATE_DEFD#include <stdlib.h>#include "globals.h"#include "gram.h"#include "lexer.h"#include "token.h"#include "nmetab.h"#include "symtab.h"#include "heap.h"FILE *yyin;char line_buf[MAX_TOKN_LEN];int err_cnt = 0;int err_tok = 0;int err_type = 0;int in_preprocess = 0;int gcc2_paren_cnt = 0;char tokn_buff[MAX_TOKN_LEN];char *tokn_end = &tokn_buff[MAX_TOKN_LEN-1];char *tokn_ptr;int cnt_nl(char *, int);void *emalloc(uint);#undef YY_INPUT#ifdef STRING_PARSE#define min(X,Y) ((X)<(Y) ? (X) : (Y)) /* Scan input from a string instead */#define YY_INPUT(buf,result,max_size) \ { int n = min(max_size, Parse_TOS->parselimit - Parse_TOS->parseptr);\ if (n>0){ \ memcpy(buf,Parse_TOS->parseptr,(unsigned int) n); \ Parse_TOS->parseptr += n; \ result = n; \ } else \ result = YY_NULL; \ }#else /* not STRING_PARSE */ /* Scan input from a file pointer (default) */#define YY_INPUT(buf,result,max_size) \ { if (fgets((char*) (buf),max_size,yyin) == NULL) { \ result = YY_NULL; \ } else { \ result = strlen((char*)(buf)); \ strncpy(line_buf, (char*)(buf), result+1 /* MAX_TOKN_LEN */); \ } \ }#endif /* not STRING_PARSE */#define YY_USER_ACTION \ { Parse_TOS->yycolno = Parse_TOS->yynxtcol; \ Parse_TOS->yynxtcol += yyleng; \ }#undef SETPOS_NDE#define SETPOS_NDE(TYPE) \ { err_type = 0; \ lvalp->node = make_node(TN_EMPTY); \ lvalp->node->hdr.line = Parse_TOS->yylineno; \ lvalp->node->hdr.col = Parse_TOS->yycolno; \ lvalp->node->hdr.fnm = Parse_TOS->filename; \ err_tok = lvalp->node->hdr.tok = (int) (TYPE); \ }#undef SETPOS_LEAF#define SETPOS_LEAF(TOK_TYPE,PRSE_TYPE) \ { lvalp->leaf = make_leaf((err_type= (int) PRSE_TYPE)); \ lvalp->leaf->hdr.line = Parse_TOS->yylineno; \ lvalp->leaf->hdr.col = Parse_TOS->yycolno; \ lvalp->leaf->hdr.fnm = Parse_TOS->filename; \ err_tok = lvalp->leaf->hdr.tok = (int) (TOK_TYPE); \ }#undef SETPOS_IF#define SETPOS_IF(TYPE) \ { err_type = 0; \ lvalp->ifn = make_if(err_type= (int) TN_IF); \ lvalp->ifn->hdr.line = Parse_TOS->yylineno; \ lvalp->ifn->hdr.col = Parse_TOS->yycolno; \ lvalp->ifn->hdr.fnm = Parse_TOS->filename; \ err_tok = lvalp->ifn->hdr.tok = (int) (TYPE); \ }#undef SETPOS_FOR#define SETPOS_FOR(TYPE) \ { err_type = 0; \ lvalp->forn = make_for(err_type= (int) TN_FOR); \ lvalp->forn->hdr.line = Parse_TOS->yylineno; \ lvalp->forn->hdr.col = Parse_TOS->yycolno; \ lvalp->forn->hdr.fnm = Parse_TOS->filename; \ err_tok = lvalp->forn->hdr.tok = (int) (TYPE); \ }#undef LAST_STATE#define LAST_STATE() \ { if (in_preprocess) \ BEGIN(PP); \ else \ BEGIN(INITIAL); \ }#undef BEGIN_PP#define BEGIN_PP() \ { BEGIN(PP); \ in_preprocess = 1; \ }/* YYSTYPE my_var; not used, gjh */EXTERN int yylex ARGS((YYSTYPE *lvalp));/* To give some args to yylex */#undef YY_DECL#ifdef __STDC__/* #define YY_DECL int yylex YY_PROTO((YYSTYPE *lvalp)) */#define YY_DECL int yylex (YYSTYPE *lvalp)#else#define YY_DECL int yylex(lvalp) YYSTYPE *lvalp;#endif#define Start 1#define PP 2#define CMMT 3#define CC 4#define STR 5#define GCC2 6#line 995 "lex.yy.c"/* Macros after this point can all be overridden by user definitions in * section 1. */#ifndef YY_SKIP_YYWRAP#ifdef __cplusplusextern "C" int yywrap YY_PROTO(( void ));#elseextern int yywrap YY_PROTO(( void ));#endif#endif#ifndef YY_NO_UNPUTstatic void yyunput YY_PROTO(( int c, char *buf_ptr ));#endif#ifndef yytext_ptrstatic void yy_flex_strncpy YY_PROTO(( char *, yyconst char *, int ));#endif#ifdef YY_NEED_STRLENstatic int yy_flex_strlen YY_PROTO(( yyconst char * ));#endif#ifndef YY_NO_INPUT#ifdef __cplusplusstatic int yyinput YY_PROTO(( void ));#elsestatic int input YY_PROTO(( void ));#endif#endif#if YY_STACK_USEDstatic int yy_start_stack_ptr = 0;static int yy_start_stack_depth = 0;static int *yy_start_stack = 0;#ifndef YY_NO_PUSH_STATEstatic void yy_push_state YY_PROTO(( int new_state ));#endif#ifndef YY_NO_POP_STATEstatic void yy_pop_state YY_PROTO(( void ));#endif#ifndef YY_NO_TOP_STATEstatic int yy_top_state YY_PROTO(( void ));#endif#else#define YY_NO_PUSH_STATE 1#define YY_NO_POP_STATE 1#define YY_NO_TOP_STATE 1#endif#ifdef YY_MALLOC_DECLYY_MALLOC_DECL#else#if __STDC__#ifndef __cplusplus#include <stdlib.h>#endif#else/* Just try to get by without declaring the routines. This will fail * miserably on non-ANSI systems for which sizeof(size_t) != sizeof(int) * or sizeof(void*) != sizeof(int). */#endif#endif/* Amount of stuff to slurp up with each read. */#ifndef YY_READ_BUF_SIZE#define YY_READ_BUF_SIZE 8192#endif/* Copy whatever the last rule matched to the standard output. */#ifndef ECHO/* This used to be an fputs(), but since the string might contain NUL's, * we now use fwrite(). */#define ECHO (void) fwrite( yytext, yyleng, 1, yyout )#endif/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, * is returned in "result". */#ifndef YY_INPUT#define YY_INPUT(buf,result,max_size) \ if ( yy_current_buffer->yy_is_interactive ) \ { \ int c = '*', n; \ for ( n = 0; n < max_size && \ (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ buf[n] = (char) c; \ if ( c == '\n' ) \ buf[n++] = (char) c; \ if ( c == EOF && ferror( yyin ) ) \ YY_FATAL_ERROR( "input in flex scanner failed" ); \ result = n; \ } \ else \ { \ errno=0; \ while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ { \ if( errno != EINTR) \ { \ YY_FATAL_ERROR( "input in flex scanner failed" ); \ break; \ } \ errno=0; \ clearerr(yyin); \ } \ }#endif/* No semi-colon after return; correct usage is to write "yyterminate();" - * we don't want an extra ';' after the "return" because that will cause * some compilers to complain about unreachable statements. */#ifndef yyterminate#define yyterminate() return YY_NULL#endif/* Number of entries by which start-condition stack grows. */#ifndef YY_START_STACK_INCR#define YY_START_STACK_INCR 25#endif/* Report a fatal error. */#ifndef YY_FATAL_ERROR#define YY_FATAL_ERROR(msg) yy_fatal_error( msg )#endif/* Default declaration of generated scanner - a define so the user can * easily add parameters. */#ifndef YY_DECL#define YY_DECL int yylex YY_PROTO(( void ))#endif/* Code executed at the beginning of each rule, after yytext and yyleng * have been set up. */#ifndef YY_USER_ACTION#define YY_USER_ACTION#endif/* Code executed at the end of each rule. */#ifndef YY_BREAK#define YY_BREAK break;#endif#define YY_RULE_SETUP \ if ( yyleng > 0 ) \ yy_current_buffer->yy_at_bol = \ (yytext[yyleng - 1] == '\n'); \ YY_USER_ACTIONYY_DECL { register yy_state_type yy_current_state; register char *yy_cp, *yy_bp; register int yy_act;#line 183 "lexer.l"#line 1162 "lex.yy.c" if ( yy_init ) { yy_init = 0;#ifdef YY_USER_INIT YY_USER_INIT;#endif if ( ! yy_start ) yy_start = 1; /* first start state */ if ( ! yyin ) yyin = stdin; if ( ! yyout ) yyout = stdout; if ( ! yy_current_buffer ) yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); yy_load_buffer_state(); } while ( 1 ) /* loops until end-of-file is reached */ { yy_cp = yy_c_buf_p; /* Support of yytext. */ *yy_cp = yy_hold_char; /* yy_bp points to the position in yy_ch_buf of the start of * the current run. */ yy_bp = yy_cp; yy_current_state = yy_start; yy_current_state += YY_AT_BOL();yy_match: do { register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; if ( yy_accept[yy_current_state] ) { yy_last_accepting_state = yy_current_state; yy_last_accepting_cpos = yy_cp; } while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; if ( yy_current_state >= 572 ) yy_c = yy_meta[(unsigned int) yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; ++yy_cp; } while ( yy_base[yy_current_state] != 1145 );yy_find_action: yy_act = yy_accept[yy_current_state]; if ( yy_act == 0 ) { /* have to back up */ yy_cp = yy_last_accepting_cpos; yy_current_state = yy_last_accepting_state; yy_act = yy_accept[yy_current_state]; } YY_DO_BEFORE_ACTION;do_action: /* This label is used only to access EOF actions. */ switch ( yy_act ) { /* beginning of action switch */ case 0: /* must back up */ /* undo the effects of YY_DO_BEFORE_ACTION */ *yy_cp = yy_hold_char; yy_cp = yy_last_accepting_cpos; yy_current_state = yy_last_accepting_state; goto yy_find_action;case 1:YY_RULE_SETUP#line 184 "lexer.l"{ BEGIN(CMMT); tokn_ptr = tokn_buff; *tokn_ptr++ = '/'; *tokn_ptr++ = '*'; SETPOS_LEAF(COMMENT,TN_COMMENT); } YY_BREAKcase 2:YY_RULE_SETUP#line 190 "lexer.l"{ BEGIN(CC); SETPOS_LEAF(CHAR_CONST,TN_INT); } YY_BREAKcase 3:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -