What is Lex and Yacc?
Lex is an application used on Unix/Linux systems whose main task is to divide an input stream into more useful items. Lex reads the file or standard input and then creates a C language program. That is why it is known as the "Word Analyzer".
On the other hand, Yacc (Yet Another Compiler-Compiler) is a compiler that creates a compiler for developed by Stephen C. Johnson. It is a Look Ahead Left-to-Right Rightmost Derivation parser generator, generating a LALR parser (the part of a compiler that tries to make syntactic sense of the source code) based on a formal grammar.
Lex and yacc are often used together. When you type "How to install lex" or "How to install yacc" in the internet, you can see that it is mentioned together.
How to install Lex and Yacc in Kali Linux?
GNU-based Linux distributions use Bison, which replaces Yacc, and Flex, the open source version of Lex. Now let's install them on Kali Linux.
Install Flex (Lex)
If you think the flex package is installed on your system, you can check it with the following command:
┌──(foc㉿foc)-[~]
└─$ sudo dpkg -l | grep flex
[sudo] password for foc:
ii flex 2.6.4-8 amd64 fast lexical analyzer generator
If there is no package information on the screen, flex is not installed. Firstly, search for the flex package in the repository:
──(foc㉿foc)-[~]
└─$ sudo apt search flex
Sorting... Done
Full Text Search... Done
flex/kali-rolling 2.6.4-8 amd64
fast lexical analyzer generator
In the output above, the flex package that can be installed on the system is displayed. For installation run the following command in terminal:
┌──(foc㉿foc)-[~]
└─$ sudo apt install flex
[sudo] password for foc:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libfl-dev libfl2 m4
Suggested packages:
bison flex-doc m4-doc
The following NEW packages will be installed:
flex libfl-dev libfl2 m4
0 upgraded, 4 newly installed, 0 to remove and 821 not upgraded.
Need to get 852 kB of archives.
After this operation, 1,975 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://kali.download/kali kali-rolling/main amd64 m4 amd64 1.4.18-5 [204 kB]
Get:2 http://kali.download/kali kali-rolling/main amd64 flex amd64 2.6.4-8 [440 kB]
.
Setting up flex (2.6.4-8) ...
Setting up libfl-dev:amd64 (2.6.4-8) ...
Processing triggers for libc-bin (2.33-6) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for kali-menu (2022.2.0) ...
After the above command, the package was installed with its dependencies.
bison
as the recommended package in flex installation.For information about Flex usage and parameters, you can get help with the following command:
──(foc㉿foc)-[~]
└─$ flex -h
Usage: flex [OPTIONS] [FILE]...
Generates programs that perform pattern-matching on text.
Miscellaneous:
-c do-nothing POSIX option
-n do-nothing POSIX option
-?
-h, --help produce this help message
-V, --version report flex version
Install Bison (Yacc)
Let's check out the bison package:
┌──(foc㉿foc)-[~]
└─$ sudo dpkg -l | grep bison
ii bison 2:3.8.2+dfsg-1 amd64 YACC-compatible parser generator
If there is no bison
package installed on the system, run the following command to view the package information in the repository:
──(foc㉿foc)-[~]
└─$ sudo apt search bison
Sorting... Done
Full Text Search... Done
bison/kali-rolling 2:3.8.2+dfsg-1 amd64
YACC-compatible parser generator
Now let's install the package from the kali-rolling repository:
┌──(foc㉿foc)-[~]
└─$ sudo apt install bison
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
bison-doc
The following NEW packages will be installed:
bison
0 upgraded, 1 newly installed, 0 to remove and 821 not upgraded.
Need to get 1,173 kB of archives.
After this operation, 3,186 kB of additional disk space will be used.
You can see more information about the bison package with the bison -h command:
┌──(foc㉿foc)-[~]
└─$ bison -h
Usage: bison [OPTION]... FILE
Generate a deterministic LR or generalized LR (GLR) parser employing
LALR(1), IELR(1), or canonical LR(1) parser tables.
Mandatory arguments to long options are mandatory for short options too.
The same is true for optional arguments.
Summary
There are many programs like Yacc and Lex. These include advanced and new features, but they are still preferred. It used to be installed by default in the Unix systems. To install on Kali Linux, simply follow the instructions in this article.
References
Bison - GNU Bison
Flex - Ubuntu Manpages Flex