Created
December 11, 2012 06:31
-
-
Save Liutos/4256337 to your computer and use it in GitHub Desktop.
tokenizer函数演示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef enum { | |
LPARENTHESIS, | |
RPARENTHESIS, | |
SYMBOL_TOKEN, | |
EOF_TOKEN, | |
}; | |
void consume(lexer_t lexer) | |
{ | |
if (lexer->c != '\0') { | |
lexer->p++; | |
lexer->c = lexer->string[lexer->p]; | |
} | |
} | |
token_t tokenizer(lexer_t lexer) | |
{ | |
while (lexer->c != '\0') { | |
switch (lexer->c) { | |
case '(': consume(lexer); return make_token(LPARENTHESIS, "("); | |
case ')': consume(lexer); return make_token(RPARENTHESIS, ")"); | |
case ' ': consume(lexer); continue; | |
default: { | |
int start = lexer->p; | |
while (lexer->c != ' ' && lexer->c != '\0' && | |
lexer->c != '(' && lexer->c != ')') | |
consume(lexer); | |
return make_token(SYMBOL_TOKEN, strndup(lexer->string + start, lexer->p - start)); | |
} | |
} | |
} | |
return make_token(EOF_TOKEN, "#<EOF>"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
token_t make_token(TokenType type, char *text) | |
{ | |
token_t token = malloc(sizeof(struct token_t)); | |
token->type = type; | |
token->text = text; | |
return token; | |
} | |
token_t tokenizer(char *string) | |
{ | |
switch (*string) { | |
case '(': return make_token(LPARENTHESIS, "("); | |
case ')': return make_token(RPARENTHESIS, ")"); | |
case ' ': break; | |
default: { | |
int i = 0; | |
while (string[i] != ' ' && string[i] != '\0' && | |
string[i] != '(' && string[i] != ')') | |
i++; | |
return make_token(SYMBOL_TOKEN, strndup(string, i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment