Install Lex and Yacc on Kali Linux

Deepak Prasad
Tested on Kali GNU/Linux Rolling 2026.2 (kali-rolling)
Package flex 2.6.4-8.2+b5
bison 2:3.8.2+dfsg-1+b3
m4 1.4.19-8
libfl-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.

bash
sudo apt update

When indexes refresh without errors, you are ready to install packages.

bash
sudo apt install -y flex bison build-essential
output
flex 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: 784

On 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:

bash
dpkg -l flex bison
output
ii  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 generator

The ii status means each package is installed and configured.


Verify flex and bison

Check that the binaries are on your PATH:

bash
which flex bison yacc
output
/usr/bin/flex
/usr/bin/bison
/usr/bin/yacc

On Kali, /usr/bin/yacc points through Debian alternatives to bison, so Yacc-style tutorials that call yacc still work.

Read flex version and help:

bash
flex -V
output
flex 2.6.4
bash
flex -h
output
Usage: flex [OPTIONS] [FILE]...
Generates programs that perform pattern-matching on text.
  -h, --help              produce this help message
  -V, --version           report flex version

Check bison:

bash
bison --version
output
bison (GNU Bison) 3.8.2
Written by Robert Corbett and Richard Stallman.
bash
bison -h
output
Usage: 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:

text
%{
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:

bash
WORK_DIR="${HOME}/lex-yacc-lab"
mkdir -p "${WORK_DIR}"

Copy or paste the count.l contents into "${WORK_DIR}/count.l", then run:

bash
cd "${WORK_DIR}" && flex count.l

flex creates lex.yy.c in the same directory without printing a success banner.

bash
gcc -lfl lex.yy.c -o count

Linking exits silently when gcc succeeds.

Feed sample input through the scanner:

bash
printf 'hello\nworld\n' | ./count
output
2       12

The 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:

bash
sudo apt update && sudo apt install --only-upgrade flex bison

apt 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:

bash
sudo apt purge -y flex bison
bash
sudo apt autoremove -y

Remove 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.


Frequently Asked Questions

1. Are Lex and Yacc packages available on Kali Linux?

Kali Rolling ships GNU flex as the Lex replacement and GNU bison as the Yacc replacement. Install them with sudo apt install flex bison. Legacy Unix lex and yacc source packages are not in the default repositories.

2. What is the difference between flex and Lex?

Lex is the classic Unix lexical analyzer generator. flex is the GNU implementation that accepts similar .l scanner rules and is what Debian and Kali package as flex. Commands and tutorials that reference lex usually map to flex on modern Linux.

3. What is the difference between bison and Yacc?

Yacc is the original LALR parser generator format. bison implements a Yacc-compatible grammar syntax and installs a yacc command through the Debian alternatives system. Parser projects on Kali typically use bison even when documentation says yacc.

4. Do I need build-essential to use flex and bison?

flex and bison only generate C source files. You need gcc and libc development headers from build-essential to compile lex.yy.c or bison output into a binary. Install build-essential alongside flex and bison when you plan to link scanners and parsers.

5. How do I remove flex and bison from Kali?

Run sudo apt purge -y flex bison and sudo apt autoremove -y to drop libraries such as libfl-dev when nothing else depends on them. Remove your project build artifacts separately.
Kennedy Muthii

Information Security Analyst

Accomplished professional proficient in Python, ethical hacking, Linux, cybersecurity, and OSINT. With a track record including winning a national cybersecurity contest, launching a startup in Kenya, and holding a degree in information science, he is currently engaged in cutting-edge research in ethical hacking.

  • Python (programming language)
  • Certified Ethical Hacker
  • White Hat (Computer Security)
  • Linux
  • Penetration Testing