Sunteți pe pagina 1din 58

SYSTEM PROGRAMING lab

Name: Jamuna M
Reg.no: 16MIS1129
Slot: D1+TD1
Topic: Interactive Text Editor
Faculty: BVANSS Prabhakar Rao
Semester: Fall 19-20
Date: 01/11/2019

1. SYMBOL TABLE PREPARATION:


Symbol table is an important data structure created and maintained by compilers in order to
store information about the occurrence of various entities such as variable names, function
names, objects, classes, interfaces, etc. Symbol table is used by both the analysis and the
synthesis parts of a compiler.
ALGORITHM:

Step1: Start the program for performing insert, display, delete, search and modify option in
symbol table
Step2: Define the structure of the Symbol Table
Step3: Enter the choice for performing the operations in the symbol Table
Step4: If the entered choice is 1, search the symbol table for the symbol to be inserted. If the
symbol is
already present, it displays “Duplicate Symbol”. Else, insert the symbol and the corresponding
address in
the symbol table.
Step5: If the entered choice is 2, the symbols present in the symbol table are displayed.
Step6: If the entered choice is 3, the symbol to be deleted is searched in the symbol table.
Step7: If it is not found in the symbol table it displays “Label Not found”. Else, the symbol is
deleted.
Step8: If the entered choice is 5, the symbol to be modified is searched in the symbol table.
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void main()
{
int i=0,j=0,x=0,n;
void *p,*add[5];
char ch,srch,b[15],d[15],c;
printf("Expression terminated by $:");
while((c=getchar())!='$')
{
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf("\n Symbol Table\n");
printf("Symbol \t addr \t type");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("\n%c \t %d \t identifier\n",c,p);
x++;
j++;
}
else
{
ch=c;
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
{
p=malloc(ch);
add[x]=p;
d[x]=ch;
printf("\n %c \t %d \t operator\n",ch,p);
x++;
j++;
}}}}
OUTPUT:
2) IMPLEMENTATION OF PASS 1 ASSEMBLER:
Assembler is a program for converting instructions written in low-level assembly
code into relocatable machine code and generating along information for the
loader.It generates instructions by evaluating the mnemonics (symbols) in
operation field and find the value of symbol and literals to produce machine code.
Now, if assembler do all this work in one scan then it is called single pass
assembler, otherwise if it does in multiple scans then called multiple pass
assembler.

C CODE:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *f1,*f2,*f3,*f4,*f5;
int lc,sa,i=0,j=0,m[10],pgmlen,len,k,len1,l=0;
char name[10],opnd[10],la[10],mne[10],s1[10],mne1[10],opnd1[10];
char lcs[10],ms[10];
char sym[10],symaddr[10],obj1[10],obj2[10],s2[10],q[10],s3[10];

f1=fopen("input.txt","r");
f2=fopen("optab.txt","r");
f3=fopen("symtab.txt","w+");
f4=fopen("symtab1.txt","w+");
f5=fopen("output.txt","w+");
fscanf(f1,"%s%s%s",la,mne,opnd);
if(strcmp(mne,"START")==0)
{
sa=atoi(opnd);
strcpy(name,la);
lc=sa;
}
strcpy(s1,"*");
fscanf(f1,"%s%s%s",la,mne,opnd);
while(strcmp(mne,"END")!=0)
{
if(strcmp(la,"-")==0)
{
fscanf(f2,"%s%s",mne1,opnd1);
while(!feof(f2))
{
if(strcmp(mne1,mne)==0)
{
m[i]=lc+1;
fprintf(f3,"%s\t%s\n",opnd,s1);
fprintf(f5,"%s\t0000\n",opnd1);
lc=lc+3;
i=i+1;
break;
}
else
fscanf(f2,"%s%s",mne1,opnd1);
}

else
{
fseek(f3,SEEK_SET,0);
fscanf(f3,"%s%s",sym,symaddr);
while(!feof(f3))
{
if(strcmp(sym,la)==0)
{
itoa(lc,lcs,10);
fprintf(f4,"%s\t%s\n",la,lcs);
itoa(m[j],ms,10);
j=j+1;
fprintf(f5,"%s\t%s\n",ms,lcs);
i=i+1;
break;
}
else
fscanf(f3,"%s%s",sym,symaddr);
} //f3
if(strcmp(mne,"RESW")==0)
lc=lc+3*atoi(opnd);
else if(strcmp(mne,"BYTE")==0)
{
strcpy(s2,"-");
len=strlen(opnd);
lc=lc+len-2;
for(k=2;k<len;k++)
{
q[l]=opnd[k];
l=l+1;
}
fprintf(f5,"%s\t%s\n",q,s2);
break;
}
else if(strcmp(mne,"RESB")==0)
lc=lc+atoi(opnd);
else if(strcmp(mne,"WORD")==0)
{
strcpy(s3,"#");
lc=lc+3;
fprintf(f5,"%s\t%s\n",opnd,s3);
break;
}
} // else la=-

fseek(f2,SEEK_SET,0);
fscanf(f1,"%s%s%s",la,mne,opnd);
}
fseek(f5,SEEK_SET,0);
pgmlen=lc-sa;
printf("H^%s^%d^0%x\n",name,sa,pgmlen);
printf("T^");
printf("00%d^0%x",sa,pgmlen);
fscanf(f5,"%s%s",obj1,obj2);
while(!feof(f5))
{
if(strcmp(obj2,"0000")==0)
printf("^%s%s",obj1,obj2);
else if(strcmp(obj2,"-")==0)
{
printf("^");
len1=strlen(obj1);
for(k=0;k<len1;k++)
printf("%d",obj1[k]);
}
else if(strcmp(obj2,"#")==0)
{
printf("^");
printf("%s",obj1);
}
fscanf(f5,"%s%s",obj1,obj2);
}
fseek(f5,SEEK_SET,0);
fscanf(f5,"%s%s",obj1,obj2);
while(!feof(f5))
{
if(strcmp(obj2,"0000")!=0)
{
if(strcmp(obj2,"-")!=0)
{
if(strcmp(obj2,"#")!=0)
{
printf("\n");
printf("T^%s^02^%s",obj1,obj2);
}
}
}
fscanf(f5,"%s%s",obj1,obj2);
}
printf("\nE^00%d",sa);
getch();
}

INPUT:
Input.txt:
COPY START 1000
- LDA ALPHA
- STA BETA
ALPHA RESW 1
BETA RESW 1
- END -
Optab.txt:
LDA 00
STA 23
LDCH 15
STCH 18
OUTPUT:
3) IMPLEMENTATION OF PASS 2 ASSEMBLER:
The two pass assembler performs two passes over the source program. ... Basically, the assembler goes
through the program one line at a time, and generates machine code for that instruction. Then the
assembler proceeds to the next instruction
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
main()
{
FILE *fint,*ftab,*flen,*fsym;
int op1[10],txtlen,txtlen1,i,j=0,len;
char
add[5],symadd[5],op[5],start[10],temp[30],line[20],label[20],m
ne[10],operand[10],symtab[10],opmne[10];
clrscr();
fint=fopen("input.txt","r");
flen=fopen("length.txt","r");
ftab=fopen("optab.txt","r");
fsym=fopen("symbol.txt","r");
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
if(strcmp(mne,"START")==0)
{
strcpy(start,operand);
fscanf(flen,"%d",&len);
}
printf("H^%s^%s^%d\nT^00%s^",label,start,len,start);
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
while(strcmp(mne,"END")!=0)
{
fscanf(ftab,"%s%s",opmne,op);
while(!feof(ftab))
{
if(strcmp(mne,opmne)==0)
{
fclose(ftab);
fscanf(fsym,"%s%s",symadd,symtab);
while(!feof(fsym))
{
if(strcmp(operand,symtab)==0)
{
printf("%s%s^",op,symadd);
break;
}
else
fscanf(fsym,"%s%s",symadd,symtab);
}
break;
}
else
fscanf(ftab,"%s%s",opmne,op);
}
if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))
{
if(strcmp(mne,"WORD")==0)
printf("0000%s^",operand);
else
{
len=strlen(operand);
for(i=2;i<len;i++)
{
printf("%d",operand[i]);
}
printf("^");
}
}
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
ftab=fopen("optab.txt","r");
fseek(ftab,SEEK_SET,0);
}
printf("\nE^00%s",start);
fclose(fint);
fclose(ftab);
fclose(fsym);
fclose(flen);
fclose(fout);
getch();
}

