/* EBNF: simple-expression -> * addtivite-expression [relop additivie-expression] */ static TreeNode * simple_expression (void) { TreeNode * t = additive_expression(); TreeNode * newNode; while (token == COMMENT) unexpectedTokenHandling(); /* [relop additive-expression] */ if (isRelop(token)) { newNode = newExpNode (OpK); if (newNode != NULL) { newNode->child[0] = t; newNode->attr.op = token; t = newNode; } match (token); // match relop if (t != NULL) t->child[1] = additive_expression(); } return t; }
// 设已经弹入一个字符,CURRENT_CHAR指向当前待分析的字符(由于可能需要向后看一个字符) // 试探下一个字符,若是自己的一部分,则读入下一个字符再返回,否则直接返回 Token* Lex_getNextToken(){ Token* tk = (Token*) malloc (sizeof(Token)); tk -> type = TK_UNDEF; tk ->value = NULL; while(isSpace(CURRENT_CHAR)){ if(CURRENT_CHAR == '\n')curLine++; MOVE_NEXT_CHAR; } if(CURRENT_CHAR == EOF ){ // 注意先跳过格式字符再看是否EOF,否则文件最后的空格会识别错误 tk -> type = TK_EOF; // 删除:curLine=0; 原因:造成最后一个错误报告行为0 return tk; } if(isAlpha(CURRENT_CHAR)){ int i = 0; tk -> value = NEW_STRING; while(isAlpha(CURRENT_CHAR)||isDigit(CURRENT_CHAR)){ APPEND_CHAR(CURRENT_CHAR); MOVE_NEXT_CHAR; } APPEND_CHAR('\0'); // 保留字和标志符的识别 for(i=0;i< KEYWORDS_NUM ;i++){ if(0==strcmp(tk -> value ,keywords[i])){ tk -> type = i; //(tk_e) free(tk -> value);// 如果是关键字,就释放存储值的空间 return tk; } } tk -> type = TK_IDENT; return tk; } else if(isDigit(CURRENT_CHAR)){ // 无符号整数识别 tk -> value = NEW_STRING; while(isDigit(CURRENT_CHAR)){ APPEND_CHAR(CURRENT_CHAR); MOVE_NEXT_CHAR; } APPEND_CHAR('\0'); tk -> type = TK_NUM; return tk; } else if(isRelop(CURRENT_CHAR)){ tk -> value = NEW_STRING; APPEND_CHAR(CURRENT_CHAR); MOVE_NEXT_CHAR; if('=' == CURRENT_CHAR){ if(CURRENT_CHAR == '!'){ tk -> type = TK_UNDEF; } else { APPEND_CHAR(CURRENT_CHAR); MOVE_NEXT_CHAR; tk -> type = TK_RELOP; } } else { tk -> type = TK_RELOP; // 2014-7-6 Fixed BUG1 } APPEND_CHAR('\0'); return tk; } else if( '=' == CURRENT_CHAR ){ tk -> value = NEW_STRING; APPEND_CHAR(CURRENT_CHAR); MOVE_NEXT_CHAR; if('=' == CURRENT_CHAR){ APPEND_CHAR(CURRENT_CHAR); MOVE_NEXT_CHAR; tk -> type = TK_RELOP; } else{ tk -> type = TK_ASSIGN; } APPEND_CHAR('\0'); return tk; } else { switch(CURRENT_CHAR){ case '+':tk -> type = TK_ADD;break; case '-':tk -> type = TK_SUB;break; case '*':tk -> type = TK_MUL;break; case '/':tk -> type = TK_DIV;break; case '{':tk -> type = TK_LBRACE;break; case '}':tk -> type = TK_RBRACE;break; case '(':tk -> type = TK_LPAREN;break; case ')':tk -> type = TK_RPAREN;break; case ';':tk -> type = TK_SEMICOLON;break; case '|':tk -> type = TK_LOG_OR;break; case '&':tk -> type = TK_LOG_AND;break; default: tk -> type = TK_UNDEF; tk -> value = NEW_STRING; APPEND_CHAR(CURRENT_CHAR); APPEND_CHAR('\0'); break; } MOVE_NEXT_CHAR; return tk; } }
static int isOp (TokenType token) { if (isRelop(token)||isAddop(token)||isMulop(token)) return TRUE; else return FALSE; }
void check() { char ch; int i,cur=0; ch=str[cur]; while(ch!='\0') { char token[100]; i=0; if(isalpha(ch)||ch=='_') { while(isalpha(ch)||isdigit(ch)||ch=='_') { append(token,ch,i); i++; cur++; ch=str[cur]; } append(token,'\0',i); isKey(token); } else if(ispunct(ch)) { if(ch=='<'||ch=='>'||ch=='=') { append(token,ch,i); i++; cur++; ch=str[cur]; if(ch=='=') { append(token,ch,i); i++; cur++; ch=str[cur]; isRelop(token); } else isRelop(token); } else if(ch=='!') { append(token,ch,i); i++; cur++; ch=str[cur]; if(ch=='=') { append(token,ch,i); i++;; cur++; ch=str[cur]; isRelop(token); } else continue; } else if(ch=='('||ch==')'||ch=='{'||ch=='}'||ch=='['||ch==']') { isPar(ch); cur++; ch=str[cur]; } else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='^') { isOp(ch); cur++; ch=str[cur]; } else if(ch=='"') { while(ch!='"') { cur++; ch=str[cur]; } } else if(ch==','||ch==';') { cur++; ch=str[cur]; } } else if(strncmp(&ch," ",1)==0||ch=='\t'||ch=='\n') { while(strncmp(&ch," ",1)==0||ch=='\t'||ch=='\n') { cur++; ch=str[cur]; } } else if(isdigit(ch)) { append(token,ch,i); i++; cur++; ch=str[cur]; while(isdigit(ch)) { append(token,ch,i); i++; cur++; ch=str[cur]; } printf("%s\t\tNumeric constant\n",token); if(ch=='.') { append(token,ch,i); i++; cur++; ch=str[cur]; while(isdigit(ch)) { append(token,ch,i); i++; cur++; ch=str[cur]; } printf("%s\t\tNumeric constant\n",token); } } else { isUndef(ch); cur++; ch=str[cur]; } } }