Assignment 1
Practical No. 1-2
Statement: Write a program to implement the computation of FIRST and
FOLLOW of a generalized grammar.
System.out.println("Enter grammer");
String s[] = new String[numberOfGrammer];
//for storing all the input statements
for (int i = 0; i < numberOfGrammer; i++) {
//input array s[]
s[i] = br.readLine();
}
//make array t[] to store first 4 chars of each s[]
for (String item : s) {
al.add(item.substring(0, 4));
}
//calculate and store all productions divided by / in
arraylist al
for (String item : s) {
if (item.contains("/")) {
int j = item.indexOf('/');
String fi = item.substring(0, 3) + item.substring(j
+ 1, j + 2);
al.add(fi);
}
}
//if non terminal then get its terminal or else add it in
the final array
for (Object al1 : al) {
String a = (String) al1;
if (Character.isUpperCase(a.charAt(3))) {
f.addAll(getTerminal(a));
} else {
f.add(a);
}
}
//System.out.println(al);
System.out.println("Final First List : " + filter(f));
}
Date: -
Assignment 2
Practical No. 2
Statement: Write a program to count number of characters, words and lines
from an input file.
Code: %{
#include<stdio.h>
int cc=0,bc=0,wc=0,lc=0;
char c;
%}
%%
[^ \t\n]+
{
wc++;
cc=cc+yyleng;
}
\n
lc++;
" "
bc++;
\t
bc=bc+5;
%%
main(int argc,char *argv[])
{
if (argc!=2)
printf("\nusage:./a.out filename\n");
return(0);
}
yyin=fopen(argv[1],"r");
yylex();
printf("\n no of lines are %d\n",lc);
printf("\n no of words are %d\n",wc);
printf("\n no of blanks are %d\n",bc);
printf("\n no of character are %d\n",cc);
}
int yywrap(){
return 1;
}
Date: -
Assignment 2
Practical No. 3
Statement: Write a program finding number of occurrence of a character in a
given word.
Code: %{
#include<stdio.h>
int cc=0,bc=0;
char c;
%}
%%
[^ \t\n]+
{if(cc=='a'){
cc=cc+1;}
}
%%
main(int argc,char *argv[])
{
if (argc!=2)
printf("\nusage:./a.out filename\n");
return(0);
}
yyin=fopen(argv[1],"r");
yylex();
printf("\n no of occurence of character are %d\n",cc);
}
int yywrap(){return 1;}