OUTPUT:
4) ABSOLUTE LOADER:

The absolute loader is a kind of loader in which relocated object files are created, loader
accepts these files and places them at a specified location in the memory. This type of loader
is called absolute loader because no relocating information is needed, rather it is obtained
from the programmer or assembler.

Algorithm:

1.Start the program

2.Assign the required variable

3.Open the filesfp1=fopen("input2.dat","r");fp2=fopen("out2.dat","w");

4.Read the content. Using while loop perform the loop until character is not equal to E.

5.Then compare whether the character is equal to H

6.If H then get the starting address,length and input

7.Else if the character is T then store the string as the three address in the output file
withinput[0],inuput[1] for addressinput[2],inuput[3] for address+1input[4],inuput[5] for
address+2

8.Else if it is not H or T then perform the following fprintf in fp2 for output file for
,input[0],inuput[1] for addressinput[2],inuput[3] for address+1input[4],inuput[5] for
address+2

9.Increment the address value by 3.

10.Read the next input string and repeat from step 4.

11.Finally terminate the program


SOURCE CODE:
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main()
{
char input[10];
int start,length,address;
FILE *fp1,*fp2;
//clrscr();
fp1=fopen("input12.txt","r");
fp2=fopen("output12.txt","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%d",&start);
fscanf(fp1,"%d",&length);
fscanf(fp1,"%s",input);
}
else if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",input);
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
else
{
fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);
fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);
fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);
address+=3;
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
printf("FINISHED");
////getch();
}
OUTPUT:
5) RELOCATION LOADER:
A loader in which some of the addresses in the program to be loaded are expressed relative to
the start of the program rather than in absolute form.
Algorithm:
1.Startthe program
2.Include the necessary header file and variable
3.Open the following two file fp1= relinput.dat fp2= reloutput.dat 4.Read the content. Using
while loop perform the loop until character is not equal to E.
5.If the character is H then get the variable add, length, and input
6.Else if the character is T then get the variable address and bitmask and perform the for loop
starting from zero and up to len.
7.Get the opcode ,addr and assign relocbit to bitmask
8.If relocabit is zero then actualadd=addr
9.Else addthe addr and starting value
10.Finally terminate the program

SOURCE CODE:
# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
void main()

{char add[6],length[10],input[10],binary[12],bitmask[12],relocbit;
int start,inp,len,i,address,opcode,addr,actualadd;FILE *fp1,*fp2;
//clrscr();
printf("Enter the actual starting address : ");
scanf("%d",&start);fp1=fopen("relinp.txt","r");
fp2=fopen("reloutput.txt","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",add);
fscanf(fp1,"%s",length);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",bitmask);
address+=start;
len=strlen(bitmask);
for(i=0;i<len;i++)
{
fscanf(fp1,"%d",&opcode);
fscanf(fp1,"%d",&addr);
relocbit=bitmask[i];
if(relocbit=='0')actualadd=addr;
else
actualadd=addr+start;
fprintf(fp2,"%d\t%d%d\n",address,opcode,actualadd);
address+=3;
}
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
printf("FINISHED");
//getch();
}
OUTPUT:
6) TEXT EDITOR:

Text editor:
Definition:
A text editor is a tool that allows a user to create and revise documents in a computer. Though
this task can be carried out in other modes, the word text editor commonly refers to the tool that
does this interactively. Earlier computer documents used to be primarily plain text documents,
but nowadays due to improved input-output mechanisms and file formats, a document frequently
contains pictures along with texts whose appearance (script, size, colour and style) can be varied
within the document. Apart from producing output of such wide variety, text editors today
provide many advanced features of interactiveness and output.

Types of text editor:


1. Line Editors - During original creation lines of text are recognised and delimited by end-
of-line markers, and during subsequent revision, the line must be explicitly specified by
line number or by some pattern context. eg. edlin editor in early MS-DOS systems.
2. Stream Editors - The idea here is similar to line editor, but the entire text is treated as a
single stream of characters. Hence the location for revision cannot be specified using line
numbers. Locations for revision are either specified by explicit positioning or by using
pattern context. eg. sed in Unix/Linux.

Line editors and stream editors are suitable for text-only documents.

3. Screen Editors - These allow the document to be viewed and operated upon as a two
dimensional plane, of which a portion may be displayed at a time. Any portion may be
specified for display and location for revision can be specified anywhere within the
displayed portion. eg. vi, emacs, etc.
4. Word Processors - Provides additional features to basic screen editors. Usually support
non-textual contents and choice of fonts, style, etc.
5. Structure Editors - These are editors for specific types of documents, so that the editor
recognises the structure/syntax of the document being prepared and helps in maintaining
that structure/syntax.

Diagram:
Salient Aspects of Text Editor

A text editor has to cover the following main aspects related to document creation, storage and
revision -

1. Interactive user interface


2. Appropriate format for storing the document in file in secondary storage
3. Efficient transfer of information between the user interface and the file in secondary
storage

Interactive text editor:

An important consideration of a document is its layout to a human reader. For this it is essential
to present a document in a rectangular form, on the monitor screen (terminal) or via a printer (as
an output of the interactive process). Further since a document can potentially be very large and
since in a computer terminal can display only a limited size of a document at a time, hence it is
necessary for a text editor to display only a portion (page) of a large document that cannot be
displayed in entireity at once.

The process of creating and revising a document in a computer is called editing. For interactive
editing a text editor displays a document on the terminal and accepts various types of input from
the user through different types of input devices, viz., keyboard, mouse, data-tablet (flat,
rectangular, elctromagnetically sensitive panel over which a ball-point-pen-like stylus is moved
and the coordinates of the stylus is sent to the system).

Initially the text editor may display the starting portion of the document. During editing the user
may give inputs to bring other portions to the display. Besides the displayed page, there is the
notion of a point within the page, indicated by an easily distinguishable symbol called the cursor,
where the next insertion, deletion or modification will be effective. An editor provides various
mechanisms (left-right-up-down keys, page-up, page-down keys, mouse pointer, commands,
etc.) for the cursor to be moved throughout the document. If the user requires it to be moved
outside of the displayed page, the editor displays the new portion that would contain the cursor.
Many editors also allows the user to select (and deselect) portions of the document whose
attributes can be modified as a whole (like change of font, delete, etc.)

The most basic action by a user in creating or revising a document is to type in the text through
the keyboard and, possibly, delete already typed portions. Most text editors support many other
features in order to reduce the work of the user as well as to produce a higher quality documents.
Commonly found features include cut-paste, pattern-search (and substitute), font-change,
working with multiple documents at the same time, etc.

Many text editors allows pictures and other non-text information to be included in a document.
Such editors provide mechanisms to either create/modify such information or embed such
objects inside the document.
The output of an editing process, i.e., the document, can be either saved as file(s) in the
secondary storage or sent to some destination in the network, such as a printer or some remote
host.

Structure of Text Editor

The structure of a text editor depends largely on the types of editing features and displaying
capabilities that are to be supported. To implement the diplaying capabilities, the semantics of
the meta data that may be present in the document file needs to be implemented as display
actions. For example, if the meta data implies a particular colour to be used for a segment of text,
editor should invoke methods to effect that colour for the particular segment of text. Since at a
time only a finite portion of the document can be displayed, such actions are to be taken for a
portion of the information in the file. However, the user may specify some other portion to be
displayed (through page-up, page-down, pattern search, etc.), in which case the display actions
must be performed for that portion. Thus the editor program should keep track of the size of the
display window, and the boundaries of the current displayed portion in terms of offsets from
some fixed point in the document (such as line number of the first and the last displayed lines,
etc.)

It is not enough to directly produce the display of a document page from the information in the
file. The user provides different editing inputs which implies changes in the displayed
information as well as the document file. For this, firstly, the editor should keep track of the
cursor position with respect to the displayed information. Then, one possible way to effect the
changes is to update the document file for each insertion, deletion or modification input, and then
redraw the page on the monitor according to the changed content. But this is a very inefficient
method. Instead, text editors maintain a memory image of the document, and pages are displayed
from this memory image rather than from the document file. In fact, when a document is being
created, a corresponding file in the secondary storage may not exist at all. All updations in the
document due to editing inputs from the user are effected in the memory image maintained in
suitable buffers.

SOURCE CODE:
<!DOCTYPE html>
<html>
<head>
<title> TEXT EDITOR</title>
<style>
div#textEditor{
margin: 0 auto;
width: 750px;
height: 300px;
}
div#theRibbon{
border-bottom: none;
padding: 10px;
background-color: rgb(40,110,89);
color: white;
border-radius: 8px 8px 0px 0px;
}
div#richTextArea{
border:2px solid rgb(40,110,89);
height: 100%;
width: 746px;
}
iframe#theWYSIWYG{
height: 100%;
width: 100%;
}
div#theRibbon > button {
color: white;
border: none;
outline: none;
background-color: transparent;
cursor: pointer;
}
div#theRibbon > button:hover{
background-color: rgb(20,90,70);
transition: all 0.3s linear 0s;
}
input[type="color"]{
border: none;
outline: none;
background-color: transparent;
}
</style>
<script>
window.addEventListener("load",function(){
var editor=theWYSIWYG.document;
editor.designMode="on";

boldButton.addEventListener("click",function(){
editor.execCommand("Bold",false,null);
},false);
italicButton.addEventListener("click",function(){
editor.execCommand("Italic",false,null);
},false);
supButton.addEventListener("click",function(){
editor.execCommand("Superscript",false,null);
},false);
subButton.addEventListener("click",function(){
editor.execCommand("Subscript",false,null);
},false);
strikeButton.addEventListener("click",function(){
editor.execCommand("strikethrough",false,null);
},false);
orderedListButton.addEventListener("click",function(){
editor.execCommand("InsertOrderedList",false,"newOL" +Math.round(Math.random() *
1000));
},false);
unorderdListButton.addEventListener("click",function(){
editor.execCommand("InsertUnOrderedList",false,"newOL"
+Math.round(Math.random() * 1000));
},false);

fontColorButton.addEventListener("change",function(event){
editor.execCommand("ForeColor",false, event.target.value );
},false);
highlightButton.addEventListener("change",function(event){
editor.execCommand("BackColor",false, event.target.value );
},false);
fontChanger.addEventListener("change",function(event){
editor.execCommand("FontName",false, event.target.value );
},false);
fontSizeChanger.addEventListener("change",function(event){
editor.execCommand("FontSize",false, event.target.value );
},false);
linkButton.addEventListener("click",function(){
var url=promt("Enter a URL","http://")
editor.execCommand("CreateLink",false, url);
},false);
unLinkButton.addEventListener("click",function(){
editor.execCommand("UnLink",false, null);
},false);
redoButton.addEventListener("click",function(){
editor.execCommand("Redo",false, null);
},false);
undoButton.addEventListener("click",function(){
editor.execCommand("Undo",false, null);
},false);
},false);
</script>
</head>
<body>
<div id="textEditor">
<div id="theRibbon">
<button id="boldButton" title="Bold"><b>B</b></button>
<button id="italicButton" title="Italic"><em>I</em></button>
<button id="supButton" title="Superscript">X<sup>2</sup></button>
<button id="subButton" title="Subscript">X<sub>2</sub></button>
<button id="strikeButton" title="strikethrough"><s>abc</s></button>
<button id="orderedListButton" title="Numbered list">(i)</button>
<button id="unorderdListButton" title="Bulleted list"">&bull;</button>
<input type="color" id="fontColorButton" title="Change Font Color">
<input type="color" id="highlightButton" title="highlight Text">
<select id="fontChanger">
<option value="Times New Roman">Times New Roman</option>
<option value="Cansolas">Cansolas</option>
<option value="Tahoma">Tahoma</option>
<option value="monospace">monospace</option>
<option value="cursive">cursive</option>
<option value="sans-serif">sans-serif</option>
<option value="Calibri">Calibri</option>
</select>
</div>
<select> id="fontSizeChanger">
<script>
for(var i=1;i<10;i++){
document.write("<option value='"+i+"'>"+i+"</option>");
}
</script>
</select>
<button id="linkButton" title="Create Link">Link</button>
<button id="unLinkButton" title="Remove Link">UnLink</button>
<button id="undoButton" title="Undo the previous action">&larr;</button>
<button id="redoButton" title="Redo">&rarr;</button>
<div id="richTextArea">
<iframe id="theWYSIWYG" name="theWYSIWYG" frameborder="0"></iframe>
</div>
</div>
<script>
var fonts=document.querySelectorAll("select#fontChanger > option");
for (var i =0;i < fonts.length; i++) {
fonts[i].style.fontFamily=fonts[i].value;
}
</script>
</body>
</html>
OUTPUT:

7) DIRECT LINKING LOADER:


Direct Linking Loaders  A Direct linking loader is a general relocating loader and is the
most popular loading scheme presently used.  This scheme has an advantage that it allows the
programmer to use multiple procedure and multiple data segments.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int i,j,k,pgmaddr,csaddr,cslth=0,z,addr1,l1;
char line[50],name[10],len[10],la[10],addr[10];
FILE *fp1;
//clrscr();
printf("Enter the starting address\n");
scanf("%d",&pgmaddr);
csaddr=pgmaddr;
for(k=0;k<2;k++)
{
if(k==0)
fp1=fopen("linkin.txt","r");
if(k==1)
fp1=fopen("linkin1.txt","r");
do
{
fscanf(fp1,"%s",line);
if(line[0]=='H')
{
for(i=2,j=0;i<8,j<6;i++,j++)
name[j]=line[i];
name[j]='\0';
for(i=16,j=0;i<20,j<5;i++,j++)
len[j]=line[i];
len[j]='\0';
cslth=atoi(len);
printf("%s\t\t%d\t%s\n",name,csaddr,len);
}
else if(line[0]=='D')
{
i=2;
j=0;
do
{
do
{
la[j++]=line[i++];
}while(line[i]!='^');
la[j]='\0';
j=0;i++;
do
{addr[j++]=line[i++];
} while(line[i]!='^');
i++;
addr[j]='\0';
addr1=atoi(addr)+csaddr;
j=0;
printf("%s\t\t%d\n",la,addr1);
} while(line[i]!='\0');
}
else if(line[0]=='R'||'T')
z=0;
else if(line[0]=='E')
fclose(fp1);
}while(!feof(fp1));
csaddr=csaddr+cslth;
}
//getch();
}
OUTPUT:
8) ONE PASS MACRO PROCESSOR:

A one-pass macro processor that alternate between macro definition and macro
expansion is able to handle “macro in macro”.
• However, because of the one-pass structure, the definition of a macro must appear in
the source program before any statements that invoke that macro.
Algorithm:
begin {macro processor}
EXPANDING : = FALSE
while OPCODE ≠ ‘END’ do
begin
GETLINE
PROCESSLINE
end {while}
end {macro processor}
procedure PROCESSLINE
begin
search NAMTAB for OPCODE
if found then
EXPAND
else if OPCODE = ‘MACRO’ then
DEFINE
else write source line to expanded file
end {PROCESSLINE}

Algorithm:

procedure EXPAND
begin
EXPANDING : = TRUE
get first line of macro definition {prototype} from DEFTAB
set up arguments from macro invocation in ARGTAB
write macro invocation to expanded file as a comment
while not end of macro definition do
begin
GETLINE
PROCESSLINE
end {while}
EXPANDING : = FALSE
end {EXPAND}

