SPCC Module 5 Lect 4 Lexical Analysis Part 3.Pptx
SPCC Module 5 Lect 4 Lexical Analysis Part 3.Pptx
Module 5
LEX
C
Compiler lex.yy.c
Compiler
a.out
Input Sequence
Stream a.out
of
Tokens
%{
Structure Declaration
%}
of
%%
LEX
Rule Section
program
%%
Auxiliary Procedure
P1 {action1}
P2 {action2}
Structure
of ....
LEX Pi {action3}
program
Working of • It reads until it finds the longest prefix of the input that
matches one of the patterns
Parser • The lexical analyzer returns a single quantity i.e the token
to the parser
LT, LE, EQ, NE, GT, GE, IF, THEN, ELSE, ID, NUMBER, RELOP */
%}
/* Regular definitions */
/* Function to install the lexeme whose first character is pointed to by yytext, and whose length
is yyleng, into the symbol table and return a pointer to Symbol Table
*/ }
int installNum() {
• ./a.out
%}
%%
Example 1
void main()
{
yylex();
}
%%
[\n] {printf("\n Hi........%s......Good Morning\n",name); return 1;}
LEX %%
void main()
Program {
char opt;
do {
Example 2 printf("\nWhat is your name?"); scanf("%s",name);
yylex();
printf("\nPress y to continue"); scanf("%c",&opt);
}
while(opt=='y');
}
%}
%%
LEX . { charcount++; }
%%
yylex();
printf("# of lines = %d, # of chars = %d", linecount, charcount);
%{
void display(int flag,char *t)
void display(int, char *);
{
int flag;
%} if(flag==1)
• Organization of Entries:
Analyzer
Number of parameters, address of
Function parameter list, type and length of
returned value