Wednesday, 8 November 2017

Lexical Analyzer for Sample Language

                       In this post, we will see LEX code to generate Lexical Analyzer for sample language.


                      Lexical Analyzer reads the input character by character. Each meaningful word in input code is called as Lexeme. Token is a class or category of Lexeme. 
                      Lexical Analyzer tokenize the input code i.e. it decides the Token for each Lexeme based on the Regular Expressions or Patterns that we provide to the LEX program.
                     Lexical Analyzer skips or avoid the white spaces (e.g. tab).
                     Lexical analyzer makes an entry of each variable into symbol table.
                     Lexemes which do not match with patterns provided in LEX file are considered as lexical errors or invalid characters.

                     Go through the following program. It will clear your doubts regarding the functioning of Lexical Analyzer. Here, we are providing input from terminal. (You can also provide input through text file. For this, check http://www.comrevo.com/2016/01/lex-yacc-input-file-example.html).                 
                                
Program (lexicalanalyzer.l)
%{
#include<stdio.h>
char name[20];
int i=0;
%}

%%
main {printf("%s: main function.\n",yytext);}
[(] {printf("%s: opening bracket.\n",yytext);}
[)] {printf("%s: closing bracket.\n",yytext);}
[{] {printf("%s: opening Curly bracket.\n",yytext);}
[}] {printf("%s: closing Curly bracket.\n",yytext);}
void|int|float|char {printf("%s: data type.\n",yytext);}
" " ;
[a-zA-Z]+ {printf("%s: Identifier.\n",yytext); strcpy(name, yytext);}
[;] {printf("%s: Semi Colon .\n",yytext);}
[\t] ;
. {printf("Lexical Error:%s is Unknown Character\n",yytext);} 
[\n] return 0;

%%
int yywrap()
 {
  return 1;
 }

int main()
 {
  printf("Enter Input (Sample Language):\n");
  yylex();
  printf("\nSymbol Table\n");
  printf("Variable\n");
  printf("%s\n",name);
  return 0;
 }
  
How To Run:
parag@parag-Inspiron-N4010:~/Desktop/programs$ lex lexicalanalyzer.l
parag@parag-Inspiron-N4010:~/Desktop/programs$ gcc lex.yy.c
parag@parag-Inspiron-N4010:~/Desktop/programs$ ./a.out


Output:
parag@parag-Inspiron-N4010:~/Desktop/programs$ lex lexicalanalyzer.l
parag@parag-Inspiron-N4010:~/Desktop/programs$ gcc lex.yy.c
parag@parag-Inspiron-N4010:~/Desktop/programs$ ./a.out
Enter Input (Sample Language):
int main() {float b; $$}
int: data type.
main: main function.
(: opening bracket.
): closing bracket.
{: opening Curly bracket.
float: data type.
b: Identifier.
;: Semi Colon .
Lexical Error:$ is Unknown Character
Lexical Error:$ is Unknown Character
}: closing Curly bracket.

Symbol Table
Variable
b


                   Check LEX YACC program to evaluate arithmetic expression in this link http://www.comrevo.com/2015/06/lex-and-yacc-programs-for-arithmetic-expression-evaluation.html.

                     Check LEX YACC programs To Parse Input Text File in this link http://www.comrevo.com/2016/01/lex-yacc-input-file-example.html.

No comments:

Post a comment