Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- typedef struct {
- const char* text;
- unsigned text_len;
- unsigned text_pos;
- const char* token;
- unsigned token_len;
- } lexer_t;
- bool lexer_next_token(lexer_t* self)
- {
- unsigned i;
- unsigned len;
- char ch;
- for(i = self->text_pos, len = 0;
- i != self->text_len;
- i++, len++) {
- ch = self->text[i];
- if((ch < 'A' || ch > 'Z') &&
- (ch < 'a' || ch > 'z')) {
- if(len == 0 && ch != ' ') {
- self->token = &self->text[i];
- self->token_len = 1;
- self->text_pos = i + 1;
- return true;
- }
- put_token:
- self->token = &self->text[i-len];
- self->token_len = len;
- self->text_pos = i + 1;
- return true;
- }
- }
- if(len) {
- self->token = &self->text[i-len];
- self->token_len = len;
- self->text_pos = i;
- return true;
- }
- return false;
- }
- int main(int argc, char* argv[])
- {
- const char* text = "hello world";
- lexer_t lexer = {0};
- lexer.text = text;
- lexer.text_len = strlen(text);
- while(lexer_next_token(&lexer)) {
- printf("%.*s\n",(int)lexer.token_len,lexer.token);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement