Lab8 10
Lab8 10
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: 7TC6 BATCH: C
Experiment 8
WALex Program to extract HTML tags from .html file.
p8tags.l
%{
#include<stdio.h>
%}
%%
\<[^>]*\>
{ printf("%s\n",yytext);
fprintf(yyout,"%s\n",yytext);
}
.|\n;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin = fopen("input8.html","r");
yyout = fopen("output8.txt","w");
yylex();
return 0;
}
Output:
Experiment 9
Write a C Program to compute FIRST Set of the given grammar
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void FIRST(char);
int count, n = 0;
char prodn[10][10], first[10];
int contains(char arr[], char ch, int len) {
// Check if character 'ch' is already in 'arr'
for (int i = 0; i < len; i++) {
if (arr[i] == ch) return 1;
}
return 0;
}
void main()
{ int i, choice;
char c, ch;
printf("How many productions? : ");
scanf("%d", &count);
printf("Enter %d productions (use epsilon as $ and | for alternatives):\n\n", count);
for (i = 0; i < count; i++) {
scanf("%s%c", prodn[i], &ch); // Input each production rule
}
do {
n = 0;
printf("Element: ");
scanf(" %c", &c); // Reading the non-terminal for which FIRST set is required
FIRST(c);
printf("\nFIRST(%c) = { ", c);
for (i = 0; i < n; i++) {
printf("%c ", first[i]);
}
printf("}\n");
printf("Press 1 to continue: ");
scanf("%d%c", &choice, &ch);
} while (choice == 1);
}
void FIRST(char c)
{ int j, k;
if (!isupper(c)) { // If terminal, add to FIRST
if (!contains(first, c, n)) { // Avoid duplicates first[n++]
= c;
}
return;
}
for (j = 0; j < count; j++) {
if (prodn[j][0] == c) { // If left-hand side of production matches
for (k = 2; prodn[j][k] != '\0'; k++) { // Start reading the right-hand side
if (prodn[j][k] == '|') continue; // Skip '|'
Output:
Experiment 10
Write a C Program to compute FOLLOW Set of the given grammar
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main()
{
int i,z;
char c,ch;
printf("Enter the no.of productions:");
scanf("%d",&n);
printf("Enter the productions(epsilon=$):\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do
{
m=0;
printf("Enter the element whose FOLLOW is to be found:");
scanf("%c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",f[i]);
printf(" }\n");
printf("Do you want to continue(0/1)?");
scanf("%d%c",&z,&ch);
}
while(z==1);
}
void follow(char c)
{
if(a[0][0]==c)f[m++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))f[m++]=c;
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))f[m++]=a[k][2];
else first(a[k][2]);
}
}
Output: