Wednesday 5 May 2010

Lex and Yacc

Its been long since I've blogged about anything. I wanted to share lots of things, but unfortunately I couldn't find enough time to write on a topic. A few days back, I completed my lab exams successfully. I had a great time learning new concepts such as Lex and Yacc. Lex and Yacc are powerful tools through which we can design complex programs such as a compiler with very less effort. Most of the programming is done by Lex itself. Let me explain how it works!
Basically, we have got to write a lex program which has an extension ".l". The lex file contains lex specifications which are mainly regular expressions and their corresponding C response code. Once we write a regular expression, we can specify a C language function that will execute the necessary actions that are to be taken if input matches to one of the regular expressions. The lex can also return values to other programs. Lexers are written to analyse the syntax of anything. For e.g. a decimal number can be of form 90.34 or 1232e10.
Yacc is a tool to write parsers. Parsers are grammar analysers. We can specify grammars as we do in writing grammar for CFGs and other automata concepts. If yacc finds the input to be matching to the grammar, it finds the action to be taken and executes it.
According to me, they form a really powerful tool and a must learn for any developer/student of CSE. I just loved when I wrote shorter and simpler regular expressions for solving my lab exercises. I should thank Mr.Sai Ram for giving us an insight into the world of regular expressions,grammars and their implementations.
May be if time permits, I'm willing to do a spelling and grammar checking program and embed it into my own text editor.

2 comments:

adithya said...

nice :) but it seems to complex for ppl like me. but it was very informative, i will google more n find out :)

Dhananjay said...

We can directly use the shell in the Linux to do this. We can make the shell easily recognize string sequences and call a program(an executable) whenever a condition is satisfied. Quite useful technique for processing files using python