📄 lex.c
字号:
/*****************************************************************************
* *
* --------------------------------- lex.c -------------------------------- *
* *
*****************************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "chtbl.h"
#include "lex.h"
#include "symbol.h"
/*****************************************************************************
* *
* ---------------------------------- lex --------------------------------- *
* *
*****************************************************************************/
Token lex(const char *istream, CHTbl *symtbl) {
Token token;
Symbol *symbol;
int length,
retval,
i;
/*****************************************************************************
* *
* Allocate space for a symbol. *
* *
*****************************************************************************/
if ((symbol = (Symbol *)malloc(sizeof(Symbol))) == NULL)
return error;
/*****************************************************************************
* *
* Process the next token. *
* *
*****************************************************************************/
if ((symbol->lexeme = next_token(istream)) == NULL) {
/**************************************************************************
* *
* Return that there is no more input. *
* *
**************************************************************************/
free(symbol);
return lexit;
}
else {
/**************************************************************************
* *
* Determine the token type. *
* *
**************************************************************************/
symbol->token = digit;
length = strlen(symbol->lexeme);
for (i = 0; i < length; i++) {
if (!isdigit(symbol->lexeme[i]))
symbol->token = other;
}
memcpy(&token, &symbol->token, sizeof(Token));
/**************************************************************************
* *
* Insert the symbol into the symbol table. *
* *
**************************************************************************/
if ((retval = chtbl_insert(symtbl, symbol)) < 0) {
free(symbol);
return error;
}
else if (retval == 1) {
/***********************************************************************
* *
* The symbol is already in the symbol table. *
* *
***********************************************************************/
free(symbol);
}
}
/*****************************************************************************
* *
* Return the token for the parser. *
* *
*****************************************************************************/
return token;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -