SPCC Viva

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1.

Implementation of Lexical Analyzer in C / Java / Python


2.Implement Lexical Analyzer using FLEX Count no of
Vowels & Consonants
%{

int vow_count=0;

int const_count =0;

%}

%%

[aeiouAEIOU] {vow_count++;}

[a-zA-Z] {const_count++;}

%%

int yywrap(){}

int main()

FILE *fp;

char filename[50];

printf("Enter the file name: \n");

scanf("%s",filename);

fp=fopen(filename,"r");

yyin=fp;

yylex();

printf("Number of vowels are: %d\n", vow_count);

printf("Number of consonants are: %d\n", const_count);

return 0;

}
Input:

#include<stdio.h>

int main()

int a,b,c;

printf("Enter two numbers");

scanf("%d %d",&a,&b);

c=a+b;

printf("Sum is %d",c);

return 0;

3. Implement Lexical Analyzer using FLEX Count no of


Words, characters & lines
%{

#include <stdlib.h>

#include <stdio.h>
int charCount=0;

int wordCount=0;

int lineCount=0;

%}

%%

\n {charCount++; lineCount++;}

[^ \t\n]+ {wordCount++; charCount+=yyleng;}

. {charCount++;}

%%

int yywrap(void) {

return 1;

int main(int argc, char** argv)

if (argc > 1)

FILE *file;

file = fopen(argv[1], "r");

if (!file)

fprintf(stderr, "Could not open %s\n", argv[1]);

exit(1);

yyin = file;

yylex();

printf("No: of Chararacter : %d \n", charCount);


printf("No: of Words : %d \n", wordCount);

printf("No: of Lines : %d \n", lineCount);

return 0;

Input:

#include<stdio.h>

int main()

int a,b,c;

printf("Enter two numbers");

scanf("%d %d",&a,&b);

c=a+b;

printf("Sum is %d",c);

return 0;

}
4. Implement Lexical Analyzer using FLEX Count no of
keywords,identifiers & operators
/*lex code to count the number of lines,
tabs and spaces used in the input*/
%{
#include<stdio.h>
int ids = 0;
int rel = 0;
int keywords = 0;
%}
DIGIT [0-9]
LETTER [A-Za-z]
TYPE int|char|bool|float|void|for|do|while|if|else|return|void
%option yylineno
%option noyywrap
%%
{TYPE} {keywords++;}
(<|>|<=|>=|==|=) {rel++;}
'#'/[a-zA-Z0-9]* {;}
[a-zA-Z]+[a-zA-Z0-9]* {ids++;}
. {;}
%%
void main(int argc, char **argv)
{
FILE *fh;
if (argc == 2 && (fh = fopen("input3.c", "r"))) {
yyin = fh;
}
yylex();
printf("identifiers = %d, operators = %d, keywords = %d \n",ids,rel,keywords);
}

Input:
#include<stdio.h>
void main(){
float a123;
char a;
char b123;
char c;
int ab[5];
int bc[2];
int ca[7];
int ds[4];
for( a = 0; a < 5 ;a++)
printf("%d ", a);
return 0;
}

5.Implement Lexical Analyzer using FLEX Identify Even


& odd integers
%{
#include<stdio.h>
int i;
%}
%%
[0-9]+ {i=atoi(yytext);
if(i%2==0)
printf("Even");
else
printf("Odd");}
%%

int yywrap(void) {
return 1;
}

int main()
{

printf("Enter the number:" );


yylex();
return 0;
}

6.Implement Lexical Analyzer using FLEX Count of


printf & scanf statements in C program\
%{
#include<stdio.h>
int pf=0,sf=0;
%}
%%
scanf {sf++;}
printf {pf++;}
%%
int yywrap(void) {
return 1;
}
int main(int argc, char* argv[])
{
if (argc > 1)
{
FILE *file;
file = fopen("minal.c", "r");
if (!file)
{
fprintf(stderr, "Could not open %s\n", argv[1]);
exit(1);
}
yyin = file;
}yylex();
printf("no of printf are : %d\n", pf);
printf("no of scanf are : %d\n", sf);
return 0;
}

Input:
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter two numbers");
scanf("%d %d",&a,&b);
c=a+b;
printf("Sum is %d",c);
return 0;
}

7. Write a program to find FIRST & FOLLOW Symbols


for the given grammar.

You might also like