Thursday, November 13, 2008

Lexical analysis

Lexical analysis is the first step of compling a program. It involves converting sequence of characters to a sequence of tokens. Programs to perform lexical analysis known as lexers or lexical analysers are used to such conversions.

Tokens created by lexical analysis have two parts, one is the block of text corresponding to the token(Known as lexeme) and the other is the token type. So to give an example, a sequence like "a=1+2" after tokenisation will look like
Lexeme Token
a Variable
= equaltooperator
1 numericconstant
+ additionoperator
2 numericconstant
Now that we understans the basic concept of lexical anlysis, we might be tempted to write a Lexer :) . Writing lexers though possible might be a waste of time at times. There are programs known as "Lexer generators", yes u guessed it write, these are programs that generate lexer programs.

The lexer generators take a input file with details about the required characteristics of a lexer and generate the lexer. One lexer that seems to be popular is "flex". The input file for flex consists of 3 parts, the definiton part, the rules part and the user code section.

Each of the parts is separated by "%%". So a simple program in flex would be like

%%sayhello printf("Hello");

This just makes "sayhello" a token that has the action printf("Hello");. The flex has much much more that can be done. There are features to optimise the memory usage to suit the lexer u design.

Wednesday, November 12, 2008

The Rainmaker

A book that's popular enough to be made into a movie and a good read. The Rainmaker is one John Grisham's legal thrillers. Written in 1995, the book revolves around the life of a newly qualified lawyer and his fight with a big insurance company.

With its various sub-plots involving everything from underworld runaways to will changing grannies it has the reader turning pages.The best part is the anticipation of big money squezed out of a big bad insurance company.

It has very good ending, which lets the reader wanting for more.

Friday, November 7, 2008

Cascading error loss in Software

Cascading error is a phenomenon where one error condition leads to another error. This is more common than expected. Generally an error which is not expected to occur will lead to the error getting cascaded into another error. Since, the first error itself was not expected the error generated as a result of the first error will also be unexpected. Thus a cascade of errors continues.

Consider the example of a special character in the input a program. The special character will miss all the given conditions and will go to a error handling mechanism. The error handling mechanism may again fail due to the special character. This leads to a cascading error condition.

Now coming to the serious problem of error loss. A very serious error may get lost in the cascading error due to reduction in error severity. The severity of the error may get reduced in each step of the cascade and finally end up as a info alert. Although programming languages like Java have dedicated error handling mechanisms, the very nature of cascading errors makes it difficult to catch them. Thus, its better to have a catch all exceptions condition that reports the error that has been caught as the highest possible error severity, as the severity might have got reduced during the cascade.

Let me tell you a story to explain how cascading error loss occurs. A shop catches fire at night and starts burning. The onlookers see the burning shop and the news about the fire starts to spread. By the time the news reaches the owner of the shop, the location of the shop is not very clear. The shop owner comes to know about a shop on fire but does not realise that its his own shop. Similarly a very serious error can be overlooked due to cascading errors.