Professional Documents
Culture Documents
Lexical analyzer
How to implement lexical analyzer by using lex tools
• Systematically translate regular definitions into c source code for efficient scanning
4
Program structure
• In the input file, there are 3 sections;
1.Declaration section:
Useful for declaration of c variables ,constants and header files
%{ Example %{
variables ,constant int a,b;
float c=2.0;
%} %}
Useful for defining regular expression not enclosed by this two symbol % { %}
Example: digit [0-9]
letter [a-Za-Z]
2. The rules section
8
Lex functions
yylex() - The function that starts the analysis. It is automatically generated by Lex.
Reads the input stream and generate tokens according to the regular
expression/pattern written in rules section
yywrap() - This function is called when end of file (or input) is encountered. If this
9
Examples
%{
#include <stdio.h>
%}
%%
if|else|while|do|switch|case { printf("%s is keyword \n", yytext); }
[a-zA-Z][a-zA-Z|0-9]* { printf("%s is identifier \n", yytext); }
[0-9]+(\.[0-9]+)?(E[+\-]?[0-9]+)? { printf("%s is number \n", yytext); }
"<"|"<="|"="|"<>"|">"|">=" { printf("%s is relational operator \n", yytext); }
"!"|"@"|"*"|"&"|"^"|"%"|"$"|"#" {printf("%s Special Character\n",yytext);}
%%
int yywrap()
{
return 1;
}
main()
{
printf("Enter a string of data\n");
yylex();
}
Examples
%{
#include <stdio.h>
%}
digit [0-9]
letter [A-Za-z]
id {letter}({letter}|{digit})*
number {digit}+(\.{digit}+)?(E[+\-]?{digit}+)?
%%
if|else|while|do|switch|case { printf("%s is keyword \n", yytext); }
{id} { printf("%s is identifier \n", yytext); }
{number} { printf("%s is number \n", yytext); }
"<"|"<="|"="|"<>"|">"|">=" { printf("%s is relational operator \n", yytext); }
"!"|"@"|"*"|"&"|"^"|"%"|"$"|"#" {printf("%s Special Character\n",yytext);}
%%
int yywrap()
{
return 1;
}
main()
{
printf("Enter a string of data\n");
yylex();
}
How to run lex
• In cmd write
• lex name.l
• cc lex.yy.c
• a.exe
Lexical analyzer
• flex is a tool for generating scanners. A scanner is a program
which recognizes lexical patterns in text.
• The flex program reads the given input files, or its standard
input if no file names are given, for a description of a scanner
to generate.
Lex Lex
lex.l lex.yy.c
and token definitions compiler
y.tab.h
lex.yy.c C a.out
y.tab.c compiler
17
Lex Yacc interaction…
If lex is to return tokens that yacc will process, they
have to agree on what tokens there are. This is
done as follows:
The yacc file will have token definitions
%token INTEGER
in the definitions section.
When the yacc file is translated with yacc -d, a header file
y.tab.h is created that has definitions like
#define INTEGER 258
This file can then be included in both the lex and yacc
program.
The lex file can then call return INTEGER, and the yacc
program can match on this token.
18
To design program in yacc language
yyval-value associated with the tokens are returned by lex in the
variable yyval.
yyval=atoi(yytext):converts input to numeric and stores in a variable
yytext: it is a pointer to the input character stream/matched input string
yywrap(): called by lex/yacc when input is exhausted/finished(return 1
when input finished)
yyparse():it is responsible for parsing to occur. It reads tokens and
executes the actions. if it gives 0 means string accepted.
Structure of program
As with Lex, a Yacc program is also divided into three
%%
translation rules
%%
• Flex name.l
• Yacc –d name.y
• cc lex.yy.c y.tab.c
• a.out
Lax
a program for validating strings accepted by a language {L=a^n|n>=1} {a,aa,aaa….}
S aS/null
%{
#include<stdio.h> Generated by yacc, when u compile yacc
#include "y.tab.h" program the Yacc compiler crates 2 separate
%} files
%% 1 file that contains the c code and an other file it
[a] {return A;} contain definition of the tokens .
[\n] {return yytext[0];} so,that definition of the token required in here
. { return 0;}
%%
yywrap()
{
return 1;
}
YACC
%{
#include<stdio.h>
%}
%token A
%%
start:S '\n'{return 0;}
S:A S
|;
%%
main()
{
printf("\n enter string");
if(yyparse()==0)
printf("\n valid");
}
yyerror()
{
printf("\n notaccepted");
}
END