Compiler Lab: Name of The Department: Computer Science & Engg

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Compiler Lab(ECS 653)

Sumitted To:Mr. Rahul Singh


(Assistant Professor)

Name of the Department:


Computer Science & Engg.

Submitted By:

INDEX(CS-A 3rd year/Group G1)


S. Name of Experiment Date Date Signature Remarks
No (Proposed) (Actual)

1. Introduction to Linux 28/01/11


operating system and working
on vi editor
2. Program in “C/Lex” to check 04/02/11
whether Identifier is valid or
not.
3. Program in “LEX” to count 11/02/11
number of vowels and
consonants.
4. Program in “LEX” to count the 18/02/11
verb in a given line of text.
5. Program in “LEX” to count 04/03/11
number of Characters, Words,
Lines.
6. Program in “C” to implement 11/03/11
DFA.
7. Program in “C” to implement 25/03/11
NFA.
8. Program in lex to count the 01/04/11
number of identifiers in a c file
9. Internal viva/quiz on compiler 08/04/11
design lab
10. Program in “LEX” to count 15/04/11
number of Identifiers and
Keywords.

Experiment No. 1
Date of Experiment: Grade: ________ Faculty’s Signature: _________

Introduction to vi editor in Linux and Unix system


The vi editor is the classic screen editing program for Unix systems and is based on a older line editor called ex.
It can be used from any type of terminal because it doesn’t depend on arrow and function keys. It uses standard
alphabetic keys for command as well as arrow keys.

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)

2. The file will open in command mode.

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.

h – one space left

j – one line down

k – one line up

l – one space right

Note: If you were in Insert mode, Press ESC to go to command mode.

Basic vi editing command


You must be in command mode to execute editing commands. Many of the editing commands are case
sensitive. They have different meaning depending on whether they are typed as lowercase or upper case. Also,
editing commands can be preceded by a number to indicate a repetition of the command.

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

Deleting Lines: Make sure the cursor is at the beginning of the line


1. dd : delete the entire line.
2. 2dd : delete two lines
3. D : To delete from the cursor postion to the end of the line.

Replacing Characters: This will replace one character with another


1. Position the cursor to the character to be replaced.
2. Then type r
3. Type the new character you want to replace with.
4. Now, the new character should appear and you should still be in the command mode.

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:

w : forward word by word


b : backward word by word
$ : to end of line
0 : to the beginning of line
H : to top line of screen
M : to middle line of screen
L : to last line of screen
G : to last line of file
1G : to first line of file
CTRL + f : scroll forward one screen
CTRL + b : Scroll backward one screen
CTRL + d : Scroll down one half screen
CTRL + u : Scroll up one half screen

Search Text:

While in command mode,


1. Type /
2. Enter the text you want search
3. Press “Enter” Key
4. The cursor moves to the first occurrence of the text you typed above after slash
5. Type n to search in forward direction and N to search in backward direction

Saving and Closing the file:

Save the file and quit vi: Shift + ZZ

Save only and leave the file open:


1. Press ESC
2. Type :w
3. Press “Enter”

Don’t save and quit vi:


1. Press ESC
2. Type :q!
3. Press “Enter”

Experiment No. 2
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective

Program in “C/Lex” to check whether Identifier is valid or not


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=0,j=0,flag=1;
char ch,ip[100];
while((ch=getchar())!='\n')
{
ip[i]=ch;
i++;
}
ip[i]='*';
if((ip[j]>='a'&&ip[j]<='z')||(ip[j]>='A'&&ip[j]<='Z'))
{
j++;
while(ip[j]!='*')
{
if((ip[j]>='a'&&ip[j]<='z')||(ip[j]>='A'&&ip[j]<='Z')||(ip[j]>='0'&&ip[j]<='9')||ip[j]=='_')
  {
j++;
  }
  else
  {
  flag=0;
  goto loop;
  }
  }
  }
  else
  flag=0;
  loop:
  if(flag==0)
  printf("invalid");
  else
  printf("valid");
getch();
 }

Experiment No. 3
Date of Experiment: Grade: ________ Faculty’s Signature: _________
Objective

Program in “LEX” to count number of vowels and consonants .

%{
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

Program in “LEX” to count the verb in a given line of text.

%{
%}
%%
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

Program in “LEX” to count number of Characters, Words, Lines.

%{
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

Program in “C” to implement NFA.

#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);
}

You might also like