SPCC lab manual 8-2-21 - 10 experiments on SPCC PDF

Title SPCC lab manual 8-2-21 - 10 experiments on SPCC
Course System Programming & Complier Construction
Institution University of Mumbai
Pages 78
File Size 1.3 MB
File Type PDF
Total Downloads 549
Total Views 841

Summary

Mahatma Education Society’sPillai HOC College of Engineering and Technology, Rasayani Department of Computer EngineeringPractical List Subject: System Programming & Compiler Construction Semester: VISr. No.Name of the Experiment Page No.1 Implementation of the Pass-I of the assembler to generate...


Description

System Programming and Compiler Construction Lab Manual

Mahatma Education Society’s Pillai HOC College of Engineering and Technology, Rasayani Department of Computer Engineering Practical List __________________________________________________________ Subject: System Programming & Compiler Construction Semester: VI Sr. No.

Name of the Experiment

1

Implementation of the Pass-I of the assembler to generate SYMTAB from assembly language program

Write Implementation of the Pass-I of the assembler to generate LITTAB and 2 POOLTAB from assembly language program 3

Implementation of the Pass-II of the assembler produces a target program which is in the machine language of the target computer.

4

To study & implementation of the macro expansion

5

To Study & understand different schemes of loader

6

Write a program in C/Cpp to identify keywords, identifiers and special symbols in a given program.

7

To implement a program for computation of FIRST() function.

8

To implement a LL(1) parser in C language.

9

Study and implement Lex & Yacc tool

10

To implement a C program to generate a three address code for a given expression.

11

To implement a Symbol table with functions to create, insert, modify, search and display in C language.

12

To implement a TEXT Editor with features like create, append, and delete.

PHCET - T.E. (Comp.)Page

Page No.

System Programming and Compiler Construction Lab Manual

H/W Requirements

RAM 512 MB, Printer, Cartridges

S/W Requirements

Turbo c, Lex/Flex, Yacc

EXPERIMENT NO: 1 Name of the Student:-____________________________________________________________ Roll No.____________ Date of Practical Performed:-___________ Staff Signature with Date & Marks

Aim : Implementation of the Pass-I of the assembler to generate SYMTAB from assembly language program. Theory: Tasks performed by the Pass-I are 1. Separate the symbol table, mnemonic table, and operand fields. 2. Build the symbol table. 3. Perform LC Processing. 4. Construct Intermediate Representation (IR). Pass-I performs the analysis of the Source Program and synthesize the IR. Pass-I uses the following data structures: i)

OPTAB= A table of mnemonic opcodes and related information.

ii)

SYMTAB= Table of Symbols used in the program.

iii)

LITTAB= A table of Literals used in the program.

iv)

POOLTAB= Table containing the information about the LITTAB. In this experiment we will generate only the SYMTAB for the assembly language program. SYMTAB: ● An entry in the SYMTAB contains the fields symbol, address & length. ● While processing an assembly statement if the symbol is found, the symbol & address

contained in location counter LC is copied in to a new entry of SYMTAB. PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

● In the start of the assembly program an address is specified, this address is assigned to

LC to keep track of each and every instruction. Input: An assembly Language Program: START NEXT: MOVER

501 AREG,A

PRINT

ONE

A

DS

3

ONE

DC

'1'

F

EQU

200

END The assembly language program given above contains four symbols as NEXT, A, ONE, F. Input file: MEM_TAB.txt MOVER IS (04,3) ADD IS (01,3) PRINT IS (10,3) READ IS (09,3)

Input program: T.txt START 501 NEXT:MOVER AREG,A PRINT ONE A DS 3 ONE DC '1' F EQU 200 END and final CPP program: /* Symbol tabel genration*/ #include #include #include #include #include # define MAX 80 # define SI 30 PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

