lex and yacc

If you lift the lid on the engine compartment of your car (bonnet to English-speakers, hood to Americans), you see a complex arrangement of TUBES, wires, boxes, containers, and mysterious objects. Worse, they may be concealed by covers, bundles and conduits. If you have a friend or perhaps classroom instruction, with the help of a readable manual, you can find your way and check your fluids, look for damage, identify the source of a mysterious new noise.

Should you decide that there is a problem that you can fix yourself, you will go to your tool box to choose the right wrench (spanner) or screwdriver to fix the problem.

Linux is not different. And the tool box is vast.

Or perhaps you wish to make something new and different. Linux (and Unix, and all that goes before) provides a huge tool box (the USPTO pretending to not know (“I am not knowing” (has this been outsourced too?)). The great advantage of the Linux distributions is that they are free (as in freedom) and free or inexpensive (in cost). When you receive benefit, give back.

In my home tool box, I have a basin wrench . Cost per use $5. It allows one to reach into the inaccessable space between the wall and the basin to loosen or tighten the nut that holds the faucet to the basin or the supply-line to the faucet.

LEX and YACC are just such tools. Like many tools in the Unix/linux world, the have a specific application. And they are absolutely genius in what they do.

“Proper tool for the proper job”. Each tool does one thing very well. Something else? That is why there is | (pipe)

If you are not trying to create a programme or solve specific problem, you may already know every thing you need to know.

lex – a tool to generate (in conjunction with yacc) C code for a lexical scanner
yacc – a tool to generate (in conjunction with lex) C code for a grammer interpreter.

In naive terms, lex – what do I see, yacc – what do I do with it.

Think about this problem: You are trying to create program that “understands” specific input words and is able to act on them.
lex is the tool that can do this. The “shorthand” description to lex and the C compiler can create a program for Linux that scans the input stream for specific values, and generates specific results.

If the inputs are more complicated, you may need to have the program interpret as an example “what to do” “thing that it is done to”. This is where yacc come in.

To add to the confusion, lex is the classic Unix program, flex is the Linux equivalent. Mostly compatable but subtly different.

Similarly, yacc is a Unix program, bison is the linux equivalent. Fortunately, flex and bison have compatability so previous knowledge and program source is not rendered valueless.

More in a future post.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.