NAME OF THE EXPERIMENT: Implement lexical analyzer using lexical analyzer generating tools.
AIM: Implement the lexical analyzer using Jl.ex, flex or lex other lexical analyzer generating toolsLex tool used to generate lexical analyzers and parsers. The Overview describes the basic building blocks of a compiler and explains the interaction with lex. Conventional arithmetic operations and control statements, such as if-else and while, are implemented.
ALGORITHM:
Start
Declare the declarations for the given language tokens like digit, alphabet, white
Step1: Step2:
space, delimiters, etc..
digit[0-9] letter[A-Za-z]
delim\t\n]
W$ delim)-ID letter)(letter/digit))+ Integer (digit+
(ws) (print ("Special Characters")
(ID) (print("Identifiers"
(digit) (print("\n Integer
if (printf("keyword"))
else (print(keyword)
"&& (print(logical operators print/logical operators))
print(logical operators)
print(logical operators))
print(logical operators); (printf("\n \n");
printf("\n \n"))
printf("arithmetic operator"}}
(printi (arithmetic") printf("arithmetic"))
(printf("arithmetic"}}
%% (printf("arithmetic"))
Step3: Write the auxillary procedure in main() function
Step4: end
Step5: Stop
SOURCE CODE:
DIGIT [0-9]
LETTER [A-Za-z]
DELIM [\t\n]
WS {DELIM}+
ID {LETTER}({LETTER}|{DIGIT})+
INTEGER {DIGIT}+
%%
{WS} { printf("Special Characters\n"); }
{ID} { printf("Identifier: %s\n", yytext); }
{INTEGER} { printf("Integer: %s\n", yytext); }
"if" { printf("Keyword: if\n"); }
"else" { printf("Keyword: else\n"); }
">" { printf("Logical Operator: >\n"); }
"<" { printf("Logical Operator: <\n"); }
"<=" { printf("Logical Operator: <=\n"); }
"=>" { printf("Logical Operator: >=\n"); }
"=" { printf("Logical Operator: =\n"); }
"!=" { printf("Logical Operator: !=\n"); }
"&&" { printf("Relational Operator: &&\n"); }
"||" { printf("Relational Operator: ||\n"); }
"!" { printf("Relational Operator: !\n"); }
"+" { printf("Arithmetic Operator: +\n"); }
"-" { printf("Arithmetic Operator: -\n"); }
"*" { printf("Arithmetic Operator: *\n"); }
"/" { printf("Arithmetic Operator: /\n"); }
"%" { printf("Arithmetic Operator: %%\n"); }
. { printf("Unknown Character: %s\n", yytext); }
%%
int main() {
yylex();
return 0;
}
OUTPUT
[root@localhost]# lex lexprog. I
[root@localhost]# ce lex.yy.c
[root@localhostje ./a.out lexprog
1. What is are the functions of a Scanner?
2. What is Token?
3. What is lexeme, Pattern?
4. What is purpose of Lex?
5. What are the other tools used in Lexical Analysis?
To run this program, save the code in a file named wordcount.l, then compile and run it using the following commands in the terminal:bashCopy code
small example:
lex program to count number of words:%{
#include<stdio.h>
#include<string.h>
int word_count = 0;
%}
%%
[a-zA-Z]+ { word_count++; }
\n { printf("Number of words: %d\n", word_count); word_count = 0; }
. { /* ignore all other characters */ }
%%
int main()
{
printf("Enter text:\n");
yylex();
return 0;
}
flex wordcount.l
gcc lex.yy.c -lfl -o wordcount
./wordcount
Install:
Sudo apt-get update
sudo apt-get install bison flex