int yylex( void ) { yylast_g = yylex0(); return(yylast_g); }
int yylex() { int token = yylex0(); int i; #ifdef LEXER_SAVE_TOKENCODE ((struct Token *)yylval)->code = token; #endif yyCommentBuffer[yyCommentLength] = 0; /* ensure NUL termination */ #ifdef DEBUG /* for debugging */ switch (token) { case IDENT: fprintf(stderr, "[IDENT %s]", ((struct Token *)yylval)->ident); return token; case T_YIELDS: fprintf(stderr, "[=>]"); return token; case T_HBAR: fprintf(stderr, "[---]"); return token; default: if (token > 0 && token < 256) { fprintf(stderr, "['%c']", token); return token; } for(i = 0; keywords[i].name; ++i) if (keywords[i].code == token) { fprintf(stderr, "[KW %s]", keywords[i].name); return token; } fprintf(stderr, "[TOKEN %d]", token); } #endif return token; }
static int yylex0() { int i, c, d, e, token; struct Token *val; static char ident[LEXER_IDENT_MAXLENGTH]; static char string[LEXER_STRING_MAXLENGTH]; if(creg >= MAX_COMMENTINFO-1) yyerror("!commentinfo-buffer overflow"); if (tmp != yylineno) { tmp = yylineno; //printf("Curren row:%d \n",yylineno); } if(creg == 0) { //initial bound struct CommentInfo cib; cib.bound = 1; cib.lastline = 1; cib.lastcol = 1; commentInfo[creg++] = cib; } else if(yylineno == specboundline + 2 && columnno == 0) { //bound after result spreg = creg; createBound(1); } val = (struct Token *) malloc(sizeof(struct Token)); if (!val) yyerror("!out of memory"); memset(val, 0, sizeof(struct Token)); yylval = (rml_t) val; c = get(); #ifdef LEXER_TOKEN_POSITION val->file = yyThisFileName; val->firstline = yylineno; val->firstcol = columnno; val->lastline = yylineno; val->lastcol = columnno; #endif switch (c) { case '\n': case ' ': case '\t': return yylex0(); case EOF: case 0: return EOF; case '=': /* '=' or '=>' or == or ==. */ d = get(); if (d == '>') { token = YIELDS; break; } if (d == '=') { e = get(); if (e == '.') { token = EQEQ_REAL; break; } unget(); token = EQEQ_INT; break; } unget(); token = EQUALS; break; case '<': /* < or <= or <=. or <. or <> or <>. */ d = get(); if (d == '.') { token = LT_REAL; break; } if (d == '=') { e = get(); if (e = '.') { token = LE_REAL; break; } unget(); token = LE_INT; break; } if (d == '>') { e = get(); if (e = '.') { token = NE_REAL; break; } unget(); token = NE_INT; break; } unget(); token = LT_INT; break; case '>': /* > or >= or >=. or >. */ d = get(); if (d == '.') { token = GT_REAL; break; } if (d == '=') { e = get(); if (e = '.') { token = GE_REAL; break; } unget(); token = GE_INT; break; } unget(); token = GT_INT; break; case '!': /* != or !=. */ d = get(); if (d == '=') { e = get(); if (e = '.') { token = NE_REAL; break; } unget(); token = NE_INT; break; } yyerror("invalid comparison operator!"); break; case ':': /* ':' or '::' */ d = get(); if (d == ':') { token = COLONCOLON; break; } unget(); token = COLON; break; case '(': /* '(' or '(*...comment...*)' */ d = get(); if (d == '*') { struct CommentInfo ci;//added ci.firstline = yylineno;//added ci.firstcol = columnno; yyCommentLength = 0; //added int innercomments = 1; while (d != EOF) { d = get(); if (d != '*' && d != '(') { if (yyCommentLength < LEXER_COMMENT_MAXLENGTH) ci.buffer[yyCommentLength++] = d; continue; } if (d == '(') { d = get(); if (d == '*') { if (yyCommentLength < LEXER_COMMENT_MAXLENGTH) ci.buffer[yyCommentLength++] = '('; if (yyCommentLength < LEXER_COMMENT_MAXLENGTH) ci.buffer[yyCommentLength++] = '*'; innercomments++; } else { if (yyCommentLength < LEXER_COMMENT_MAXLENGTH) ci.buffer[yyCommentLength++] = '('; } unget(); continue; } if (d == '*') { d = get(); if (d == ')') { if (yyCommentLength < LEXER_COMMENT_MAXLENGTH) ci.buffer[yyCommentLength++] = '*'; if (yyCommentLength < LEXER_COMMENT_MAXLENGTH) ci.buffer[yyCommentLength++] = ')'; innercomments--; if (innercomments == 0) { yyCommentLength--; /* don't put the last ')' */ break; } } else { if (yyCommentLength < LEXER_COMMENT_MAXLENGTH) ci.buffer[yyCommentLength++] = '*'; } unget(); continue; } } ci.buffer[yyCommentLength] = 0; ci.lastline = yylineno; //added ci.lastcol = columnno; //added ci.bound = 0; commentInfo[creg++] = ci;//added return yylex0(); /* comment: skip and return next token */ } unget(); iinner = 1; token = LPAR; break; case '-': /* '-' or horizontal bar */ d = get(); if (d == '-') { while (d == '-') d = get(); unget(); createBound(1); specboundline = yylineno; token = DASHES; break; } unget(); token = MINUS; break; case '"': /* string constant */ i = 0; while (1) { c = get(); if (c == '"') break; if (c == '\\') c = esc_char(0); if (i < LEXER_STRING_MAXLENGTH-1) string[i++] = c; else yyerror("string buffer overflow"); } string[i] = 0; token = SCON; val->u.string = strdup(string); //printf("String: %s", val->u.string); break; case '#': c = get(); if (c != '"') yyerror("invalid character constant"); c = get(); if (c == '\\') c = esc_char(1); d = get(); if (d != '"') yyerror("invalid character constant"); val->u.number = c; token = CCON; break; case ')': iinner = 0; token = RPAR; break; case '.': token = DOT; break; case ',': token = COMMA; break; case '/': token = DIV; break; case '+': token = PLUS; break; case '*': specboundline = yylineno; //can be problem if an expression... if(!iinner) createBound(3); token = STAR; break; case '|': token = BAR; createBound(1); specboundline = yylineno; break; case '&': token = AMPERSAND; createBound(1); break; case '[': token = LBRACK; break; case ']': token = RBRACK; break; case '%': token = MOD_INT; break; default: if (isalpha(c) || c == '\'' || c == '_') { i = 0; do { if (i < 50) { ident[i++] = c; #ifdef LEXER_TOKEN_POSITION val->lastline = yylineno; val->lastcol = columnno; #endif } c = get(); } while (isalnum(c) || c == '\'' || c == '_'); unget(); ident[i] = 0; if (!keywords_sorted) { init_sort_keywords(); keywords_sorted = 1; } for(i = 0; keywords[i].name; ++i) { if (!strcmp(ident, keywords[i].name)) { if((keywords[i].code == KW_RELATION) || (keywords[i].code == KW_RULE) || (keywords[i].code == KW_END) || (keywords[i].code == KW_MODULE) || (keywords[i].code == KW_TYPE) || (keywords[i].code == KW_VAL) || (keywords[i].code == KW_WITH)) { if(keywords[i].code == KW_END) { if(spreg != 0) { commentInfo[spreg].bound = 2; spreg = 0; } } createBound(1); } else if((keywords[i].code == KW_AXIOM) || (keywords[i].code == KW_DATATYPE) || (keywords[i].code == KW_AND)){ createBound(1); specboundline = yylineno; } return keywords[i].code; } } val->u.ident = strdup(ident); //printf("the string: %s \n\n",ident); return ident[0] == '\'' ? TYVARIDENT : IDENT; } else if (isdigit(c)) { int is_float = 0; i = 0; for(val->u.number = 0; isdigit(c); c = get()) { val->u.number = val->u.number*10 + c-'0'; if (i < LEXER_IDENT_MAXLENGTH) ident[i++] = c; }; if (c == '.') { do { if (i < LEXER_IDENT_MAXLENGTH) ident[i++] = c; c = get(); } while (isdigit(c)); is_float = 1; }; if (c == 'e') { do { if (i < LEXER_IDENT_MAXLENGTH) ident[i++] = c; c = get(); } while (isdigit(c) || c == '+' || c == '-'); is_float = 1; }; unget(); if (is_float) { ident[i] = 0; if (sscanf(ident, "%lf", &(val->u.realnumber)) == 1) { //printf("Real value %f",val->u.realnumber); token = RCON; break; } yyerror("invalid real number"); } token = ICON; break; } else yyerror("invalid input character"); } #ifdef LEXER_TOKEN_POSITION val->lastline = yylineno; val->lastcol = columnno; #endif return token; }