Thursday, October 22, 2015

Lex and Yacc

Hey All,

This post explains how to install lex and yacc onto your Linux distros and how to run small programs.

For Debian based systems - there is a package called Flex. Install it. This is the alternative for lex. Here you go - the definition from Synaptic itself:

A Flex generates as output a C source file, lex.yy.c, which defines a routine yylex().  This file is compiled and linked with the -lfl library to produce an executable. When the executable is run, it analyzes its input for occurrences of the regular expressions. Whenever it finds one, it executes the corresponding C code.

Similarly to work yacc - there is tool called Bison. Install it using synaptic.

Or you can install it via cmd line issuing the command:

sudo apt-get install flex bison

After installation, you can execute the attached program [see dropbox link - https://www.dropbox.com/s/ynbcghps02vhmo4/a.l?dl=0] for counting the number of consonants and vowels in a given input.

Run the lexical analyzer to create tokens from the program - a.l

Issue the below commands:

[1]flex a.l

This would create a file called lex.yy.c - A C file. Compile this C file to get your executable

[2]cc lex.yy.c -o al -ll

Finally run the executable:

[3] ./al

You should not be able to type input to the program

Type "Linux" [without quotes] and press enter.

Type Ctrl + D to see the output as shown in the below diagram.



The next post will help you to figure out how to run a yacc program.

Till then, keep reading.

Tc,
Ananth S Gouri


No comments:

Post a Comment