Instructions for using vi
1. Open a terminal window
2. Type vi prg1a.l
3. The vi editor is now open in ‘command mode’
4. Press the i key
5. Now editor is in insert mode
6. Start typing the program
7. To save the program switch back to command mod(Press ‘esc’ key for this)
8. Them type “:wq”
9. Program is saved and you should be back to the terminal window
To run a lex program
1. Type “lex prg1a.l”
2. This will generate lex.yy.c
3. Then do gcc lex.yy.c
4. Now you have the a.out file
5. Now run this using ./a.out test1.txt
Introduction to VI:The command to start the VI editor is vi, followed by the filename. For example to
edit a file called temporary, you would type vi temporary and then return.
The VI editor has two modes and in order to get out of VI, you have to be in command mode. Hit the key
labeled "Escape" or "Esc" (If your terminal does not have such a key, then try ^[, or control-[.) to get into
command mode. If you were already in the command mode when you hit "Escape", don't worry. It
might beep, but you will still be in the command mode.
The command to quit out of VI is :q. Once in command mode, type colon, and 'q', followed by return.
The command to save the contents of the editor is :w. You can combine the above command with the
quit command, or :wq.
The first thing most users learn about the VI editor is that it has two modes: command and insert. The
command mode allows the entry of commands to manipulate text. The insert mode puts anything typed
on the keyboard into the current file.
VI starts out in command mode. There are several commands that put the VI editor into insert mode.
The most commonly used commands to get into insert mode are a and i.
a enter insert mode, the characters typed in will be inserted after the current cursor position. If you
specify a count, all the text that had been inserted will be repeated that many times.
i enter insert mode, the characters typed in will be inserted before the current cursor position. If you
specify a count, all the text that had been inserted will be repeated that many times.
Prg 1b
%option noyywrap
%{
#include<stdio.h>
int c=0;
%}
%%
("/*"[ a-zA-Z0-9' '\n\t.]*"*/") c++;
("//"[\n]?[ a-zA-Z0-9]*) c++;
[a-zA-Z0-9(){}.\,#<>]* {fprintf(yyout,"%s",yytext);}
%%
main(int argc,char *argv[])
{
if(argc!=3)
{
printf("\nusage:<./a.out><source><destination>");
return(0);
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("\n no of comment lines is %d",c);
}
Prg 1a
%option noyywrap
%{
unsigned wordCount=0,charCount=0,lineCount=0,spaceCount=0;
%}
word [^ \t\n]+
line [\n]
space [" "]
tab [\t]
%%
{word} {wordCount++;
charCount+=yyleng;}
{line} {lineCount=lineCount+1;}
{space} {spaceCount=spaceCount+1;}
{tab} {spaceCount=spaceCount+5;}
%%
main(argc,argv)
int argc;
char **argv;
{if (argc>1){
FILE *file;
file=fopen(argv[1],"r");
if(!file){fprintf(stderr,"could not open %s\n",argv[1]);
exit(1);
}
yyin=file;
}
yylex();
printf("WORD COUNT IS %d \n",wordCount);
printf("CHARACTER COUNT IS %d \n",charCount);
printf("LINE COUNT IS %d \n",lineCount);
printf("SPACE COUNT IS %d \n",spaceCount);
return 0;
}