📄 lex.c
字号:
#include <string.h>#include <stdlib.h>#include "eval.h"#include "evaltype.h"#include "evalparse.h"static int lex_pos;static int char2int(char a, int base){ int i; if ((a>='A') && (a<='Z')) { i=a-'A'+10; } else if ((a>='a') && (a<='z')) { i=a-'a'+10; } else if ((a>='0') && (a<='9')) { i=a-'0'; } else return -1; if (i>=base) return -1; return i;}static int parse_float(eval_scalar *f, char *fpn){ char *end; double d; d=strtod(fpn, &end); if (*end == 0) { f->type=SCALAR_FLOAT; f->scalar.floatnum.value=d; return 1; } return 0;}static int parse_integer(eval_scalar *i, char *num, int base, int lenmod){ uint64 k = 0; int l = strlen(num) + lenmod; while (l--) { int c=char2int(*num, base); if (c==-1) return 0; k *= base; k += c; num++; } i->type=SCALAR_INT; i->scalar.integer.value=k; i->scalar.integer.type=TYPE_UNKNOWN; return 1;}static int parse_cstring(eval_scalar *r, char *s, int len){ char *result; int alloclen = len; if (alloclen < 1) alloclen = 1; r->type = SCALAR_STR; r->scalar.str.value = (char*)malloc(alloclen); if (!r->scalar.str.value) return 0; result = r->scalar.str.value; // may not end with '\\' if (len && s[len-1] == '\\') return 0; while (s && *s && len) { if (*s == '\\') { s++;len--;if (!len) break; switch (*s) { case '0': *result++='\0'; break; case 'a': *result++='\a'; break; case 'b': *result++='\b'; break; case 'e': *result++='\e'; break; case 'f': *result++='\f'; break; case 'n': *result++='\n'; break; case 'r': *result++='\r'; break; case 't': *result++='\t'; break; case 'v': *result++='\v'; break; case '\"': *result++='"'; break; case '\\': *result++='\\'; break; case 'x': { int p, q; s++;len--;if (!len) break; p=char2int(*s, 16); if (p==-1) return 0; s++;len--;if (!len) break; q=char2int(*s, 16); if (q==-1) return 0; *result++=(char)p*16+q; break; } default: *result++='\\'; if (len) *result++=*s; break; } } else { *result++ = *s; } s++;len--; } r->scalar.str.len=result-r->scalar.str.value; return 1;}static int parse_pstring(eval_scalar *s, char *cstr, int len){ int alloclen=len; if (!len) alloclen=1; s->type=SCALAR_STR; s->scalar.str.value=(char*)malloc(alloclen); memcpy(s->scalar.str.value, cstr, len); s->scalar.str.len=len; return 1;}void *lex_current_buffer(){ return (void*)YY_CURRENT_BUFFER;}int lex_current_buffer_pos(){ return lex_pos;}void lex_switch_buffer(void *buffer){ yy_switch_to_buffer(buffer); }void lex_delete_buffer(void *buffer){ yy_delete_buffer(buffer); }void *lex_scan_string_buffer(const char *str){ return yy_scan_string(str);}/**/#line 700 "lex.c"#define INITIAL 0#ifndef YY_NO_UNISTD_H/* Special case for "unistd.h", since it is non-ANSI. We include it way * down here because we want the user's section 1 to have been scanned first. * The user has a chance to override it with an option. */#include <unistd.h>#endif#ifndef YY_EXTRA_TYPE#define YY_EXTRA_TYPE void *#endifstatic int yy_init_globals (void );/* Macros after this point can all be overridden by user definitions in * section 1. */#ifndef YY_SKIP_YYWRAP#ifdef __cplusplusextern "C" int yywrap (void );#elseextern int yywrap (void );#endif#endif static void yyunput (int c,char *buf_ptr ); #ifndef yytext_ptrstatic void yy_flex_strncpy (char *,yyconst char *,int );#endif#ifdef YY_NEED_STRLENstatic int yy_flex_strlen (yyconst char * );#endif#ifndef YY_NO_INPUT#ifdef __cplusplusstatic int yyinput (void );#elsestatic int input (void );#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_LVALUE->yy_is_interactive ) \ { \ int c = '*'; \ size_t 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/* end tables serialization structures and prototypes *//* Default declaration of generated scanner - a define so the user can * easily add parameters. */#ifndef YY_DECL#define YY_DECL_IS_OURS 1extern int yylex (void);#define YY_DECL int yylex (void)#endif /* !YY_DECL *//* 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 \ YY_USER_ACTION/** The main scanner function which does all the work. */YY_DECL{ register yy_state_type yy_current_state; register char *yy_cp, *yy_bp; register int yy_act; #line 192 "lex.l"#line 856 "lex.c" if ( !(yy_init) ) { (yy_init) = 1;#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 ) { yyensure_buffer_stack (); YY_CURRENT_BUFFER_LVALUE = 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_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 >= 66 ) 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] != 130 );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 194 "lex.l"/* nop */ YY_BREAKcase 2:/* rule 2 can match eol */YY_RULE_SETUP#line 195 "lex.l"if (parse_cstring(&YY_LVALP->scalar, yytext+1, strlen(yytext+1)-1)) return EVAL_STR; YY_BREAKcase 3:/* rule 3 can match eol */YY_RULE_SETUP#line 196 "lex.l"if (parse_pstring(&YY_LVALP->scalar, yytext+1, strlen(yytext+1)-1)) return EVAL_STR; YY_BREAKcase 4:YY_RULE_SETUP#line 197 "lex.l"return EVAL_STR_LT; YY_BREAKcase 5:YY_RULE_SETUP#line 198 "lex.l"return EVAL_STR_LE; YY_BREAKcase 6:YY_RULE_SETUP#line 199 "lex.l"return EVAL_STR_GT; YY_BREAKcase 7:YY_RULE_SETUP#line 200 "lex.l"return EVAL_STR_GE; YY_BREAKcase 8:YY_RULE_SETUP#line 201 "lex.l"return EVAL_STR_EQ; YY_BREAKcase 9:YY_RULE_SETUP#line 202 "lex.l"return EVAL_STR_NE; YY_BREAKcase 10:YY_RULE_SETUP#line 203 "lex.l"return EVAL_POW; YY_BREAKcase 11:YY_RULE_SETUP#line 204 "lex.l"return EVAL_SHL; YY_BREAKcase 12:YY_RULE_SETUP#line 205 "lex.l"return EVAL_SHR; YY_BREAKcase 13:YY_RULE_SETUP#line 206 "lex.l"return EVAL_LT; YY_BREAKcase 14:YY_RULE_SETUP#line 207 "lex.l"return EVAL_LE; YY_BREAKcase 15:YY_RULE_SETUP#line 208 "lex.l"return EVAL_GT; YY_BREAKcase 16:YY_RULE_SETUP#line 209 "lex.l"return EVAL_GE; YY_BREAKcase 17:YY_RULE_SETUP#line 210 "lex.l"return EVAL_EQ; YY_BREAKcase 18:YY_RULE_SETUP#line 211 "lex.l"return EVAL_NE; YY_BREAKcase 19:YY_RULE_SETUP#line 212 "lex.l"return EVAL_LAND; YY_BREAKcase 20:YY_RULE_SETUP#line 213 "lex.l"return EVAL_LOR; YY_BREAKcase 21:YY_RULE_SETUP#line 214 "lex.l"return EVAL_LXOR; YY_BREAKcase 22:YY_RULE_SETUP#line 215 "lex.l"YY_LVALP->ident=strdup(yytext); return EVAL_IDENT; YY_BREAKcase 23:YY_RULE_SETUP#line 216 "lex.l"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -