Compiler Design and Construction - Old Questions

2.  What are the functional responsibilities of a lexical analyzer? Write a lexical analyzer that recognizes the identifier of C language.

6 marks | Asked in 2072

Lexical Analyzer reads the input characters of the source program, groups them into lexemes, and produces a sequence of tokens for each lexeme. The tokens are then sent to the parser for syntax analysis.

Responsibilities of a lexical analyzer are:

  • It reads the input character and produces output sequence of tokens that the Parser uses for syntax analysis.
  • Lexical analyzer helps to identify token into the symbol table.
  • Lexical Analyzer is also responsible for eliminating comments and white spaces from the source program.
  • It also generates lexical errors.
  • Lexical analyzer is used by web browsers to format and display a web page with the help of parsed data from JavsScript, HTML, CSS

Second part:

/* Lex program to recognize the identifier of C language */

% {
#include <stdio.h>
%}
% %
^[a - z A - Z _ ][a - z A - Z 0 - 9 _ ]* printf("Valid Identifier");
^[^a - z A - Z _] printf("Invalid Identifier");
. ;
% %
main()
{
 yylex();
}