A Telnet protocol command parser built with Flex (lexical analyzer) and Bison (parser generator). It reads lines of text, identifies Telnet IAC (Interpret As Command) sequences, and classifies each line as either an IAC command or regular body text.
Telnet uses a command/response protocol where special sequences are prefixed with IAC (Interpret As Command, byte 0xFF). This parser reads textual representations of Telnet commands (e.g., IAC DO ECHO) and parses them into structured output.
Input:
IAC DO ECHO
Hello from Telnet client!
IAC WILL TERMINAL-TYPE
Output:
[IAC] DO ECHO
[Body] Hello from Telnet client!
[IAC] WILL TERMINAL-TYPE
The parser recognizes two types of lines:
- IAC lines:
IAC <COMMAND> <OPTION>- Commands:
DO,DONT,WILL,WONT - Options:
ECHO,SUPPRESS-GO-AHEAD,TERMINAL-TYPE,NAWS,LINEMODE,NEW-ENVIRON,STATUS,BINARY,SGA, or anyWORD
- Commands:
- Body lines: Free-form text (one or more words)
- GCC (or any C99-compatible compiler)
- Flex (lexical analyzer)
- Bison (parser generator)
makemake run
# or
./telnet_parser < input.textmake clean # remove binary
make distclean # remove binary AND generated C files| File | Description |
|---|---|
telnet.y |
Bison grammar definition (.y file) |
telnet.l |
Flex lexer definition (.l file) |
telnet.tab.c |
Generated C parser (from Bison) |
telnet.tab.h |
Generated parser header (from Bison) |
lex.yy.c |
Generated C lexer (from Flex) |
input.text |
Sample input file |
Makefile |
Build automation |
Edit telnet.l and add new option tokens following the existing pattern:
"MY-NEW-OPTION" { yylval.str = strdup("MY-NEW-OPTION"); return OPTION; }The lexer uses %option case-insensitive, so iac do echo, IAC DO ECHO, and Iac Do Echo are all treated identically.
MIT