Compiler Design and Construction
Assignment no.:01
Q. Take input file as:
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
int PI;
printf("%d",PI);
printf("The area is %d",2*PI*3);
printf("The value of PI is %d",PI);
}
and read value of PI ,replace it and write in output file.
Program:
package lab1;
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args)throws Exception
{
String macro = "";
String macroValue = "";
Scanner fin=new Scanner(new File("test.txt"));
while(fin.hasNext())
{
String str = fin.nextLine();
String str1 [] = str.split(" ");
if(str1[0].equals("#define"))
{
macro = str1[1];
macroValue = str1[2];
}
}
fin.close();
System.out.println("macro = "+macro);
System.out.println("macro value = "+macroValue);
Scanner fread=new Scanner(new File("test.txt"));
PrintStream fwrite=new PrintStream(new File("test2.txt"));
int i,j;
while(fread.hasNext())
{
String str = fread.nextLine();
int len = macro.length();
for(i=0; i < str.length(); i++)
{
if(str.charAt(i) == ' " ' )
{
do
{
fwrite.print(str.charAt(i));
i++;
}while((str.charAt(i)!= ' " '));
fwrite.print(str.charAt(i));
}
else if (str.charAt(i) == macro.charAt(0) && str.charAt(0) != '#')
{
String temp = "";
for(j=i; j<i+len; j++)
{
temp = temp + str.charAt(j);
}
if(temp.equals(macro))
{
if(Character.isLetterOrDigit(str.charAt(i+temp.length()))|| Character.isLetterOrDigit(str.charAt(i-1)))
fwrite.print(macro);
else
fwrite.print(macroValue);
}
i = i + len-1;
}
else
{
fwrite.print(str.charAt(i));
}
}
fwrite.println();
}
}
}
Input:
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
int PI;
printf("%d",PI);
printf("The area is %d",2*PI*3);
printf("The value of PI is %d",PI);
}
Output:
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
int 3.14;
printf("%d",3.14);
printf("The area is %d",2*3.14*3);
printf("The value of PI is %d",3.14);
}
Lab assignments often require practical understanding, accurate observations, and proper documentation, which can be challenging when working with complex datasets or technical tools. Students need to focus on analysis, clear reporting, and correct methodology to achieve better results. For those dealing with analytical projects, getting guidance from data mining assignments can be helpful in understanding patterns, improving data interpretation, and presenting lab work in a more structured and professional way.
ReplyDelete