Loading the code workspace…
Implement a lexer that tokenizes a subset of C code.
Create a Lexer struct containing:
Implement these functions:
lexer_create(const char* source): Initialize lexer statelexer_destroy(Lexer* lexer): Clean up resourceslexer_next_token(Lexer* lexer): Return the next tokenlexer_peek(Lexer* lexer): Look at next token without consumingHandle these token types:
+ - * / ( ) { } ; ,== != <= >= && ||42, 0, 123)[a-zA-Z_][a-zA-Z0-9_]*int, return, if, else, whileSkip whitespace and single-line comments (//)
int main() { return 42; }Hand-written lexers power the compilers you use daily: Clang, GCC, V8, and Go's gc compiler all scan source with hand-rolled loops like the one you are writing, because they outperform generated scanners and give better error control. Lexing is also the same skill behind JSON parsers, syntax highlighters, and log analyzers.