SPCC Viva
SPCC Viva
SPCC Viva
int vow_count=0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
FILE *fp;
char filename[50];
scanf("%s",filename);
fp=fopen(filename,"r");
yyin=fp;
yylex();
return 0;
}
Input:
#include<stdio.h>
int main()
int a,b,c;
scanf("%d %d",&a,&b);
c=a+b;
printf("Sum is %d",c);
return 0;
#include <stdlib.h>
#include <stdio.h>
int charCount=0;
int wordCount=0;
int lineCount=0;
%}
%%
\n {charCount++; lineCount++;}
. {charCount++;}
%%
int yywrap(void) {
return 1;
if (argc > 1)
FILE *file;
if (!file)
exit(1);
yyin = file;
yylex();
return 0;
Input:
#include<stdio.h>
int main()
int a,b,c;
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;
}
int yywrap(void) {
return 1;
}
int main()
{
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;
}