Upload
rajijee
View
218
Download
0
Embed Size (px)
Citation preview
8/7/2019 Instructions for using vi
1/3
Instructions for using vi
1. Open a terminal window2. Typevi prg1a.l3. The vieditorisnow openin commandmode4. Press thei key5. Now editorisininsert mode6. Start typing theprogram7. To save theprogramswitch back to commandmod(Press esckey for this)8. Them type :wq9. Programissaved and youshould be back to the terminal window
To run a lex program
1. Type lexprg1a.l2. This will generate lex.yy.c3. Thendogcc lex.yy.c4. Now you have the a.out file5. Now run thisusing ./a.out test1.txt
Introduction to VI:The command tostart the VI editorisvi, followed by the filename. Forexample to
edit a file calledtemporary, you would typevi temporary and thenreturn.
The VI editor hastwomodes andinorder toget out of VI, you have to beincommandmode. Hit thekey
labeled "Escape" or "Esc" (If your terminal doesnot havesuch a key, then try ^[,or control-[.) toget into
commandmode. If you were already in the commandmode when you hit "Escape",don't worry. It
might beep, but you will still bein thecommandmode.
The command to quit out of VI is :q. Onceincommandmode, type colon, and'q', followed by return.
The command tosave the contentsof theeditoris :w. You can combine the above command with the
quit command,or :wq.
The first thingmost users learn about the VI editoris that it has twomodes: commandandinsert. The
commandmode allows theentry of commands tomanipulate text. Theinsertmodeputs anything typed
on thekeyboardinto the current file.
VI startsout incommandmode. There areseveral commands that put the VI editorintoinsertmode.
Themost commonly used commands toget intoinsert mode are a andi.
a enterinsertmode, the characters typedin will beinserted after the current cursorposition. If you
specify a count, all the text that had beeninserted will berepeated that many times.
i enterinsertmode, the characters typedin will beinserted before the current cursorposition. If you
specify a count, all the text that had beeninserted will berepeated that many times.
8/7/2019 Instructions for using vi
2/3
Prg 1b
%optionnoyywrap
%{
#include
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:");
return(0);
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("\nnoof comment linesis %d",c);
}
Prg 1a
%optionnoyywrap
%{
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");
8/7/2019 Instructions for using vi
3/3