procedure GETLINE
begin
if EXPANDING then
begin get next line of macro definition from DEFTAB
substitute arguments from ARGTAB for positional notation
end {if}
else
read next line from input file
end {GETLINE}
SOURCE CODE:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

void main()

FILE *f1,*f2,*f3,*f4,*f5;

int len,i,pos=1;

char arg[20],mne[20],opnd[20],la[20],name[20],mne1[20],opnd1[20],pos1[10],pos2[10];

//clrscr();
f1=fopen("input1.txt","r");

f2=fopen("namtab.txt","w+");

f3=fopen("deftab.txt","w+");

f4=fopen("argtab.txt","w+");

f5=fopen("op.txt","w+");

fscanf(f1,"%s%s%s",la,mne,opnd);

while(strcmp(mne,"END")!=0)

if(strcmp(mne,"MACRO")==0)

fprintf(f2,"%s\n",la);

fseek(f2,SEEK_SET,0);

fprintf(f3,"%s\t%s\n",la,opnd);

fscanf(f1,"%s%s%s",la,mne,opnd);

while(strcmp(mne,"MEND")!=0)

if(opnd[0]=='&')

itoa(pos,pos1,5);

strcpy(pos2,"?");

strcpy(opnd,strcat(pos2,pos1));

pos=pos+1;

}
fprintf(f3,"%s\t%s\n",mne,opnd);

fscanf(f1,"%s%s%s",la,mne,opnd);

fprintf(f3,"%s",mne);

else

fscanf(f2,"%s",name);

if(strcmp(mne,name)==0)

len=strlen(opnd);

for(i=0;i<len;i++)

if(opnd[i]!=',')

fprintf(f4,"%c",opnd[i]);

else

fprintf(f4,"\n");

fseek(f3,SEEK_SET,0);

fseek(f4,SEEK_SET,0);

fscanf(f3,"%s%s",mne1,opnd1);

fprintf(f5,".\t%s\t%s\n",mne1,opnd);

fscanf(f3,"%s%s",mne1,opnd1);
while(strcmp(mne1,"MEND")!=0)

if((opnd[0]=='?'))

fscanf(f4,"%s",arg);

fprintf(f5,"-\t%s\t%s\n",mne1,arg);

else

fprintf(f5,"-\t%s\t%s\n",mne1,opnd1);

fscanf(f3,"%s%s",mne1,opnd1);

else

fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);

fscanf(f1,"%s%s%s",la,mne,opnd);

fprintf(f5,"%s\t%s\t%s",la,mne,opnd);

fclose(f1);

fclose(f2);

fclose(f3);

fclose(f4);

fclose(f5);
printf("files to be viewed \n");

printf("1. argtab.txt\n");

printf("2. namtab.txt\n");

printf("3. deftab.txt\n");

printf("4. op.txt\n");

//getch();

OUTPUT:
9) LEXICAL ANAYSER:

Compiler is responsible for converting high level language in machine language. There are
several phases involved in this and lexical analysis is the first phase.

Lexical analyzer reads the characters from source code and convert it into tokens.

different tokens or lexemes are:

 Keywords
 Identifiers
 Operators
 Constants

SOURCE PROGRAM:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<ctype.h>

