Professional Documents
Culture Documents
SPCC - Lab 23 24
SPCC - Lab 23 24
SPCC - Lab 23 24
Lab Journal
Index
Page
Ex. No Name of the Experiment
Number
Implement a program to count number of chars, words,
1 sentences, paragraph present in input.
8 Study of LLVM
11 ABSOLUTE LOADER
Translation of a Statement
Grammar Components:
a set of rules, consisting of left-hand side and right-hand side ( the left-hand side consist of a single
a start symbol.
Example:
expression expression + term
| expression - term
| term
factor Identifier
| Constant
| ( expression )
Grammar Requirements:
- a grammar must be unambiguous: a unique parse tree must exist for each sentence in
the language ;
- a grammar must be and deterministic: it must enable to decide which rule to use in
order to complete the parse tree or to discover that no tree can be built to accommodate the next
input.
%%
rules
%%
user code
● The definitions section contains declarations of simple name definitions to simplify the
scanner specification.
● Name definitions have the form: name definition
● Example:
DIGIT [0-9]
ID [a-z][a-z0-9]*
● The rules section of the flex input contains a series of rules of the form: pattern action
● Example:
{ID} printf( "An identifier: %s\n", yytext );
AIM: Implement a program to count number of chars, words, sentences, paragraph present in input.
ALGORITHM:
Step by step descriptive logic to count characters, words and lines in a text file.
1. Open source file in r (read) mode.
2. Initialize three variables characters = 0, words = 0 and lines = 0 to store counts.
3. Read a character from file and store it to some variable say ch.
4. Increment characters count.
Increment words count if current character is whitespace character i.e. if (ch == ' ' || ch == '\t' || ch
== '\n' || ch == '\0').
Increment lines count if current character is new line character i.e. if (ch == '\n' || ch == '\0').
5. Repeat step 3-4 till file has reached end.
6. Finally after file has reached end increment words and lines count by one if total characters > 0 to
make sure you count last word and line.
Get the inputs-data types and symbols from the user in the statement
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
exit(EXIT_FAILURE);
}
/*
* Logic to count characters, words and lines.
*/
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
return 0;
}
CONCLUSION
Ex. No. 2
IMPLEMENT SYMBOL TABLE CREATION IN C.
Date:
ALGORITHM:
1. Get the inputs-data types and symbols from the user in the statement
2. Data types and symbols are separated to identify the types separately.
If the data type is char, then 1 byte(8 bits) are allocated.If the data type is integer, then 2
bytes(16 bits) are allocated. If it is long then 4 bytes are allocated. If it is float 8 bytes are allocated
in the memory.
3. The addresses are allocated for each data type and are printed along with the symbols.
PROGRAM:
if(strcmp(type,"float")==0)
{
addr+=(4*len); size=4*len;
}
if(strcmp(type,"char")==0)
{
addr+=(1*len); size=1*len;
}
if(strcmp(type,"long")==0)
{
addr=addr+(4*len); size=4*len;
}
printf("\n%s\t\t%s\t\t%d\t\t%d",var,type,size,addr);
}
getch();
}
CONCLUSION:
Ex. No. 3
CONVERSION OF INFIX TO POSTFIX EXPRESSION IN C
Date:
AIM:
To write a C program to convert of infix to postfix expression in C
ALGORITHM:
5. If it is a closing parenthesis then pop a character from the stack and let it be pch and append
pch value to postfix form. Repeat this process until pch equals opening paranthesis.
6. If ch=4, pop all entries from the stack and append it to the postfix form.
8. If stack priority of pch is greater than or equal to the infix priority of ch,append pch to
postfix form and push ch to the stack.
PROGRAM:
push(ch[j]);
}
}
}while(ch[j]!='#'); printf("Required postfix string is \n"); printf("%s",post);
getch();
}
void concat(char item1)
{
post[i]=item1; i=i+1;
}
void push(char item2)
{
top=top+1; stack[top]=item2;
}
char pop()
{
char delitem; delitem=stack[top]; top=top-1; return(delitem);
}
int infixpri(char ch1)
{
int k; switch(ch1)
{
case '*':
case '/':
DEPT. PF COMPUTER ENGINEERING, BHIVPURO ROAD Page 13
SARASWATI EDUCATION SOCIETY’S
k=2;
break; case '+':
case '-': k=1;
break; case '(':
k=3;
break; case ')':
case '#':
k=0;
break;
}
return(k);
}
int stackpri(char ch2)
{
int m; switch(ch2)
{
case '*':
case '/':
m=2;
break;
case '+':
case '-': m=1;
break; case '(':
case '#':
m=0;
break;
}
return(m);
}
CONCLUSION:
Ex. No. 4.
CONVERSION OF REGULAR EXPRESSION TO NFA
Date:
AIM:
To write a C program to convert regular expression to NFA.
ALGORITHM:
3. If there is a final state in the above e-closure , add all these state to final state.
7. For each state in the e-closure find the move on current input.
9. These are the states for the move in the new NFA for current state and input.
PROGRAM:
printf("%d-----> %d e\n",f1,i1);
printf("%d-----> %d e\n",i,f);
break; case 2:
printf("\nEnter the new symbol : "); scanf("%d",&sym);
i1=i;f1=f; i2=count++;f2=count++;
printf("AT \n1.Front\n2.Back\nEnter ur choice :"); scanf("%d",&s);
printf("\nThe Added Transitions are :\n"); printf("%d %d %d\n",i2,sym,f2);
if (s==1)
printf("%d e %d\n",f2,i1);
else
printf("%d e %d\n",f1,i2);
break; case 3:
printf("Enter the new symbol:"); scanf("%d",&sym);
i1=i;f1=f; i2=count++;f2=count++; i=count++;f=count++;
printf("\nThe Added Transition are:\n"); printf("%d %d %d\n",i2,sym,f2); printf("%d e %d\n",i,i1);
printf("%d e %d\n",i,i2);
printf("%d e %d\n",f1,f);
printf("%d e %d\n",f2,f); break;
case 4: exit(0);
}
}
}
CONCLUSION:
Ex. No. 5.
IMPLEMENTATION OF SCIENTIFIC CALCULATOR
Date:
AIM:
To write a C program to implement scientific calculator
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
float sine(float x);
float cosine(float x);
float tangent(float x);
float sineh(float x);
float cosineh(float x);
float tangenth(float x);
float logten(float x);
float squareroot(float x);
float exponent(float x);
float power(float x,float y);
int main()
{
int x,y,n,answer;
printf("What do you want to do?\n");
printf("1.sin 2.cos 3. tan 4. sinh 5.cosh 6.tanh 7.1og10 8. square root. 9.exponent 10.power.");
scanf ("%d",&n);
if (n<9 && n>0)
{
printf("\n What is x? ");
scanf("%f",&x);
switch (n)
{
case 1: answer = sine(x); break;
case 2: answer = cosine(x); break;
case 3: answer = tangent(x); break;
case 4: answer = sineh(x); break;
case 5: answer = cosineh(x); break;
case 6: answer = tangenth(x); break; case 7: answer = logten(x); break; case 8: answer =
squareroot(x); break; case 9: answer = exponent(x); break;
}
}
if (n==10)
{
printf("What is x and y?\n"); scanf("%f%f",&x,&y); answer = power(x,y);
}
if (n>0 && n<11) printf("%f",answer);
else
printf("Wrong input.\n"); return 0;
}
float sine(float x)
{
return (sin (x*PI/180));
}
float cosine(float x)
{
return (cos (x*PI/180));
}
float tangent(float x)
{
return (tan(x*PI/180));
}
float sineh(float x)
{
return (sinh(x));
}
float cosineh(float x)
{
return (sinh(x));
}
float tangenth(float x)
{
return (sinh(x));
}
float logten(float x)
{
return (log10(x));
}
float squareroot(float x)
{
return (sqrt(x));
}
float exponent(float x)
{
return(exp(x));
}
float power(float x, float y)
{
return (pow(x,y));
}
CONCLUSION:
Ex. No. 6.
IMPLEMENTATION OF LL(1)
Date: PARSER
AIM:
To write a C program to implement LL(1) parser
ALGORITHM:
PROGRAM:
else
{
if(s>0)
{
while(st[i][j]!=st[a][0])
{ a++;
} b=0;
while(ft[a][b]!='\0')
{
for(m=0;m<l;m++)
{
if(ft[i][m]==ft[a][b]) goto s2;
}
ft[i][l]=ft[a][b]; l=l+1; s2:b=b+1;
}
}
}
while(st[i][j]!='\0')
{
if(st[i][j]=='|')
{
j=j+1; goto l1;
}
j=j+1;
}
ft[i][l]='\0';
}
}
printf("first pos\n"); for(i=0;i<n;i++)
printf("FIRS[%c]=%s\n",st[i][0],ft[i]); fol[0][0]='$';
for(i=0;i<n;i++)
{ k=0; j=3;
if(i==0) l=1;
else l=0;
k1:while((st[i][0]!=st[k][j])&&(k<n))
{
if(st[k][j]=='\0')
{ k++; j=2;
DEPT. PF COMPUTER ENGINEERING, BHIVPURO ROAD Page 21
SARASWATI EDUCATION SOCIETY’S
} j++;
}
j=j+1;
if(st[i][0]==st[k][j-1])
{
if((st[k][j]!='|')&&(st[k][j]!='\0'))
{ a=0;
if(!((st[k][j]>64)&&(st[k][j]<91)))
{
for(m=0;m<l;m++)
{
if(fol[i][m]==st[k][j]) goto q3;
}
fol[i][l]=st[k][j]; l++;
q3:
}
else
{
while(st[k][j]!=st[a][0])
{ a++;
} p=0;
while(ft[a][p]!='\0')
{
if(ft[a][p]!='@')
{
for(m=0;m<l;m++)
{
if(fol[i][m]==ft[a][p]) goto q2;
}
fol[i][l]=ft[a][p]; l=l+1;
}
else
e=1; q2:p++;
}
if(e==1)
{ e=0;
goto a1;
}
DEPT. PF COMPUTER ENGINEERING, BHIVPURO ROAD Page 22
SARASWATI EDUCATION SOCIETY’S
}
}
else
{ a1:c=0; a=0;
while(st[k][0]!=st[a][0])
{ a++;
}
while((fol[a][c]!='\0')&&(st[a][0]!=st[i][0]))
{
for(m=0;m<l;m++)
{
if(fol[i][m]==fol[a][c]) goto q1;
}
fol[i][l]=fol[a][c]; l++;
q1:c++;
}
}
goto k1;
}
fol[i][l]='\0';
}
printf("follow pos\n"); for(i=0;i<n;i++)
printf("FOLLOW[%c]=%s\n",st[i][0],fol[i]); printf("\n");
s=0;
for(i=0;i<n;i++)
{ j=3;
while(st[i][j]!='\0')
{
if((st[i][j-1]=='|')||(j==3))
{
for(p=0;p<=2;p++)
{
fin[s][p]=st[i][p];
}
t=j; for(p=3;((st[i][j]!='|')&&(st[i][j]!='\0'));p++)
{
fin[s][p]=st[i][j]; j++;
}
DEPT. PF COMPUTER ENGINEERING, BHIVPURO ROAD Page 23
SARASWATI EDUCATION SOCIETY’S
fin[s][p]='\0';
if(st[i][k]=='@')
{ b=0; a=0;
while(st[a][0]!=st[i][0])
{ a++;
}
while(fol[a][b]!='\0')
{
printf("M[%c,%c]=%s\n",st[i][0],fol[a][b],fin[s]); b++;
}
}
else if(!((st[i][t]>64)&&(st[i][t]<91)))
printf("M[%c,%c]=%s\n",st[i][0],st[i][t],fin[s]); else
{ b=0; a=0;
while(st[a][0]!=st[i][3])
{ a++;
}
while(ft[a][b]!='\0')
{
printf("M[%c,%c]=%s\n",st[i][0],ft[a][b],fin[s]); b++;
}
} s++;
}
if(st[i][j]=='|') j++;
}
}
getch();
}
OUTPUT:
FOLLOW[C] =ed$
M [S , e] =S->CC
M [S , d] =S->CC
M [C , e] =C->eC
M [C , d] =C->d
CONCLUSION:
Exp. No. 7
DESIGN AND IMPLEMENT PASS I OF TWO
PASS ASSEMBLER
Date:
AIM:
a) Write a program to generate Machine Op-Code Table, Symbol Table and Pseudo Op- Code table during
First Pass Assembler.
b) Write a program to generate Machine Op- code table using Two pass Assembler.
THEORY:
FLOW CHART-PASS-1
A. PROGRAM
import java.io.*;
import java.lang.*;
class ass12
{
int lc=0;
int l[]=new int[50];
int val[]=new int[50];
int lval[]=new int[50];
int x=1,m=0,n=0;
String sym[]=new String[50];
DEPT. OF COMPUTER ENGINEERING
SARASWATI EDUCATION SOCIETY’S
if(!imp.equals("end"))
{
for(i=0;i<imp.length();i++)
{
ch=imp.charAt(i);
if(ch!=' ')
{
y++;
}
else
{
break;
}
}
bs=imp.substring(0,y);
as=imp.substring((y+1));
}
else
{
bs=imp;
as=null;
}
for(i=0;i<pot.length;i++)
{
if(bs.equals(pot[i]))
{
l2=bs;
}
}
for(i=0;i<mot.length;i++)
DEPT. OF COMPUTER ENGINEERING
SARASWATI EDUCATION SOCIETY’S
{
if(bs.equals(mot[i]))
{
l2=bs;
}
if(l2==null)
{
l1=bs;
for(i=0;i<pot.length;i++)
{
if(as.equals(pot[i]))
{
l2=as;
}
}
for(i=0;i<mot.length;i++)
{
if(as.equals(mot[i]))
{
l2=as;
}
}
}
if(l2==null)
{
for(i=0;i<as.length();i++)
{
ch=as.charAt(i);
if(ch!=' ')
{
nu++;
}
else
{
break;
}
}
bs=as.substring(0,nu);
as=as.substring((nu+1));
l2=bs;
}
for(i=0;i<mot.length;i++)
{
if(l2.equals(mot[i]))
{
lc=lc+mot1[i];
}
}
if(l2.equals("DC") || l2.equals("DS"))
lc=lc+4;
l[(x++)]=lc;
System.out.println(imp+" "+l[(x-2)]);
if(l1!=null)
{
sym[m]=l1;
val[m]=l[x-2];
m++;
}
if(l4!=null)
{
if(l4.charAt(0)=='=')
{
lit[n]=l4;
lval[n]=l[(x-2)];
n++;
}
}
if(l2.equals("end"))
{
System.out.println("\n\n*****************Symbol Table**********************");
System.out.println("\n\nSymbol value");
for(i=0;i<m;i++)
System.out.println(sym[i]+" "+val[i]);
System.out.println("\n\n*****************Literal Table**********************");
System.out.println("\n\nLiteral value");
for(i=0;i<n;i++)
System.out.println(lit[i]+" "+lval[i]);
}
}
}
class tables
{
public static void main(String args[]) throws IOException
{
FileInputStream fin=new FileInputStream("D:\\java-assembler\\P2\\prg.txt");
DataInputStream in=new DataInputStream(fin);
while(in.available()!=0)
{
imps=in.readLine();
d.lex(imps);
}
in.close();
}
}
DEPT. OF COMPUTER ENGINEERING
SARASWATI EDUCATION SOCIETY’S
/*
OUTPUT :
C:\Java\jdk1.6.0\bin>java tables
exp2 start 0
using *,15 0
mov 1,four 0
sub 1,four 4
add 1,FIVE 8
mov 1,=F'7' 12
mov 1,=F'5' 16
FIVE DC F'5' 20
four DC F'4' 24
TEMP DS '1'F 28
end 32
*****************Symbol Table**********************
Symbol value
exp2 0
FIVE 20
four 24
TEMP 28
*****************Literal Table**********************
Literal value
=F'7' 12
=F'5' 16
C:\Java\jdk1.6.0\bin>
*/
B. PROGRAM
import java.io.*;
class Pass2
DEPT. OF COMPUTER ENGINEERING
SARASWATI EDUCATION SOCIETY’S
{
public static void main(String args[])throws IOException
{
int i,j,k;
//Symbol table
String sym[]={"pg","four","five","temp"};
int sval[]={0,20,24,28};
int slen[]={1,4,4,4};
char sreloc[]={'R','R','R','R'};
int nos=sym.length;
//Literal Table
String Lit[]={"=f'7'"};
int lval[]={32};
int llen[]={4};
char lreloc[]={'R'};
int nol=Lit.length;
//Base Table
int reg[]=new int[15];
int avail[]=new int[15];
int value[]=new int[15];
for(i=0;i<15;i++)
{
reg[i]=i+1;
avail[i]=value[i]=0;
}
for(i=0;i<10;i++)
System.out.println(Label[i]+"\t"+Opcode[i]+"\t"+op1[i]+"\t"+op2[i]);
System.out.println();
System.out.println();
System.out.println("LC \t Instruction");
int lc=0;
for(k=0;k<nos;k++)
if(op2[i].equals(sym[k]))
{
t1=sval[k];
break;
}
for(k=0;k<nol;k++)
if(op2[i].equals(Lit[k]))
{
t1=lval[k];
break;
}
for(k=0;k<16;k++)
if(avail[k]==1) break;
System.out.println(lc+"\t"+Opcode[i]+"\t"+op1[i]+" , "+t1+"
( 0 , "+(k+1)+" )");
lc = lc+mlen[j];
} }//end of else
}
//print the base table
System.out.println("The base table is");
System.out.println("-----------------------------");
System.out.println("Register\tAvailability\tContent");
for(i=0;i<15;i++)
System.out.println(reg[i]+"\t\t"+avail[i]+"\t\t"+value[i]);
System.out.println();
}//end of main
}//end of class
OUTPUT:
Input:
Input.txt
- LDA ALPHA
Input.txt
- ADD ONE
- SUB TWO
- STA BETA
ONE RESB 2
TWO WORD 5
BETA RESW 1
- END -
Optab.txt
LDA 00
STA 23
ADD 01
SUB 05
Output:
Symtab.txt
1012 ALPHA
1017 ONE
1019 TWO
1022 BETA
1025 - END -
Program
length = 25
CONCLUSION:
Ex. No. 8
STUDY OF LLVM
Date:
Introduction:
The LLVM compiler infrastructure project is a "collection of modular and reusable
compiler and toolchain technologies"[3] used to develop compiler front ends and back ends.
LLVM is written in C++ and is designed for compile-time, link-time, run-time, and "idle- time"
optimization of programs written in arbitrary programming languages. Originally implemented
for C and C++, the language-agnostic design of LLVM has since spawned a wide variety of
front ends: languages with compilers that use LLVM include ActionScript,
Ada, C#, Common Lisp, Crystal, CUDA, D, Delphi, Fortran, Graphical G Programming
Language, Halide, Haskell, Java bytecode, Julia, Kotlin, Lua, Objective-C, OpenGL Shading
Language, Pony, Python, R, Ruby, Rust, Scala, Swift, and Xojo.
Features:
1. LLVM can provide the middle layers of a complete compiler system, taking intermediate
representation (IR) code from a compiler and emitting an optimized IR. This new IR can then be
converted and linked into machine-dependent assembly language code for a target platform.
LLVM can accept the IR from the GNU Compiler Collection (GCC) toolchain, allowing it to be
used with a wide array of extant compilers written for that project.
2. LLVM can also generate relocatable machine code at compile-time or link-time or even
binary machine code at run-time.
4. The LLVM JIT compiler can optimize unneeded static branches out of a program at
runtime, and thus is useful for partial evaluation in cases where a program has many options,
most of which can easily be determined unneeded in a specific environment. This feature
is used in the OpenGL pipeline of Mac OS X Leopard (v10.5) to provide support for missing
hardware features
Components
Front ends
Intermediate representation Back ends
Linker
C++ Standard Library Debugger
CONCLUSION:
Ex. No. 9
TWO PASS MACROPROCESSOR
Date:
Theory: -
The program which is responsible for processing of macro i.e. group of instructions.
Function of macro processor:
1. Recognize macro definition
2. Store the macro definition
3. Recognize the macro call
4. Perform macro expansion
The purpose of pass2 is to recognize the macro call and perform
The macro expansion.
Database required for pass2:
1. The copy of the input macro source deck.
2. The output expanded source deck to be used as input to the assembler.
3. The Macro Definition Table (MDT) created by pass1.
4. The Macro Name Table (MNT) created by pass1.
5. The Macro Definition Table Pointer (MDTP) used to indicate the next line of text to be used
during macro expansion.
6.The Argument List Array (ALA) used to substitute macro call argument for the index markers in
the stored macro definition.
ALGORITHM:
1. Get the statement from the input file
2. If the statement has the directive “MACRO”, then the number of macro “n” will be
incremented by 1
def_cnt++;
}while(!feof(deftable));
do
{
fscanf(namtable,"%s%d%d",namtab[nam_cnt].MacroName,
&namtab[nam_cnt].beg,&namtab[nam_cnt].end);
nam_cnt++; }
while(!feof(namtable));
fclose(deftable);
fclose(optab);
fclose(namtable);
}
int ismacro
(char *str)
{
int i;
for(i=0;i<nam_cnt;i++)
if(!strcmp(namtab[i].MacroName,str))
return 1;
return 0;
}
int iskeyword(char *str)
{ int i;
for(i=0;i<cnt;i++)
if(!strcmp(optable[i],str))
return 1; return 0;
}
void expand(char *name,char *args) {
FILE *argtbl;
int beg,end,i,j,index;
char operand[30],tmp[20];
argtbl = fopen("argtab.txt","a+"); for(i=0;i<nam_cnt;i++)
{
if(!strcmp(namtab[i].MacroName,name))
{
beg = namtab[i].beg; end = namtab[i].end; } }arg_cnt = 1; i=0; do {
j=0;
do
{
argtab[arg_cnt][j++] = args[i++];
}while(args[i] != ',' && args[i] != '\0');
argtab[arg_cnt][j] = '\0';
arg_cnt++;
}while(args[i++] != '\0');
fprintf(argtbl,"\n%s :",name);
for(i=0;i<arg_cnt;i++)
{
fprintf(argtbl,"%s",argtab[i]);
}for(i=beg+1;i<=end;i++)
{ fprintf(exp,"\t%s\t",deftab[i].instruction);
strcpy(operand,deftab[i].operand);
for(j=0;j<strlen(operand);j++)
{
if(operand[j] == '?') {
operand[j] = '%';
index = operand[j+1]-48;
operand[j+1] = 's';
sprintf(tmp,operand,argtab[index]);
strcpy(operand,tmp);
}
}
fprintf(exp,"%s\n",operand);
}
fclose(argtbl);
getch();
}
void main()
{
FILE *source,*argtbl;
char str[30],str1[30],str2[30];
int i;
source = fopen("prog.txt","r");
argtbl = fopen("argtab.txt","w+");
exp = fopen("exppgm.txt","w+");
fclose(argtbl);
initialize(); do { fscanf(source,"%s %s",str,str1); beg:
if(feof(source)){}
else if(!strcmp(str1,"MACRO"))
{
strcpy(optable[cnt++],str); fscanf(source,"%s",str2); do { fscanf(source,"%s %s",str,str1);
}while(strcmp(str,"MEND")); strcpy(str,str1);
fscanf(source,"%s",str1);
goto beg;
}else if(iskeyword(str)) { if(ismacro(str)) {
fprintf(exp,".\t%s\t%s\n",str,str1);
expand(str,str1);
}else fprintf(exp,"\t%s\t%s\n",str,str1); }else {
fscanf(source,"%s",str2);
if(ismacro(str1))
{
fprintf(exp,".%s\t%s\t%s\n",str,str1,str2);
fprintf(exp,"%s",str);
expand(str1,str2);
}else fprintf(exp,"%s\t%s\t%s\n",str,str1,str2); }
}while(!feof(source));
fclose(source);
}
CONCLUSION:
Thus two pass macro processor is implemented in C language.
Ex. No. 10
SINGLE PASS MACRO PROCESSOR
Date:
CONCLUSION:
Thus a single pass macro processor is implemented in C language.
PROGRAM:
/* C Program to implement SINGLE PASS MACRO PROCESSOR */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{int n,flag,i;
char ilab[20],iopd[20],oper[20],NAMTAB[20][20];
FILE *fp1,*fp2,*DEFTAB;
clrscr();
fp1=fopen("macroin.dat","r");
fp2=fopen("macroout.dat","w");
n=0;
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{if(strcmp(iopd,"MACRO")==0)
{
strcpy(NAMTAB[n],ilab);
DEFTAB=fopen(NAMTAB[n],"w");
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"MEND")!=0)
{
fprintf(DEFTAB,"%s\t%s\t%s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}fclose(DEFTAB); n++; }else {
flag=0;
for(i=0;i<n;i++)
{
if(strcmp(iopd,NAMTAB[i])==0) {
flag=1;
DEFTAB=fopen(NAMTAB[i],"r");
fscanf(DEFTAB,"%s%s%s\n",ilab,iopd,oper);
while(!feof(DEFTAB))
{
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper); fscanf(DEFTAB,"%s%s%s",ilab,iopd,oper);
}break; } }
if(flag==0)
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper); }fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fprintf(fp2,"%s\t%s\t%s\n",ilab,iopd,oper);
getch();
}
MACROIN.DAT M1
MACRO ** **
LDA N1 **
ADD N2 **
STA N3 **
MEND ** M2
MACRO ** **
LDA N1 **
SUB N2 **
STA N4 **
MEND ** M3
MACRO ** **
LDA N1 **
MUL N2 **
STA N5 **
MEND ** **
START 1000 **
M3 ** **
M2 ** **
M1 ** **
END **
Ex. No. 11
ABSOLUTE LOADER
Date:
2. Transfer Card:- It contains the entry point of the program, which where the loader is to transfer
control when all instruction are loaded.
1 0 -
INITIALIZE
READ CARD
Set CURLOC to
Address Field
Type=0 Type=1
Card
Type
Transfer to location
Set LNG to COUNT
CURLOC
field
ALGORITHM:
1. Read the Header record
2. Verify program name and length
3. Read first Text record from the input file
DEPT. OF COMPUTER ENGINEERING
SARASWATI EDUCATION SOCIETY’S
CONCLUSION:
Thus an Absolute loader is implemented in C language
PROGRAM:
/* C Program to implement ABSOLUTE LOADER */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char input[10];
int start,length,address;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("input.dat","r");
fp2=fopen("output.dat","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();
}
INPUT.DAT: H 1000 232
T 1000 142033 483039 102036
T 2000 298300 230000 282030 302015
Ex. No. 12
A Lexical Analyzer Generator.
Date:
THEORY:
Lex is a program generator designed for lexical processing of character input streams. It accepts
a high level, problem-oriented specification for character string matching and produces a program in a
general-purpose language which recognizes regular expressions. The user in the source specifications
given to lex specifies the regular expressions. The Lex written code recognizes these expressions in an
input stream and partitions the input stream into strings matching the expressions. At the boundaries
between strings program sections provided by the user are executed. The Lex source file associates the
regular expressions and the program fragments. As each expression appears in the input to the program
written by lex , the corresponding fragment is executed.
Lex turns the user’s expressions and actions (called source in this memo) into the host general purpose
language; the generated program is named yylex. The yylex program will recognize expressions in a
stream ( called input in this memo) and perform the specified actions for each expression as it is detected.
+--------------+
+-------------+
+------------+
+ ----------- + An
Working of LEX :
Summary of Source Format.
%%
{ user subroutines}
%{
code
%}
4) Start conditions, given in the form
%S name1 name2………..
5) Character set tables , in the form
%T
number space character-string
……
%T
6) Changes to internal array sizes, in the form
%x num
Where num is a decimal integer representing an array size and x selects the parameter as follows.
Letter Parameter P Positions n States
e tree nodes
a transitions
k packed character classes ooutput array size
Lines in the rules section have the form “ expression action “ where the action may be continued on
succeeding lines by using braces to delimit it.
Regular expression in Lex use the following operators. x the character “x”
“x” an “x” , even if x is an operator
\x an “x”, even if x is an operator. [xy] the character x or y
[x-z] the characters x, y or z
[ ^x] any character but x.
. any character but newline
^x an x at the beginning of a line.
<y>x an x when lex is in start condition y. x$ an x at the end of a line.
x? an optional x.
x* 0,1,2,……..instances of x. x+ 1,2,3,…….. instances of x.
x| y an x or a y.
(x) an x
x/ y an x but only if followed by y
DEPT. OF COMPUTER ENGINEERING
SARASWATI EDUCATION SOCIETY’S
CONCLUSION: