Sharecodepoint App Download Qrcode Sharecodepoint Android App Banner
Now You can search anything in sharecodepoint using your voice. Click on mic button. Sharecodepoint Voice Sharecodepoint Youtube Channel



Sharecodepoint popular queries

Saturday, 23 February 2019

Program to count the number of vowels and consonants in a given string | Compiler Design | Lex Programs

Compiler Design Lap Program :  %{  #include<stdio.h>  int vowels=0;  int cons=0; %} %% [aeiouAEIOU] {vowels++;} ...

Compiler Design Lap Program : 

%{
 #include<stdio.h>
 int vowels=0;
 int cons=0;
%}
%%
[aeiouAEIOU] {vowels++;}
[a-zA-Z] {cons++;}
%%
int yywrap()
{
return 1;
}
main()
{
 printf(“Enter the string.. at end press ^d\n”);
 yylex();
 printf(“No of vowels=%d\nNo of consonants=%d\n”,vowels,cons);

Program to count the number of characters, words, spaces, end of lines in a given input file. | Compiler Design Programs | Lex Programs

Compiler Design Lab Program :  %{  #include<stdio.h>  Int c=0, w=0, s=0, l=0; %} WORD [^ \t\n,\.:]+ EOL [\n] BLA...

Compiler Design Lab Program : 

%{
 #include<stdio.h>
 Int c=0, w=0, s=0, l=0;
%}
WORD [^ \t\n,\.:]+
EOL [\n]
BLANK [ ]
%%
{WORD} {w++; c=c+yyleng;}
{BLANK} {s++;}
{EOL} {l++;}
. {c++;}
%%
int yywrap()
{
return 1;
}
main(int argc, char *argv[])
{
 If(argc!=2)
 {
 printf(“Usage: <./a.out> <sourcefile>\n”);
 exit(0);
 }
 yyin=fopen(argv[1],”r”);
yylex();
printf(“No of characters=%d\nNo of words=%d\nNo of
spaces=%d\n No of lines=%d”,c,w,s,l);

Program to count no of: a) +ve and –ve integers b) +ve and –ve fractions | Compiler Design Programs | Lex Programs

Compiler Design Lap Program : %{  #include<stdio.h>  int posint=0, negint=0,posfraction=0, negfraction=0; %} %% [-...

Compiler Design Lap Program :

%{
 #include<stdio.h>
 int posint=0, negint=0,posfraction=0, negfraction=0;
%}
%%
[-][0-9]+ {negint++;}
[+]?[0-9]+ {posint++;}
[+]?[0-9]*\.[0-9]+ {posfraction++;}
[-][0-9]* \.[0-9]+ {negfraction++;}
%%
int yywrap()
{
return 1;
}
main(int argc, char *argv[])
{
 If(argc!=2)
 {
 printf(“Usage: <./a.out> <sourcefile>\n”);
 exit(0);
 }
 yyin=fopen(argv[1],”r”);
yylex();
printf(“No of +ve integers=%d\n No of –ve integers=%d\n No of
+ve
fractions=%d\n No of –ve fractions=%d\n”, posint, negint,
posfraction, negfraction);
}

Program to count the no of comment line in a given C program. Also eliminate them and copy that program into separate file . | Compiler Design Lab Programs | Lex Programs

Compiler Design Lap Program : %{  #include<stdio.h>  int com=0; %} %s COMMENT %% “/*”[.]*”*/” {com++;} “/*” {B...

Compiler Design Lap Program :

%{
 #include<stdio.h>
 int com=0;
%}
%s COMMENT
%%
“/*”[.]*”*/” {com++;}
“/*” {BEGIN COMMENT ;}
<COMMENT>”*/” {BEGIN 0; com++ ;}
<COMMENT>\n {com++ ;}
<COMMENT>. {;}
.|\n {fprintf(yyout,”%s”,yytext);
%%
int yywrap()
{
return 1;
}
main(int argc, char *argv[])
{
 If(argc!=2)
 {
 printf(“Usage: <./a.out> <sourcefile> <destn file>\n”);
 exit(0);
 }
 yyin=fopen(argv[1],”r”);
 yyout=fopen(argv[2],”w”);
 yylex();
 printf(“No of comment lines=%d\n”,com);

Program to count the no of ‘scanf’ and ‘printf’ statements in a C program. Replace them with ‘readf’ and ‘writef’ statements respectively. | Compiler Design Programs | Lex Programs

Compiler Design Program Lab : %{ #include<stdio.h> int pc=0, sc=0; %} %% “printf” { fprintf(yyout,”writef”); pc++;...

Compiler Design Program Lab :

%{
#include<stdio.h>
int pc=0, sc=0;
%}
%%
“printf” { fprintf(yyout,”writef”); pc++;}
“scanf” { fprintf(yyout,”readf”); sc++;}
%%
int yywrap()
{
return 1;
}
main(int argc, char *argv[])
{
 if(argc!=2)
 {
 printf(“Usage: <./a.out> <sourcefile> <destn file>\n”);
 exit(0);
 }
 yyin=fopen(argv[1],”r”);
 yyout=fopen(argv[2],”w”);
 yylex();
 printf(“No of printf statements = %d\n No of scanf
statements=%d\n”, pc, sc);

Program to recognize a valid arithmetic expression and identify the identifiers and operators present. Print them separately. | Compiler Design Lab Programs | Lex Programs

Compiler Design Program : %{  #include<stdio.h>  #include<string.h>  int noprt=0, nopnd=0, valid=1, top=-1, m, l...

Compiler Design Program :

%{
 #include<stdio.h>
 #include<string.h>
 int noprt=0, nopnd=0, valid=1, top=-1, m, l=0, j=0;
 char opnd[10][10], oprt[10][10], a[100];
%}
%%
“(“ { top++; a[top]=’(‘ ; }
“{“ { top++; a[top]=’{‘ ; }
“[“ { top++; a[top]=’[‘ ; }
“)” { if(a[top]!=’(‘)
 {
 valid=0; return;
}
else
 top--;
 }
“}” { if(a[top]!=’{‘)
 {
 valid=0; return;
}
else
 top--;
 }
“]” { if(a[top]!=’[‘)
 {
 valid=0; return;
}
else
 top--;
 }
“+”|”-“|”*”|”/” { noprt++;
 strcpy(oprt[l], yytext);
l++;
 }
[0-9]+|[a-zA-Z][a-zA-Z0-9_]* {nopnd++; 
strcpy(opnd[j],yytext);
 j++;
 }
%%
int yywrap()
{
return 1;
}
main()
{
 int k;
 printf(“Enter the expression.. at end press ^d\n”);
 yylex();
 if(valid==1 && i==-1 && (nopnd-noprt)==1)
 {
 printf(“The expression is valid\n”);
 printf(“The operators are\n”);
 for(k=0;k<l;k++)
 Printf(“%s\n”,oprt[k]);
 for(k=0;k<l;k++)
 Printf(“%s\n”,opnd[k]);
 }
 else
 Printf(“The expression is invalid”);
}

Program to recognize whether a given sentence is simple or compound. | Compiler Design Lab Programs | Lex Programs

Compiler Design Lab Program : %{  #include<stdio.h>  Int is_simple=1; %} %% [ \t\n]+[aA][nN][dD][ \t\n]+ {is_simpl...

Compiler Design Lab Program :

%{
 #include<stdio.h>
 Int is_simple=1;
%}
%%
[ \t\n]+[aA][nN][dD][ \t\n]+ {is_simple=0;}
[ \t\n]+[oO][rR][ \t\n]+ {is_simple=0;}
[ \t\n]+[bB][uU][tT][ \t\n]+ {is_simple=0;}
. {;}
%%
int yywrap()
{
return 1;
}
main()
{
 int k;
 printf(“Enter the sentence.. at end press ^d”);
 yylex();
if(is_simple==1)
 {
 Printf(“The given sentence is simple”);
 }
else
 {
 Printf(“The given sentence is compound”);
 }
}

 

Version 23.2.3.4 - 2016 - 2019 - © Sharecodepoint | Made in India. | Copyright Content