int isKeyword(char buffer[]){

char keywords[32][10] = {"auto","break","case","char","const","continue","default",

"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",

"sizeof","static","struct","switch","typedef","union",

"unsigned","void","volatile","while"};

int i, flag = 0;

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

if(strcmp(keywords[i], buffer) == 0){

flag = 1;

break;

return flag;

int main(){

char ch, buffer[15], operators[] = "+-*/%=";

FILE *fp;

int i,j=0;

fp = fopen("program.txt","r");
if(fp == NULL){

printf("error while opening the file\n");

exit(0);

while((ch = fgetc(fp)) != EOF){

for(i = 0; i < 6; ++i){

if(ch == operators[i])

printf("%c is operator\n", ch);

if(isalnum(ch)){

buffer[j++] = ch;

else if((ch == ' ' || ch == '\n') && (j != 0))

buffer[j] = '\0';

j = 0;

if(isKeyword(buffer) == 1)

printf("%s is keyword\n", buffer);

else

printf("%s is indentifier\n", buffer);


}

fclose(fp);

return 0;

OUTPUT:
10) SHIFT REDUCE PARSER:
Shift-reduce parser. A shift-reduce parser is a class of efficient, table-driven bottom-up parsing
methods for computer languages and other notations formally defined by a grammar. The parsing
methods most commonly used for parsing programming languages, LR parsing and its variations, are
shift-reduce methods.
The algorithm:
loop forever: for top-of-stack symbol, s, and next
input symbol, a
case action of T[s,a]
shift x: (x is a STATE number)
push a, then x on the top of the stack and
advance ip to point to the next input symbol.
reduce y: (y is a PRODUCTION number)
Assume that the production is of the form
A ==> beta
pop 2 * |beta| symbols of the stack. At this
point the top of the stack should be a state
number,
say s’. push A, then goto of T[s’,A] (a state
number)
on the top of the stack. Output the production
A ==> beta.
accept:
return --- a successful parse.
default:
error --- the input string is not in the language
SOURCE CODE:
#include<stdio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
void check();
int main()
{

puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");


puts("enter input string ");
gets(a);
c=strlen(a);
strcpy(act,"SHIFT->");
puts("stack \t input \t action");
for(k=0,i=0; j<c; k++,i++,j++)
{
if(a[j]=='i' && a[j+1]=='d')
{
stk[i]=a[j];
stk[i+1]=a[j+1];
stk[i+2]='\0';
a[j]=' ';
a[j+1]=' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);
check();
}
else
{
stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check();
}
}

}
void check()
{
strcpy(ac,"REDUCE TO E");
for(z=0; z<c; z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='E';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='*' && stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
}
OUTPUT:
11) Program to copy content to text file:

packagefilepackage;

import java.io.*;

public class kk {

public static void main(String[] args) {

InputStreamistream;

OutputStreamostream=null;

int c;

finalint EOF = -1;

istream = System.in;

File outFile= new File("Data1.txt");

System.out.println("Type THE PROGRAM");


try {

ostream = new FileOutputStream(outFile);

while ((c = istream.read()) != EOF)

ostream.write(c);

} catch (IOException e) {

System.out.println("Error: " + e.getMessage());

} finally {

try {

istream.close();

ostream.close();

} catch (IOException e) {

System.out.println("File did not close");

Program to count no.of lines in text file

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileReader;

importjava.io.IOException;

public class count

public static void main(String[] args) throws IOException

File f1=new File("Data1.txt"); //Creation of File Descriptor for input file


intlinecount=0; //Intializinglinecount as zero

FileReaderfr=new FileReader(f1); //Creation of File Reader object

BufferedReaderbr = new BufferedReader(fr); //Creation of File Reader object

String s;

while((s=br.readLine())!=null) //Reading Content from the file line by line

linecount++; //For each line increment linecount by one

fr.close();

System.out.println("Number of lines in the Files:"+linecount); //Print the line count

12) Creation of table with updated address:

importjava.io.BufferedReader;

importjava.io.FileReader;

importjava.io.FileWriter;

importjava.io.IOException;

classobj

public static void main(String args[])

StringBuildersb = new StringBuilder();

String strLine = "";


try

String filename= "Data1.txt";

FileWriterfw = new FileWriter(filename,true);

fw.close();

BufferedReaderbr = new BufferedReader(new FileReader("Data1.txt"));

while (strLine != null)

sb.append(strLine);

strLine = br.readLine();

System.out.println(strLine);

System.out.println("-----------------------------------------------------------------------");

br.close();

catch(IOExceptionioe)

System.err.println("IOException: " + ioe.getMessage());

intloopVal;

intend_value=0013;
for(loopVal=0000;loopVal<end_value;loopVal+=3)

System.out.println("********************************************************************
******");

System.out.println("address="+loopVal);

S-ar putea să vă placă și