Master the fundamental concepts of lexical analysis through this focused micro-challenge.
A deterministic finite automaton (DFA) is a state machine that reads input symbols and transitions between states. Lex/Flex compiles regex patterns into DFAs; hand-written lexers implement the same logic implicitly. GCC's cpp and Clang's character classifier tables are DFAs in disguise.
Each state represents progress in recognizing a pattern. From state S0, digit goes to S_NUMBER; letter goes to S_IDENT; / might go to S_DIV or S_COMMENT if next char is /. Accept states emit tokens; trap states report errors.
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will build an explicit DFA transition table and use it to drive token recognition. This exercise requires encoding lexer states as integers, defining transitions for digits, letters, and operators, and emitting tokens when accept states are reached.
Build a DFA for recognizing integer and floating-point literals.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github