class sym { char name[SI]; int add,len; public: void set(char [],int ,int); void set_symbol(char []); int msearch(char []); int search(char []); int get_addr(char []); void modify(char [],int ,int); void display(); }s; void sym::display() { fstream f; f.open("s.txt",ios::in|ios::beg); coutlen=-1; f.write((char *)&(*this),sizeof(*this)); f.close(); } void main() { char str[SI],str1[SI],str2[SI],strc[SI],strm[SI],strv[SI]; fstream f; int lc,flag=0,c,flag1,size,flag2; clrscr(); PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

f.open("t.txt",ios::beg|ios::in); while(f.getline(str,MAX)) { strcpy(strc,str); if(flag==0) { strcpy(str1,strtok(str," ")); if(strcmp(str1,"START")==0) { strcpy(str1,strtok(NULL," ")); lc=atoi(str1); flag=1; } } else { strcpy(str,strc); if(strstr(str,":")) { strcpy(str1,strtok(str,":")); s.set(str1,lc,1); strcpy(strc,strtok(NULL,":")); } strcpy(str,strc); if(strstr(str,",")) { strcpy(strm,strtok(str," ")); strcpy(str1,strtok(NULL," ")); strcpy(str1,strtok(str1,",")); strcpy(str1,strtok(NULL,",")); if(!strstr(str1,"=")) { s.set_symbol(str1); } c=s.msearch(strm); lc=lc+c; } else { strcpy(str,strc); strcpy(strm,strtok(str," ")); if(strcmp(strm,"PRINT")==0||strcmp(strm,"READ")==0) PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

{ strcpy(str1,strtok(NULL," ")); s.set_symbol(str1); c=s.msearch(strm); lc=lc+c; } else if(strcmp(strm,"ORIGIN")==0) { strcpy(str1,strtok(NULL," ")); flag1=s.search(str1); if(flag1==1) { lc=atoi(str1); } else { lc=s.get_addr(str1); } } strcpy(str,strc); strcpy(strv,strtok(str," ")); strcpy(str1,strtok(NULL," ")); if(strcmp(str1,"DS")==0) { strcpy(str1,strtok(NULL," ")); size=atoi(str1); flag1=s.search(strv); if(flag1==1) { s.set(strv,lc,size); } else s.modify(strv,lc,size); lc=lc+size; } if(strcmp(str1,"DC")==0) { strcpy(str,strc); strcpy(str1,strtok(str,"'")); strcpy(str1,strtok(NULL,"'")); strcpy(str1,strtok(str1,"'")); PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

size=atoi(str1); flag1=s.search(strv); if(flag1==1) { s.set(strv,lc,size); } else s.modify(strv,lc,size); lc++; } if(strcmp(str1,"EQU")==0) { strcpy(str1,strtok(NULL," ")); flag2=s.search(str1); if(flag2==1) { size=atoi(str1); flag1=s.search(strv); if(flag1==1) { s.set(strv,size,1); } else s.modify(strv,size,1); } else { size=s.get_addr(str1); flag1=s.search(strv); if(flag1==1) { s.set(strv,size,1); } else s.modify(strv,size,1); } } } }//else complete if(strcmp(str,"END")==0) break; PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

}//while complete f.close(); s.display(); getch(); } EXPERIMENT NO: 2 Name of the Student:-____________________________________________________________ Roll No.____________ Date of Practical Performed:-___________ Staff Signature with Date & Marks

Aim : Implementation of the Pass-I of the assembler to generate LITTAB and POOLTAB from assembly language program. Theory: Tasks performed by the Pass-I are 1. 2. 3. 4.

Separate the symbol table, mnemonic table, and operand fields. Build the symbol table. Perform LC Processing. Construct Intermediate Representation (IR).

Pass-I performs the analysis of the Source Program and synthesize the IR. Pass-I uses the following data structures: i. ii. iii. iv.

OPTAB= A table of mnemonic opcodes and related information. SYMTAB= Table of Symbols used in the program. LITTAB= A table of Literals used in the program. POOLTAB= Table containing the information about the LITTAB.

In this experiment we will generate only the SYMTAB for the assembly language program.

LITTAB and POOLTAB: ● Literals are entered in the LITTAB in the sequential manner, along with two fields first

one is literal and second is address of that literal. ● An entry in the POOLTAB indicates the number of pools of literals present in the input assembly language program. ● It contains only one field i.e. literal number. Literal number indicates which entry in LITTAB contains the first literal of the pool. PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

● In the assembly language program given below, there are two pools, the first one is

before the LTORG statement and second is after LTORG statement to END statement. ● Pool one is having two literals =’5’ & =’2’ and again pool two is having two literals as

=’9’ and =’1’. Hence LITTAB will have total four entries. ● Here both pools contains 2 literals, and hence POOLTAB will have the entries as 0,2 and

4.

Let’s see an example:

Input: An assembly Language Program: START 501 MOVER AREG, ='5' ADD AREG,='2' LTORG MOVER AREG,='9' ADD AREG,='1' END Output: LITTAB and POOLTAB

1. Explain the process of entering the literals in the LITTTAB 2. What is the pool? Explain in detail. 3. Explain the POOLTAB contents. 4. Explain the essence of the POOLTAB.

CODE: INPUT FILES::

PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

L1.txt START 501 MOVER AREG,='5' ADD AREG,='2' LTORG MOVER AREG,='9' ADD AREG,='1' END MEM_TAB.txt MOVER IS (04,3) ADD IS (01,3) PRINT IS (10,3) READ IS (09,3) MAIN PROGRAM::

//creation of Literal Tabel #include #include #include #include #include # define MAX 80 # define SI 30 class lit { char name[SI]; int add; public: void set_lit(char []); int msearch(char[]); int modify(int ,int); void display(); }l; void lit::display() { fstream f; coutaα is a production, then add a to FIRST(X). if X->€ to FIRST(X) 3. if -> Y1,Y2,…….Yk is a production, then for all i such that all of Y1,….Yi-1 are Non terminals and FIRST (Yj) contains € for j=1, 2,…………. i-1, add every non-€ symbol in FIRST(Y1) to FIRST(x). if V is in FIRST(Yj) for j=1,2,………k, then add € to FIRST(X) Consider the following grammar of example E->TE’ E’->+TE’ | ε T->FT’ T’->*FT’ | ε F->(E) | id Thus, the FIRST set for all the non-terminals are summarized below. FIRST(E)={(,id} FIRST(T)={(,id} FIRST(F)={(,id} FIRST(E’)={+,ϵ} FIRST(E’)={*,ϵ}

PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

Conclusion: Questions: 1. What is FIRST ? 2. What is FOLLOW ? 3. Explain FIRST rule. 4. Explain Follow rule. 5. S -> AB A -> Ca | ϵϵ B -> cB' B' -> aACB' | ϵϵ C -> b | ϵ What is the output for the above problem?

EXPERIMENT NO: 8 Name of the Student:-__________________________________________________ Roll No.____________

PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

Date of Practical Performed:-___________ Staff Signature with Date & Marks

Aim: To implement a LL(1) parser in C language. Theory: What is Parsing? : A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into another language. A parser takes input in the form of a sequence of tokens or program instructions and usually builds a data structure in the form of a parse tree or an abstract syntax tree. In the compiler model, the parser obtains a string of tokens from the lexical analyzer, and verifies that the string can be generated by the grammar for the source language. The parser returns any syntax error for the source language. It collects sufficient number of tokens and builds a parse tree. LL(1) Parser: Predictive parsers can be constructed for LL(1) grammar, the first ‘L’ stands for scanning the input from left to right, the second ‘L’ stands for leftmost derivation and ‘1’ for using one input symbol look ahead at each step to make parsing action decisions. A grammar G is LL(1) if A → α | β are two distinct productions of G. For no terminal, both α and β derive strings beginning with a. At most one of α and β can derive empty string. If β → t, then α does not derive any string beginning with a terminal in FOLLOW(A). ALGORITHM: 1. Open the input file in read format. 2. Read each line until the end of file. 3. Separate the expression and result. 4. Separate operation and operator from the expression. 5. Print the result. 6. Close the file. PROGRAM: #include #include #include void main() PHCET - T.E. (Comp.)Page

System Programming and Compiler Construction Lab Manual

{ char fin[10][20],st[10][20],ft[20][20],fol[20][20]; int a=0,e,i,t,b,c,n,k,l=0,j,s,m,p; clrscr(); printf("enter the no. of coordinates\n"); scanf("%d",&n); printf("enter the productions in a grammar\n"); for(i=0;i...


Similar Free PDFs