| Tested on | Kali GNU/Linux Rolling 2026.2 (kali-rolling) |
|---|---|
| Package | flex 2.6.4-8.2+b5bison 2:3.8.2+dfsg-1+b3m4 1.4.19-8libfl-dev 2.6.4-8.2+b5 |
| Applies to | Kali Linux |
| Lab environment | Kali workstation or VM — install Kali on VirtualBox |
| Privilege | sudo for apt installs |
| Scope | Install flex and bison from apt, verify with version and help commands, compile a small flex scanner with gcc, and uninstall. Does not cover full compiler construction or exploit development workflows. |
Classic Lex and Yacc are the Unix names for a lexical analyzer and a parser generator. On Kali Rolling 2026.2 you install the GNU replacements flex and bison with apt, then confirm the tools with version checks and a short compile test.
What are Lex and Yacc on Linux?
Lex (lexical analyzer) reads character patterns and emits tokens. Yacc (Yet Another Compiler-Compiler) reads token grammars and emits a parser, usually as C code you compile with gcc.
Modern Debian-based systems, including Kali, package:
| Classic name | Kali package | Command you run |
|---|---|---|
| Lex | flex |
flex (also /usr/bin/lex when linked) |
| Yacc | bison |
bison and the yacc alternative |
Course notes and older books still say “lex and yacc.” On a current Kali image, install flex and bison, then follow the same .l and .y workflow the documentation describes.
Compare Lex and Yacc install options on Kali
| Method | Best for | Notes |
|---|---|---|
apt install flex bison |
Default Kali and Debian labs | Ships maintained GNU builds and man pages |
byacc |
Berkeley Yacc purists | Separate package; not required when bison is installed |
Legacy lex / yacc tarballs |
Historic Unix compatibility | Not in Kali repos; use flex/bison instead |
This guide uses the apt path because it matches Kali Rolling 2026.2 repositories and security updates.
Install flex and bison on Kali
Install the scanner and parser generators together with a C compiler toolchain. build-essential supplies gcc for the verification step later. Package installs use apt command on Kali.
sudo apt updateWhen indexes refresh without errors, you are ready to install packages.
sudo apt install -y flex bison build-essentialflex is already the newest version (2.6.4-8.2+b5).
bison is already the newest version (2:3.8.2+dfsg-1+b3).
Summary:
Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 784On a fresh image, apt pulls m4, libfl2, and libfl-dev as flex dependencies. The summary line confirms whether new packages were added or already present.
Confirm both packages are registered with dpkg:
dpkg -l flex bisonii bison 2:3.8.2+dfsg-1+b3 amd64 YACC-compatible parser generator
ii flex 2.6.4-8.2+b5 amd64 fast lexical analyzer generatorThe ii status means each package is installed and configured.
Verify flex and bison
Check that the binaries are on your PATH:
which flex bison yacc/usr/bin/flex
/usr/bin/bison
/usr/bin/yaccOn Kali, /usr/bin/yacc points through Debian alternatives to bison, so Yacc-style tutorials that call yacc still work.
Read flex version and help:
flex -Vflex 2.6.4flex -hUsage: flex [OPTIONS] [FILE]...
Generates programs that perform pattern-matching on text.
-h, --help produce this help message
-V, --version report flex versionCheck bison:
bison --versionbison (GNU Bison) 3.8.2
Written by Robert Corbett and Richard Stallman.bison -hUsage: bison [OPTION]... FILE
Generate a deterministic LR or generalized LR (GLR) parser employing
LALR(1), IELR(1), or canonical LR(1) parser tables.Those banners confirm the Lex/Yacc replacements are ready before you generate scanner or parser C code.
Compile a sample flex scanner
flex writes C source (typically lex.yy.c). Link it with libfl to prove the install end to end.
Create a line-and-character counter spec:
%{
int num_lines = 0, num_chars = 0;
%}
%%
\n { num_chars++; num_lines++; }
. { num_chars++; }
%%
int main() {
yylex();
printf("%8d %8d\n", num_lines, num_chars);
return 0;
}Save that as count.l in a working directory, then generate and compile:
WORK_DIR="${HOME}/lex-yacc-lab"
mkdir -p "${WORK_DIR}"Copy or paste the count.l contents into "${WORK_DIR}/count.l", then run:
cd "${WORK_DIR}" && flex count.lflex creates lex.yy.c in the same directory without printing a success banner.
gcc -lfl lex.yy.c -o countLinking exits silently when gcc succeeds.
Feed sample input through the scanner:
printf 'hello\nworld\n' | ./count2 12The first column is lines and the second is characters, which matches two lines (hello, world) and twelve characters including the newline characters.
Pair the scanner with a bison grammar when you build a full parser: run bison -d grammar.y, run flex scanner.l, then compile the generated .c files together with gcc. That two-file workflow is the modern Lex-plus-Yacc pattern on Kali.
Update flex and bison
Refresh indexes and upgrade when you maintain parser lab tools on a long-lived VM:
sudo apt update && sudo apt install --only-upgrade flex bisonapt reports 0 upgraded when your image already matches the rolling repository versions from the intro table.
Remove flex and bison
Drop the packages when you no longer need parser generators on the system:
sudo apt purge -y flex bisonsudo apt autoremove -yRemove project directories such as "${HOME}/lex-yacc-lab" separately if you created sample scanners during the lab.
Lex and Yacc install troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
flex: command not found |
Package not installed | sudo apt install flex |
bison: command not found |
Parser generator missing | sudo apt install bison |
gcc: error: lex.yy.c: No such file or directory |
Wrong output name or wrong directory | Run flex file.l in the directory that contains file.l; link lex.yy.c unless you passed -o |
undefined reference to yylex |
Missing flex library at link time | Add -lfl when linking: gcc -lfl lex.yy.c -o prog |
yacc: command not found after bison install |
Alternatives not configured | Reinstall bison; confirm /usr/bin/yacc exists |
| Parser builds fail with type errors | bison %union not defined |
Declare %union and token types in the .y file per bison manual |
References
Summary
On Kali Rolling 2026.2, Lex and Yacc work through the flex and bison packages. A single sudo apt install flex bison build-essential command supplies the scanner generator, Yacc-compatible parser generator, and gcc for linking generated C code.
You verified the install with flex -V, bison --version, and which yacc, then compiled a small count.l scanner that reported two lines and twelve characters for a sample input. That compile-and-run step is the practical check that libfl-dev and gcc are wired correctly—not merely that the packages appear in dpkg -l.
When coursework or tooling docs mention legacy lex and yacc commands, map them to flex and bison on Kali and keep generated sources in a dedicated project directory. Remove the packages with apt purge when the lab VM no longer needs parser generators.

