Professional Documents
Culture Documents
Compiler Lab: Name of The Department: Computer Science & Engg
Compiler Lab: Name of The Department: Computer Science & Engg
Compiler Lab: Name of The Department: Computer Science & Engg
Submitted By:
Experiment No. 1
Date of Experiment: Grade: ________ Faculty’s Signature: _________
There are number of enhanced version of vi exists. Some of them are nvi, vim, vile and elvis. In Linux, the vi
command is a link to one of these programs. Most likely it will be vim. Most of the Linux distributions have vi
and emacs text editor already installed.
In Linux, configuration files are written in plain English. Hence, text editors are required to make changes to
these files. The editing capabilities can be invoked within vi by pressing the colon (:), entering ex command and
then pressing the Return key. ex commands are still important part of vi and is sometimes called hex in Linux.
Learning vi or emacs is must for every Linux or Unix user. There are some GUI based text editors available.
But, there are some situations when you can’t access GUI desktop. In that case, vi or emacs can only save you.
The version which people use normally now a days is Vi iMproved (VIM). Vi was enhanced by Bram
Moolenaar and are called vim editor.
The vi editor has three modes, command mode, insert mode and command line mode.
1.Command mode: by default, the vi editor opens in command mode. You can use commands to move cursor,
delete. cut, copy, paste and save changes. Commands are case sensitive. The ESC key is used to end a
command.
2. Insert mode: Insert mode is what we work in most of the time and text is inserted into the file in this mode
only. You can enter the insert mode by pressing “I” key. The newer vi versions will show “INSERT” word on
the bottom line. You can also enter "a" (insert after), "A" (insert at end of line), "o" (open new line after current
line) or "O" (Open line above current line) commands apart from “I” command. The ESC key ends insert mode
and returns you to command mode.
3.Command line mode: You can enter this mode by typing ":" which displays the command line entry at the
bottom of the screen.
Note: In broad sense, there are only two modes, one for editing text (i.e. Insert mode) and the other (i.e.
command mode) for giving commands. To switch between the two modes you use the “I” and “Esc” keys.
Starting vi
1. To start Vi, open a terminal or console and simply type vi filename (for existing file) or vi newfile (newfile
will be name of your new file)
3. Type “I” to go to Insert mode and add your text in the file.
4. Once you are done editing the file, Press “ESC” to go to command mode and save your changes to the file.
Press “Shift+ZZ” to save the file. Alternatively, you can use command line mode to issue command to save the
file.
Swap file:
Swap files has .swp extension. Swap file is automatically created when you make mistake in saving file.
Mistake like issuing wrong command CNTRL+ZZ instead of SHIFT+ZZ. It is similar to temp files in Windows
or DOS. .swp file will be in addition to your original file. In this case, the original file will not contain the recent
changes.
Command to recover from swap file: Type the following command at the $ prompt and press ENTER
vi -r {file name}
You must delete the swap file after recovery: Type following command at the $ prompt and press ENTER
rm .{file name}.swp
Moving the cursor: This command only works when you are in command mode. The cursor is controlled with
four keys: h, j, k, l.
k – one line up
Deleting characters:
1. X : will delete the character before the cursor
2. 4x : This will remove four characters
Deleting Words: Make sure to position the cursor to the first letter of word
1. dw : it will delete the word and space follwing it.
2. 3dw : will delete three words
Replacing word: move the cursor to the start of the word you want to replace
1. cw : vi will change itself to Inster mode and the last letter of the word to be replaced will turn into a $.
2. 3cw : will replace three words
3. Press “ESC” to get back to command mode.
Replacing Lines: This will change text from the cursor position to the eend of the line.
1. Type C
2. Type the replacement text
3. Press “ESC”
Inserting Text:
1. Position the cursor where you want the new text to appear.
2. Type “i”
3. Now you can start entering text
4. Press “ESC” to go back
Navigating a file:
Search Text:
Experiment No. 2
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
Experiment No. 3
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
%{
int count1=0;
int count2=0;
%}
%%
"a"I"e"I"i"I"o"I"u" {printf("%s is vowel",yytext);count1++;}
[a-zA-Z] {printf("%s is constonant",yytext);count2++;}
.I\n {ECHO;}
%%
main()
{
yylex();
printf("%d Vowel =%d constonant=",count1,count2);
}
Experiment No. 4
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
%{
%}
%%
isI
areI
amI printf("%s is verb",yytext );
[a-zA-Z]+ {printf("%s is not a verb",yytext);}
.I\n {ECHO;}
%%
main()
{
yylex();
}
Experiment No. 5
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
%{
int nchar=0,nword=0,nline=0;
#include<stdio.h>
%}
%%
\n {nline++;nchar++;}
[^ \t\n]+ {nword++;nchar+=yyleng;}
. {nchar++;}
%%
int main()
{
yylex();
printf("%d%d%d",nchar,nword,nline);
}
Experiment No. 6
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
Program in “C” to implement DFA.
#include<stdio.h>
#include<conio.h>
void main()
{
int state[10];
int str[10],input[10];
char ch;
int x[20];
int s,n,k=0,j,a,i,l,t,q=0,fs,b,nxt;
clrscr();
printf("enter the no. states\n");
scanf("%d",&s);
printf("enter the no.of i/ps\n");
scanf("%d",&n);
for(i=0;i<s;i++)
{
printf("enter the state\n");
scanf("%d",&state[i]);
printf("is it final state?... .y..1/n..0\n");
scanf("%d",&a);
if(a==1)
fs=state[i];
}
printf("enter the i/ps\n");
for(i=0;i<n;i++)
scanf("%d",&input[i]);
printf("transition state\n");
for(i=0;i<s;i++)
{
for(j=0;j<n;j++)
{
printf("(q%d,%d)=q",state[i],input[j]);
scanf("%d",&b);
x[k]=b; k++;
}
}
printf("enter the length of string\n");
scanf("%d",&l);
printf("enter the i/p string\n");
for(i=0;i<l;i++)
scanf("%d",&str[i]);
for(i=0;i<l;i++)
{
t=0;
do
{
if(str[i]==input[t])
{
nxt=x[n*q+t];
for(j=0;j<s;j++)
{
if(nxt==state[j])
q=j;
}
t++;
}
else
t++;
}
while(t!=n);
}
if(nxt==fs)
printf("\n string is accepted\n");
else
printf("\n not accepted\n");
getch();
}
Experiment No. 7
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
#include<stdio.h>
#include<conio.h>
int check(char* s,int state);
int a[10][10][10], fs[10],n;
int main()
{
int initial,i,j,k,l;
char ch,str[25];
clrscr();
printf("Enter no.of states \n");
scanf("%d",&n);
printf("enter initial state \n");
scanf("%d",&initial);
printf("Enter final states \n");
for(i=0;i<n;i++)
{
printf("\nq");
scanf("%d",&fs[i]);
if(fs[i]<0)
break;
}
for(i=0;i<=n;i++)
for(j=0;j<2;j++)
for(k=0;k<n;k++)
{
printf("(q%d,%d)=q",i,j);
scanf("%d",&a[i][j][k]);
if(a[i][j][k]<0)
break;
}do {printf("enter the string\n");
scanf("%s",str);
//r(l=0;l<strlen(str);l++)
if(check(str,initial))
printf("string is accepted");
else
printf("not accepted");
printf("\n\n input another (y/n)");
scanf("%s",&ch);
}
while(ch!='n');
return 0; //tch();
}
int check(char* s,int state)
{
int i,j;
for(i=0;i<n;i++)
{
if(*s=='\0')
{
if(fs[i]<0)
break;
if(fs[i]==state)
return 1;
}else{j=a[state][(int)(*s-'0')][i];
if(j<0)
break;
if(check(s+1,j))
return 1;
}}
return 0;
}
Experiment No. 8
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
Program to count the number of identifiers in a c file
Program:
%{
#include<stdio.h>
int id=0, i;
%}
%%
"int " ||
"float " ||
"double " ||
"char " ||
"bool " {
i = 0;
while (yytext[i] != ';')
{
if(yytext[i] == ',')
id++;
i++;
}
id++;
}
. ;
%%
int main(int argc, char *argv[])
{ yyin = fopen(argv[1],"r");
yylex();
printf("No. of identifiers = %d\n",id);
}
Experiment No. 10
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective
Program in “LEX” to count number of Identifiers and Keywords.
digit[0-9]
letter[A-Za-z]
%{
int count1=0;
int count2=0;
%}
%%
intI
floatI
char {count++;printf("%s is keyword",yytext);}
{letter}I({letter}I{digit})*{count2++;}
[a-zA-Z]+ {printf("%s is not keyword",yytext);}
.I\n {ECHO;}
%%
main()
{
yylex();
printf("%d no of keyword %d no of id",count1,count2);
}