Compiler Design Lab2 Report
Compiler Design Lab2 Report
Lab Report : 02
Course Title : Compiler Design Lab
Course Code : CSE-324
Submitted By Submitted To
Name : MD ABU RAYHAN Name : Md. Saddam Hossain.
ID : 22235103446 Assistant Professor
Intake : 51 Department of CSE BUBT
Section : 10
Department of CSE
……………………………………….
Problem no 01 : Write a program that reads lines of code from the console and identifies:
• Single-line comments (//)
• Multi-line comments (/* ... */)
• Lines with no comments
Source Code :
#include<bits/stdc++.h>
using namespace std;
void multiline(){
string s;
while (getline(cin,s))
{
for(int i=0;i<s.size()-1;i++){
if(s[i]=='*' && s[i+1]=='/'){
cout<<"Multiline Comment"<<endl;
return;
}
}
}
}
int main(){
string s;
while (1)
{
getline(cin,s);
int cnt=0;
for(int i=0;i<s.size()-1;i++){
if(s[i]=='/' && s[i+1]=='/'){
cout<<"Single Line Comment"<<endl;
cnt=1;
break;
}
else if(s[i]=='/' && s[i+1]=='*'){
multiline();
cnt=1;
break;
}
}
if(cnt==0) cout<<"No Comments"<<endl;
}
Output :
Conclusion : In this problem, we’ve learned how to identify whether a line is a single-line comment, a multi-
line comment, or has no comments.
Problem no 02 : Write a program that reads code from a input text le, identi es and counts single-line and
multi-line comments, and writes the results to a output text le.
Source Code :
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream n("input.txt");
ofstream fout("output.txt");
string s;
int single_count = 0, multi_count = 0;
fout << "Single Line Comments: " << single_count << endl;
fout << "Multi Line Comments: " << multi_count << endl;
n.close();
fout.close();
return 0;
}
Input : Output:
Conclusion : In this problem, we’ve learned how to read code from an input text file, identify and count
single-line and multi-line comments, and write the results to an output text file.
fi
fi
fi
fi
fi
fi