Created
January 7, 2015 19:04
-
-
Save yorickpeterse/f59d6e750a3360bc0b14 to your computer and use it in GitHub Desktop.
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
#include <string.h> | |
#include <stdio.h> | |
#include <malloc.h> | |
%%{ | |
machine test_lexer; | |
newline = [\n|\r\n]+; | |
integer = ('+'|'-')?[0-9]+; | |
operator = ['+''-''*''/''=''%''^']+; | |
main := |* | |
newline => { line += 1; column = 0; }; | |
integer => { | |
Lexer_emit("integer", ts, te, line, column); | |
column = Lexer_advance_column(ts, te, column); | |
}; | |
operator => { | |
Lexer_emit("operator", ts, te, line, column); | |
column = Lexer_advance_column(ts, te, column); | |
}; | |
space => { | |
column = Lexer_advance_column(ts, te, column); | |
}; | |
*|; | |
}%% | |
%% write data; | |
typedef struct { | |
const char *name; | |
const char *value; | |
int column_start; | |
int column_end; | |
int line; | |
} Lexer_Token; | |
int Lexer_advance_column(const char *ts, const char *te, int column) | |
{ | |
int length = te - ts; | |
return column + length; | |
} | |
Lexer_Token *Lexer_emit( | |
const char *name, | |
const char *ts, | |
const char *te, | |
int line, | |
int column | |
) | |
{ | |
int length = te - ts; | |
Lexer_Token *token = malloc(sizeof(Lexer_Token)); | |
token->name = name; | |
token->value = strndup(ts, length); | |
token->column_start = column; | |
token->column_end = column + length; | |
token->line = line; | |
return token; | |
} | |
void Lexer_lex(char *buffer) | |
{ | |
const char *p = buffer; | |
const char *pe = buffer + strlen(buffer); | |
const char *ts, *te; | |
char *eof = 0; | |
int line = 1; | |
int column = 0; | |
int act = 0; | |
int cs = 0; | |
Lexer_Token tokens[10]; | |
%% write init; | |
%% write exec noend; | |
} | |
int main() | |
{ | |
Lexer_lex("10 + 20"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment