0% found this document useful (0 votes)
22 views5 pages

Microsoft Word - Lab - Compiler

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Microsoft Word - Lab - Compiler

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 1

Design a lexical analyzer for given language and the lexical analyzer should ignore
redundant spaces, tabs and new lines. It should also ignore comments. Although the
syntax specification states that identifiers can be arbitrarily long, you may restrict the
length to some reasonable value. Simulate the same in C language.

Source code:

#include<bits/stdc++.h>
using namespace std;

int main(){
ifstream in("in.c");
if(!in){
cout<<"File cannot be opened";
return 1;
}
char ch;
while(in.get(ch)){
if(ch == '>'){
cout << ch << "\n";
ch = in.get();
if(ch == ' ' || ch == '\t' || ch == '\n')
goto nl;
else
cout << ch;
}
else if(ch == '('){
cout << ch;
ch = in.get();
while(ch != ')'){
cout << ch;
ch = in.get();
}
cout << ch;

ch = in.get();
if(ch == ' '){
ch = in.get();
while(ch == ' ' || ch == '\t' || ch == '\n'){
ch = in.get();
continue;
}
goto x;
}
else
x: ch == ';' ? cout << ";\n" : cout << "\n";
}
else if(ch == ' ' || ch == '\t' || ch == '\n'){
if(ch == ' '){
cout << ch;
nl:
ch = in.get();
while(ch == ' ' || ch == '\t' || ch == '\n'){
ch=in.get();
continue;
}
cout << ch;
}
}
else if(ch == ';'){
cout << ch << "\n";
ch = in.get();
if(ch == ' ' || ch == '\t' || ch == '\n')
goto nl;
else
cout<<ch;
}
else
cout<<ch;
}
in.close();

}
in.c :

#include<stdio.h>void main(){int a,b;printf("Hello


World");for(i=0;i<5;i++)printf("i");}
Output:

#include<stdio.h>
void main()
int a,b;
printf("Hello World");
for(i=0;i<5;i++)
printf("i");
}
Practical 2
Write a C program to identify whether a given line is a comment or not.

Source code:

//program to find comments


#include<iostream>
#include<cstring>
using namespace std;

int main(){
int arr[100];
char input[1000];
cout << "Enter your string ctrl + z to terminate \n";
scanf("%[^z]", input);

int length = strlen(input);


bool temp = false, alreadyCommentS = false, alreadyCommentM = false;
int lengthCounter = 0, commentCounter = 0;

for(int i = 0; i < length; ++i){


if(input[i] == '"')
temp = !temp;
if(input[i] == '\n'){
lengthCounter++;
alreadyCommentS = false;
}
if(!temp){
if(!alreadyCommentS && !alreadyCommentM){
if(input[i] == '/' && input[i + 1] == '/'){
arr[commentCounter] = lengthCounter;
commentCounter++;
alreadyCommentS = true;
}
if(input[i] == '/' && input[i + 1] == '*'){
arr[commentCounter] = lengthCounter;
commentCounter++;
alreadyCommentM = true;
}
}
if(input[i] == '*' && input[i + 1] == '/' &&
alreadyCommentM)
alreadyCommentM = false;
}
}
cout << "Number of lines in above lines are " << lengthCounter
<<'\n';
cout << "Number of comments in above lines are " << commentCounter
<<' '
<< "and they are in following lines" <<'\n';
for(int i = 0; i < commentCounter; ++i)
cout << arr[i] + 1 << '\n';
return 0;
}

Output:
Enter your string ctrl + z to terminate
//hi there /*this is comment program*/
/*this is //another comment */
^Z
Number of lines in above lines are 2
Number of comments in above lines are 2 and they are in following lines
1
2
Practical 3
Write a C program to test whether a given identifier is valid or not.

Source code:

//program to find identifier is valid or not


#include<iostream>
#include<cstring>
using namespace std;

int main(){
char keyword[32][15] = {"int", "float", "char", "bool", "void",
"double", "long", "case", "using", "sizeof",
"const", "enum", "extern", "namespace", "for", "while", "do",
"switch", "if", "else", "return", "default",
"typedef", "short", "union", "goto", "volatile", "unsigned",
"static", "register", "struct", "break"};

char input[100];
cout << "Enter a Identifier & press enter: ";
scanf("%[^\n]", input);

int length = strlen(input);

for(int i = 0; i < 32; ++i){


if(!strcmp(keyword[i], input)){
cout << "identifier cannot be a keyword\n";
return 0;
}
}

if(input[0] == '_' || (input[0] >= 'a' && input[0] <='z') ||


(input[0] >= 'A' && input[0] <='Z')){
for(int i = 0; i < length; ++i){
if(!(input[i] == '_' || (input[i] >= 'a' && input[i]
<='z') || (input[i] >= 'A' && input[i] <='Z')
|| (input[i] >= '0' && input[i] <='9'))){
cout << "unexpected character in identifier at "
<<i + 1 <<" position";
return 0;
}
}
cout <<"identifier accepted\n";
}
else
cout <<"invalid identifier as it is not starting with
alphabets or _\n";

return 0;
}

You might also like