0% found this document useful (0 votes)
18 views51 pages

For Stud

The document outlines a lab course on System Programming for TE-IT students, detailing various experiments related to assembler design, macro processors, and lexical analysis. It includes guidelines for conducting experiments, programming tasks, and the necessary software and hardware requirements. Additionally, it provides a structured approach to learning through assignments, objectives, and expected outcomes, along with error handling and data structures involved in the assembly process.

Uploaded by

Mahesh Wagh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views51 pages

For Stud

The document outlines a lab course on System Programming for TE-IT students, detailing various experiments related to assembler design, macro processors, and lexical analysis. It includes guidelines for conducting experiments, programming tasks, and the necessary software and hardware requirements. Additionally, it provides a structured approach to learning through assignments, objectives, and expected outcomes, along with error handling and data structures involved in the assembly process.

Uploaded by

Mahesh Wagh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 51

System Programming TE-IT

INDEX

Marks
Sr. Date of Date of Signature
Title of Experiment Obtained
No performance Submission of Faculty
(10)
Write a program to implement Pass-I of
Two-pass assembler for Symbols and
Literal processing (For hypothetical
instruction set from Dhamdhere)
considering following cases
1
i. Forward references
i. DS and DC statement
iii. START, EQU, LTORG, END.
iv. Error handling: symbol used but not
defined, invalid instruction/register etc.
Write a program to implement Pass-II of
Two-pass assembler for output of
2
Assignment 1 (The subject teacher should
provide input file for this assignment )
Study Assignment for Macro Processor.
3
(Consider all aspects of Macro Processor )
Write a program to implement Lexical
4
Analyzer for subset of C.
Write a program to implement a Recursive
5 Descent Parser .
Write a program to implement calculator
6 using LEX and YACC.
Write a program for Intermediate code
generation using LEX &YACC for Control
7
Flow statement ( Either While loop or
Switch case)

Name & Signature of Course In-charge

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

LAB INNOVATION

Note: Student are expected to do minimum one lab innovation

1. Implement lexical analyzer using LEX and YAAC

2. Implement table driven predictive parser

3. Implement operator precedence parser

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Procedure/Guidelines for Conducting Experiment

How to Write and Run a C Program in Linux

We will be using the Linux command line tool, the Terminal, in order to compile a simple C
program. To open the Terminal, you can use the Ubuntu Dash or the Ctrl+Alt+T shortcut.

Step 1: Install the build-essential packages

In order to compile and execute a C program, you need to have the essential packages installed on
your system. Enter the following command as root in your Linux Terminal:

$ sudo apt-get install build-essential

You will be asked to enter the password for root; the installation process will begin after that.
Please make sure that you are connected to the internet.

Step 2: Write a simple C program

After installing the essential packages, let us write a simple C program.


Open Ubuntu’s graphical Text Editor and write or copy the following sample program into it:

#include<stdio.h>

int main()
{
printf("\nA sample C program\n\n");
return 0;
}
Then save the file with .c extension. In this example, I am naming my C program as
sampleProgram.c

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Alternatively, you can write the C program through the Terminal in gedit as follows:
$ gedit sampleProgram.c
This will create a .c file where you can write and save a program.

Step 3: Compile the C program with gcc

In your Terminal, enter the following command in order to make an executable version of the
program you have written:
Syntax:
$ gcc [programName].c -o programName
Example:
$ gcc sampleProgram.c -o sampleProgram

Make sure your program is located in your Home folder. Otherwise, you will need to specify
appropriate paths in this command.

Step 4: Run the program

The final step is to run the compiled C program. Use the following syntax to do so:
$ ./programName
Example:
$ ./sampleProgram

You can see how the program is executed in the above example, displaying the text we wrote to
print through it.

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Name of the Student: ______________________________________ Roll no: ___

CLASS: - T.E [IT] Division:_______ Course: - SL-V

Experiment No. 1
**Write a program to implement Pass-I of Two-pass assembler for Symbols and literal processing**

Marks: / 10
Date of Performance: Sign with Date
/ /20

Aim: Write a program to implement Pass-I of Two-pass assembler for Symbols and Literal
processing (For hypothetical instruction set from Dhamdhere) considering following cases
i. Forward references
ii. DS and DC statement
iii. START, EQU, LTORG, END.
iv. Error handling: symbol used but not defined, invalid instruction/register etc.

Objectives:
1. To study basic translation process of assembly language to machine language.
2. To study two pass assembly process

Outcomes:

At the end of the assignment the students should have


1. Understood the design and implementation of pass I of assembler

PEOs, POs, PSOs and COs satisfied

POs: i PEOs: I PSOs: 2 COs: 1

Infrastructure: Desktop/ laptop system with Linux or its derivatives.

Software used: LINUX/ Windows OS/ Virtual Machine/ IOS

Theory:
A language translator bridges an execution gap to machine language of computer system.
An assembler is a language translator whose source language is assembly language.
Language processing activity consists of two phases, Analysis phase and synthesis phase.
Analysis of source program consists of three components, Lexical rules, syntax rules and semantic
rules. Lexical rules govern the formation of valid statements in source language. Semantic rules

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

associate the formation meaning with valid statements of language. Synthesis phase is concerned
with construction of target language statements, which have the same meaning as source language
statements. This consists of memory allocation and code generation.

Data structures

Pass I Pass II
Intermediate code
Data access

Control transfer

Over view of two pass assembly

Analysis of source program statements may not be immediately followed by synthesis of


equivalent target statements. This is due to forward references issue concerning memory
requirements and organization of Language Processor (LP).
Forward reference of a program entity is a reference to the entity, which precedes its
definition in the program. While processing a statement containing a forward reference, language
processor does not possess all relevant information concerning referenced entity. This creates
difficulties in synthesizing the equivalent target statements. This problem can be solved by
Postponing the generation of target code until more information concerning the entity is available.
This also reduces memory requirements of LP and simplifies its organization. This leads to multi-
pass model of language processing.

Language Processor Pass: -


It is the processing of every statement in a source program or its equivalent representation to
perform language-processing function.

Assembly Language statements: -


There are three types of statements Imperative, Declarative, Assembly directives.
1. An imperative statement indicates an action to be performed during the execution of
assembled program. Each imperative statement usually translates into one machine
instruction.
2. Declarative statement e.g. DS reserves areas of memory and associates names with them.
3. DC constructs memory word containing constants. Assembler directives instruct the
assembler to perform certain actions during assembly of a program, e.g. START<constant>
directive indicates that the first word of the target program generated by assembler should

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

be placed at memory word with address <constant>

Function of Analysis and Synthesis

1. Isolate the label operation code and operand fields of a statement.


2. Enter the symbol found in label field (if any) and address of next available machine
word into symbol table.
3. Validate the mnemonic operation code by looking it up in the mnemonics table.
4. Determine the machine storage requirements of the statement by considering the
mnemonic operation code and operand fields of the statement.
Calculate the address of the address of the first machine word following the target code
generated for this statement (Location Counter Processing)

Synthesis Phase:
1. Obtain the machine operation code corresponding to the mnemonic operation code by
searching the mnemonic table.
2. Obtain the address of the operand from the symbol table.
3. Synthesize the machine instruction or the machine form of the constant as the case may
be.

Design of a Two Pass Assembler: -


Tasks performed by the passes of two-pass assembler are as follows:
Pass I: -
Separate the symbol, mnemonic opcode and operand fields.

Determine the storage-required for every assembly language statement and update the
location counter.

Build the symbol table and the literal table.

Construct the intermediate code for every assembly language statement.

Pass II: -

Synthesize the target code by processing the intermediate code generated during

Data structures required for pass I:


1. Source file containing assembly program.

2. MOT: A table of mnemonic op-codes and related information. It has the following
fields

Mnemonic: Such as ADD, END, DC


TYPE: IS for imperative, DL for declarative and AD for Assembler directive

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

OP- code: Operation code indicating the operation to be performed.


Length: Length of instruction required for Location Counter Processing

Index Mnemonic TYPE OP-Code Length

0 ADD IS 01 01
1 BC IS 07 01
2 COMP IS 06 01
3 DIV IS 08 01
4 EQU AD 03 -
5 DC DL 01 -
6 DS DL 02 -
7 END AD 05 -

3. SYMTB: The symbol table.

Fields are Symbol name, Address (LC Value). Initialize all values in the address fields to -1
and when symbol gets added when it appears in label field replace address value with
current LC. The symbol if it used but not defined will have address value -1 which will be
used for error detection.

Symbol Address
Loop 204
Next 214

4. LITTAB and POOLTAB: Literal table stores the literals used in the program and
POOLTAB stores the pointers to the literals in the current literal pool.

Data Structure used by Pass II:


1. OPTAB: A table of mnemonic opcodes and related information.
2. SYMTAB: The symbol table
3. LITTAB: A table of literals used in the program
4. Intermediate code generated by Pass I
5. Output file containing Target code / error listing.

Instruction Opcode Assembly mnemonic Remarks


00 STOP stop execution
01 ADD first operand modified condition code
set
02 SUB first operand modified condition code
set
03 MULT first operand modified condition code

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

set
04 MOVER Memory to register
05 MOVEM Register to memory
06 COMP sets condition code
07 BC branch on condition code
08 DIV analogous to SUB
09 READ First operand is not used.
10 PRINT First operand is not used.

Sample Input & Output: -

SAMPLE INPUT SAMPLE OUTPUT FILE OF


FILE
INTERMEDIATE CODE
START
101
101) (IS,09) (0)
READ N (S,1)
102) (IS, 04)( 2)
MOVER BREG ONE S,2)
103) (IS, 05)( 2)
MOVEM BREG TERM (S,4)
AGAIN MULT BREG TERM 104) + 03 2 (S,4)
MOVER CREG TERM 105) + 04 3 (S,4)
ADD CREG ONE 106) + 01 3 (S,2)
MOVEM CREG TERM 107) + 05 3 (S.4)
COMP CREG N 108) + 06 3 (S,1)
BC LE AGAIN 109) + 07 2(S,3)
MOVEM BREG RESULT 110) + 05 2 (S,5)
PRINT RESULT 111) + 10 0 (S,5)
STOP 112) (IS,0)-- ---
113)
N DS 1 (DL,02)_(C,001)
114)
RESULT DS 1 (DL,02)_(C,001)
115)
ONE DC „1‟ (DL,01)_(C,001)
116)
TERM DS 1 (DL,02)_(C,001)
END

SYMBOL TABLE

Index Symbol Address

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

1 N 113
2 ONE 115
3 TERM 116
4 AGAIN 104
5 RESULT 114

SAMPLE CODE IC
MOVER AREG, = “5” 200) (IS,04) (1)( L,1)
MOVER BREG, = “1” 201) (IS,04)(2)(L,2)
ADD CREG, = “1” 202) (IS,01)(3)(L,2)
LTORG
203) (DL,01)__(C,005)
204) (DL,01)__(C,001)
SUB AREG, = “1” 205) (IS,02)(1)(L,03)
ORIGIN 400
MULT AREG, = “1” 400) (IS,04)(1)(L,02)
END 401)(DL,01)__(C,001)

Pool Literal Table


Table Index LITERAL
#1
#3 1 5
- 2 1
3 1

Errors: -

1. Forward reference (Symbol used but not defined): -


This error occurs when some symbol is used but it is not defined into the program.

2. Duplication of Symbol: -
This error occurs when some symbol is declared more than once in the program.

3. Mnemonic error:
If there is invalid instruction then this error will occur.

4. Register error: -
If there is invalid register then this error will occur.

5. Operand error: -
This error will occur when there is an error in the operand field

Conclusion: Thus we have designed and implemented pass 1of a 2 pass assembler in C.
Write short answer of following questions : (4-5 Max)

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

1. Explain what is meant by pass of an assembler.


2. Explain the need for two pass assembler.
3. Explain terms such as Forward Reference and backward reference.
4. Explain various types of errors that are handled in two different passes.
5. Explain the need of Intermediate Code generation and the variants used.

A. Viva Questions: (8-10)

1. What are three types of assembly language statements?


2. List the features of the assemblers?
3. What is the use of location counter in assembler?
4. What is the use of symbol table in assembler?
5. What are the formats of literal table, pool table, symbol table used in assembler?
6. Define system software.
7. List types of software system.
8. Define Loader, Assembler, Microprocessor, Compiler, and Interpreter.
9. List down assembler directives.
10. What is syntax and function of EQU and ORIGIN statement? What is the limitation/constraint
of these statements?

 Mapping of CO, PO and PSO


Note : enter Correlation levels in the box, 1. slight(Low); 2: Moderate(medium); 3.
Substantial ( High), If No correlation, Put “-”

Course Outcome (CO):

S Name of Experiment CO1 CO2 CO3


No
01 Implementation of Pass I 3 ---- ----
assembler

Program Outcomes (PO):

S Name of PO PO PO PO PO PO PO PO PO P P P
N Experiment 1 2 3 4 5 6 7 8 9 O O O
o 10 11 12
01
Implementatio  
n of Pass I
Assembler

Program Specific Outcomes (PSOs):

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

S No Name of Experiment PSO1 PSO2


01 Implementation of Pass I Assembler  

CO and PO Mapping:

COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
/POs
CO1 

CO2
CO3

CO and PSO Mapping:

COs PSO1 PSO2 PSO3 PSO4 PSO5


/PSOs
CO1 
CO2
CO3

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Name of the Student: ______________________________________ Roll no: ___

CLASS: - T.E [IT] Division:_______ Course: - SL-V

Experiment No. 2
** Write a program to implement Pass-II of Two-pass assembler for output of Assignment 1**

Marks: / 10
Date of Performance: Sign with Date
/ /20

Aim:- Write a program to implement Pass-II of Two-pass assembler for output of Assignment 1
(The subject teacher should provide input file for this assignment)

Objectives:

1. To study basic translation process of assembly language to machine language.


2. To study two pass assembly process

Outcomes:
At the end of the assignment the students should have
1. Understood the design and implementation of pass II of assembler

PEOs, POs, PSOs and COs satisfied

POs: i PEOs: I PSOs: 2 Cos: 1

Infrastructure: Desktop/ laptop system with Linux or its derivatives.

Software used: LINUX/ Windows OS/ Virtual Machine/ IOS

Theory: -

Data structures required for pass I:


1. Source file containing assembly program.
2. MOT: A table of mnemonic op-codes and related information. It has the following fields
3. Mnemonic : Such as ADD, END, DC
4. TYPE : IS for imperative, DL for declarative and AD for Assembler directive
5. OPcode: Operation code indicating the operation to be performed.
6. Length : Length of instruction required for Location Counter Processing
7. LITTAB and POOLTAB: Literal table stores the literals used in the program and

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

POOLTAB stores the pointers to the literals in the current literal pool.
Data Structure used by Pass II:
1. OPTAB: A table of mnemonic opcodes and related information.
2. SYMTAB: The symbol table
3. LITTAB: A table of literals used in the program
4. Intermediate code generated by Pass I
5. Output files containing Target code / error listing.

Instruction Opcode Assembly mnemonic Remarks


00 STOP stop execution
01 ADD first operand modified condition code
set
02 SUB first operand modified condition code
set
03 MULT first operand modified condition code
set
04 MOVER Register to Memory
05 MOVEM Memory to Register
06 COMP sets condition code
07 BC branch on condition code
08 DIV analogous to SUB
09 READ first operand is not used.
10 PRINT first operand is not used.

SAMPLE INPUT FILE SAMPLE OUTPUT FILE

START
101
READ N 101) (IS,09) (0) (S,1) 101) + 09 0 113
MOVER BREG ONE 102) (IS, 04)( 2) S,2) 102) + 04 2 115
MOVEM BREG TERM 103) (IS, 05)( 2)(S,4) 103) + 05 2 116
AGAIN MULT BREG TERM 104) + 03 2 (S,4) 104) + 03 2 116
MOVER CREG TERM 105) + 04 3 (S,4) 105) + 04 3 116
ADD CREG ONE 106) + 01 3 (S,2) 106) + 01 3 115
MOVEM CREG TERM 107) + 05 3 (S.4) 107) + 05 3 116
COMP CREG N 108) + 06 3 (S,1) 108) + 06 3 113
BC LE AGAIN 109) + 07 2(S,3) 109) + 07 2 104
RESUL
MOVEM BREG T 110) + 05 2 (S,5) 110) + 05 2 114
PRINT RESULT 111) + 10 0 (S,5) 111) + 10 0 114
STOP 112) (IS,0)-- --- 112) + 00 0 000
N DS 1 113)(DL,02)_(C,001) 113)
RESULT DS 1 114) (DL,02)_(C,001) 114)
ONE DC „1‟ 115) (DL,01)_(C,001) 115) +01 0 001
TERM DS 1 116) (DL,02)_(C,001) 116)

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

END

SAMPLE CODE IC Machine Code

MOVER AREG, = “5” 200) (IS,04) (1)( L,1) 200) + 04 01 203


MOVER BREG, = “1” 201) (IS,04)(2)(L,2) 201) + 04 02 204
ADD CREG, = “1” 202) (IS,01)(3)(L,2) 202) + 01 03 204
LTORG
203) (DL,01)__(C,005) 203) + 00 00 005
204) (DL,01)__(C,001) 204) + 00 00 001
SUB AREG, = “1” 205) (IS,02)(1)(L,03) 205) + 04 01 203
ORIGIN 400
MULT AREG, = “1” 400) (IS,04)(1)(L,02) 400) + 04 01 401
END 401)(DL,01)__(C,001) 401)+01—001

Pool Literal Table


Table Index LITERAL
#1
#3 1 5
- 2 1
3 1

Conclusion: Thus we have designed and implemented a pass 2 of a 2 pass assembler in C.

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Write short answer of following questions : (4-5 Max)


1. What are DS required for generation of machine code?
2. What is synthesis phase
3. What type of error is recognized by pass 2 of a 2 pass assembler?
4. Intermediate file contains which type of record
5. How is literal table useful?

B. Viva Questions: (8-10)

1. What does an assembler perform when it encounters LTORG assembler directive?


2. What is a common use of ORG?
3. Which data structures are used in assembler?
4. State various tables used and their significance in the design of two pass Assembler
5. What are the functions of Pass II of two pass assembler
6. Which are the phases of language processor?
7. All symbols in a ALP are forward referenced. True or False?
8. All literals in a ALP are forward referenced. True or False?
9. Is a macro open or closed sub routine

Mapping of CO, PO and PSO


Note : enter Correlation levels in the box, 1. slight(Low); 2: Moderate(medium); 3.
Substantial ( High), If No correlation, Put “-”

Course Outcome (CO):

S Name of Experiment CO1 CO2 CO3


No
02 Implementation of PASS 
II assembler

Program Outcomes (PO):

S Name of PO PO PO PO PO PO PO PO PO PO1 PO1 PO1


N Experiment 1 2 3 4 5 6 7 8 9 0 1 2
o
01 Implementati 
on of PASS
II assembler

Program Specific Outcomes (PSOs):

S Name of Experiment PSO1 PSO2 POS3 POS4 POS5


No

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

01 Implementation of PASS II 
assembler

CO and PO Mapping:

COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
/POs
CO1 

CO2
CO3

CO and PSO Mapping:

COs PSO1 PSO2 PSO3 PSO4 PSO5


/PSOs
CO1 
CO2
CO3

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Name of the Student: ______________________________________ Roll no: ___

CLASS: - T.E [IT] Division:_______ Course: - SL-V

Experiment No. 3

** Study Assignment for Macro Processor **

Marks: / 10
Date of Performance: Sign with Date
/ /20

Aim : Study Assignment for Macro Processor

Objective: -
1. To understand macro facility, features and its use in assembly language programming.
2. To study how the macro definition is processed and how macro call results in the expansion
of code.

Outcomes:
At the end of the assignment the students should have
1. Understood macro facility, features and its use in assembly language
programming
2. Understood macro definition processing and macro call results in the expansion
of code

PEOs, POs, PSOs and COs satisfied :

POs: i PEOs: I PSOs: 2 Cos: 2

Infrastructure: Desktop/ laptop system with Linux or its derivatives.

Software used: LINUX/ Windows OS/ Virtual Machine/ IOS

Theory:
An assembly language macro facility is to extend the set of operations provided in an
assembly language.

In order that programmers can repeat identical parts of their program macro facility can be
used. This permits the programmer to define an abbreviation for a part of program & use

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

this abbreviation in the program. This abbreviation is treated as macro definition & saved by
the macro processor. For all occurrences the abbreviation i.e. macro call, macro processor
substitutes the definition.

Macro definition part:


It consists of
1. Macro Prototype Statement - this declares the name of macro & types of parameters.
2. Model statement - It is statement from which assembly language statement is generated
during macro expansion.
3. Preprocessor Statement - It is used to perform auxiliary function during macro
expansion.

Macro Call & Expansion:


The operation defined by a macro can be used by writing a macro name in the mnemonic
field and its operand in the operand field. Appearance of the macro name in the mnemonic
field leads to a macro call. Macro call replaces such statements by sequence of statement
comprising the macro. This is known as macro expansion.

Macro Facilities:
1. Use of AIF & AGO allows us alter the flow of control during expansion.
2. Loops can be implemented using expansion time variables.

Design Procedure:
1. Step 1 is definition processing - Scan all macro definitions and for each macro definition
enter the macro name in macro name table (MNT). Store entire macro definition in macro
definition table (MDT) and add auxiliary information in MNT such as no of positional
parameters (#PP) no of key word parameters (#KP), macro definition table position
(MDTP) etc.
2. Step 2 is macro expansion - Examine all statement in assembly source program to detect the
macro calls. For each macro call locate the macro in MNT, retrieve MDTP, establish the
correspondence between formal & actual parameters and expand the macro.

Data structures required for macro definition processing –

1. Macro Name Table [MNT] -


Fields-Name of Macro, #pp (no of positional parameters), # kp( no of keyword parameters), #
ev (no of expansion time variables), MDTP ( Macro Definition Table Pointer), Keyword
Parameters Default Table Position (KPDTP), Sequencing Symbol Table Position (SSTP).
2. Parameter Name Table [PNTAB] - Fields - Parameter Name
3. Expansion Time Variable Name Table [EVNTAB] - Fields – Expansion time variable name
4. Keyword parameter Default Table [KPDTAB] - Fields - Parameter Name, Default value
5. Macro Definition Table [MDT] - Model Statement are stored in the intermediate code from
as: Label, Opcode, Operands.
6. Sequencing Symbol Table Name [SSNTAB] - Fields – Sequencing symbol name.
7. Sequencing Symbol Table [SSTAB] - MDT entry no(#).

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Algorithm for definition processing:


Before processing any definition initialize KPDTAB_ptr, SSTAB_ptr, MDT_ptr to 0 and
MNT_ptr to -1. These table pointers are common to all macro definitions. For each macro
definition perform the following steps.
1. Initialize SSNTAB – ptr, PNTAB – ptr to 0 & fields of MNT, # pp, # kp , #ev to 0 and
increment MNT_ptr by 1.

2. For macro prototype statement from MNT entry.


a. Entry name into name field.
b. For each position parameter field
i. Enter name in parameter name table.
ii. Increment PNTAB – ptr by 1.
iii. Increment # pp by 1.

c. KPDTP  KPDTAB - ptr d.


For each keyword parameter
i. Enter name & default value in KPDTAB.
ii. Increment KPTAB –ptr by 1.
iii. Enter name in PNTAB & increment PNTAB – ptr by 1.
iv. Increment # kp by 1.

e. MDTP MDT - ptr
f. # ev  Zero and SSTP  SSTAB – ptr.

3. Do Begin
Read next statement
a. If label field has sequencing symbol (SS) then,
If SS is already present in SSNTAB, retrieve its index in q.
Else
Begin
Enter SS in SSNTAB q

SSNTAB – ptr
Increment SSNTAB – ptr by one.
End
Store MDT – ptr in SSTAB as STAB [SSTP + q]  MDT – ptr. b. Check type of the
statement

If Model statement then


Begin

i. For parameter generate specification (p, # n).


ii. For expansion variable generate specification (E, # m).
iii. Record intermediate code in MDT.
iv. Increment MDT - ptr by 1.
end

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

c. If Preprocessor Statement –
Begin
i. If SET statement then search each expansion time variable in EVNTAB & generate
(E, # m).
ii. If AIF or AGO then if SS is already present in SSNTAB, retrieve its index in q
Else
Begin
Enter SS in SSNTAB q

SSNTAB – ptr
Increment SSNTAB – ptr by 1
End
Replace symbol by (SS, SSTP+q)

iii) Enter intermediate code in MDT & increment MDT – ptr by one.
End

d. If MEND
Begin
Enter MEND in MDT, increment MDT_ptr by 1.
If SSNTAB is empty i.e.
SSNTAB – ptr = = 0 then SSTP = SSTAB_ptr
Else
SSTAB – ptr = SSTAB – ptr + SSNTAB – ptr.
If #kp == 0 then KPDTP = 0
Return to main logic ie step 6 of main logic.
End.

Data structures required for expansion processing :-


1. Actual parameter table: APTAB
2. Expansion variable table : EVTAB
3. Macro expansion counter : MEC.

Algorithm for macro expansion:


1. Initialization
i. MEC  MDTP from MNT.
ii. Create EVTAB with # EV & set EVTAB ptr accordingly.
iii. Create APTAB with # pp & # kp entries and set APTAB ptr accordingly.
iv. Copy keyword parameter defaults from KPDTAB in APTAB[pp] to APTAB[#pp + #kp
-1].
v. Process actual positional parameters in call copy them in APTAB from 0 to # pp-1.
vi. For keyword parameter specification search name in parameter name field of KPDTAB,
get matching entry in q & enter value in APTAB [ #pp + q – KPDTP ].

2. While Statement pointed by MEC in MDT is not MEND.


i. If LCL statement then MEC++ .

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

ii. If model statement then replace operands of the form (p, #n) & (E, #m) by values in
APTAB & EVTAB respectively.
iii. If set statement with specification (E, #m) in label field then set appropriate value in
EVTAB [m] & increment MEC by one.
iv. If AGO statement with (s, #s) specification in operand field then set new value of MEC

SSTAB[SSTP + s]
v. If AIF statement with (s, #s) specification in operand field & if condition in A/F is true
then
MEC  SSTAB [SSTP +s]
Else

MEC <- MEC+1 3.


Exit from macro expansion

Main Program Logic :

1. Initialize KPDTAB_ptr, SSTAB_ptr, MDT_ptr to 0 and MNT_ptr to -1. These table


pointers are common to all macro definitions
2. Read the statement from source file.
3. Separate the word from that line and count the no of words. Save the separated
words in the array say word which is a string array.
4. If count of words is one then check that word matches with “MACRO”. If it matches
then perform definition processing.
5. If it does not match and if there is no symbol in the label field then check whether
first word matches with any of the entries in the macro name table. (Search the
complete macro name table for presence of macro call), if so then call macro
expansion routine.
6. If conditions in 3 and 4 are not satisfied then enter the line in the expanded code file.
7. Read next statement and if not end of file go to step 3.

Conclusion: Thus we have studied macro processor in C.

Department of Information Technology, SIT, Lonavala


System Programming TE-IT

Write short answer of following questions: (4-5 Max)

1. Define and Distinguish between parameters that can be used in macros.


2. State various tables used in processing the macro.
3. Explain the role of stack in nested macros.
4. What are the three main data structures used by our macro processor?
5. What is the difference between attributes of symbols and of macro arguments?
6. What should be printed in the object code field when a macro definition is listed?

C. Viva Questions: (8-10)

1) Is a macro open or closed sub routine.


2) Does macro processing happen before or after compilation / assembling of a program?
3) List down all macro directives. Do macro directives occur in expanded code?
4) What are the functions of Pass I of two pass macro processor?
5) What are the functions of Pass II of two pass macro processor?
6) Can II pass macro processor handle nested macros?
7) Define the term macro.
8) How many parameters can a macro have?
9) Distinguish between macro and a subroutine
10) What is meant by macro time variable?

 Mapping of CO, PO and PSO


Note: enter Correlation levels in the box, 1. slight(Low); 2: Moderate(medium); 3. Substantial
( High), If No correlation, Put “-”

Course Outcome (CO):

S Name of Experiment CO1 CO2 CO3


No
01 Study of Macro processor 

Program Outcomes (PO):

S Name of PO PO PO PO PO PO PO PO PO PO1 PO1 PO1


No Experimen 1 2 3 4 5 6 7 8 9 0 1 2
t
01 Study of 
Macro
processor

Program Specific Outcomes (PSOs):

0
System Programming TE-IT

S Name of Experiment PSO1 PSO2 POS3 POS4 POS5


No
01 Study of Macro processor 
CO and PO Mapping:

COs /POs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 

CO2
CO3

CO and PSO Mapping:

COs PSO1 PSO2 PSO3 PSO4 PSO5


/PSOs
CO1
CO2 
CO3

1
System Programming TE-IT

Name of the Student: ______________________________________ Roll no: ___

CLASS: - T.E [IT] Division:_______ Course: - SL-V

Experiment No. 4
** Write a program to implement Lexical Analyzer for subset of C**

Marks: / 10
Date of Performance: Sign with Date
/ /20

Aim : Write a program to implement Lexical Analyzer for subset of C

Objective:
To understand the role of Lexical analyzer

Outcomes:
At the end of the assignment the students should have
1. Understood the design and implementation of lexical analyzer

PEOs, POs, PSOs and COs satisfied

POs: i PEOs: I PSOs: 2 Cos: 2,3

Infrastructure: Desktop/ laptop system with Linux or its derivatives.

Software used: LINUX/ Windows OS/ Virtual Machine/ IOS

Input:
1) Source File
2) Terminal Symbol Table

Output:
1) Symbol Table
2) Literal Table
3) Universal Table

Theory:

2
System Programming TE-IT

Lexical analysis is the process of converting a sequence of characters into a sequence of tokens. A
program or function which performs lexical analysis is called a lexical analyzer, lexer or scanner.The
specification of a programming language will often include a set of rules which defines the lexer. These
rules usually consist of regular expressions and they define the set of possible character sequences that
are used to form individual tokens or lexemes whitespace is also defined by a regular expression and
influences the recognition of other tokens, but does not itself contribute any tokens.
A token is a string of characters, categorized according to the rules as a symbol (e.g. IDENTIFIER,
NUMBER, COMMA, etc.). The process of forming tokens from an input stream of characters is called
tokenization and the lexer categorizes them according to a symbol type. A token can look like anything
that is useful for processing an input text stream or text file.
A lexical analyzer generally does nothing with combinations of tokens, a task left for a parser. For
example, a typical lexical analyzer recognizes parenthesis as tokens, but does nothing to ensure that each
'(' is matched with a ')'.
Consider this expression in the C programming language:
sum=3+2;
Tokenized in the following table:
lexeme token type
sum Identifier
= Assignment operator
3 Number
+ Addition operator
2 Number
; End of statement
Tokens are frequently defined by regular expressions, which are understood by a lexical analyzer
generator such as lex. The lexical analyzer (either generated automatically by a tool like lex, or hand-
crafted) reads in a stream of characters, identifies the lexemes in the stream, and categorizes them into
tokens. This is called "tokenizing." If the lexer finds an invalid token, it will report an error.

ALGORITHM:
1) Start the program.
2) Declare all the variables and file pointers.
3) Display the input program.
4) Separate the keyword in the program and display it.
5) Display the header files of the input program.
6) Separate the operators of the input program and display it.
7) Print the punctuation marks.
8) Print the constant that are present in input program.
9) Print the identifiers of the input program.
10) Stop the program.
Conclusion: Thus we have implemented Lexical Analyzer for subset of C

3
System Programming TE-IT

Write short answer of following questions : (4-5 Max)


1. Explain phase of compiler.
2. Explain symbol table.
3. Explain lexical analysis.
4. What is token?
5. What is identifier?
6. What is UST?

Viva Questions: (8-10)

1) List down phases of compiler.


2) What is the function of each phase?
3) What is lex?
4) How are tokens identified in lex?
5) List down the types of tokens?
6) What is the structure of lex file?
7) RE to DFA algorithm is implemented in which phase of compiler?
8) How do lexical analyzer and parser work together?
9) What is uniform symbol table?

 Mapping of CO, PO and PSO


Note : enter Correlation levels in the box, 1. slight(Low); 2: Moderate(medium); 3. Substantial
( High), If No correlation, Put “-”

Course Outcome (CO):

S Name of Experiment CO1 CO2 CO3


No
01 Implementation of lexical  
analyzer

Program Outcomes (PO):

S Name of PO PO PO PO PO PO PO PO PO PO1 PO1 PO1


N Experiment 1 2 3 4 5 6 7 8 9 0 1 2
o
01 Implementatio 
n of lexical
analyzer

Program Specific Outcomes (PSOs):

S Name of Experiment PSO1 PSO2 POS3 POS4 POS5

4
System Programming TE-IT

No
01 Implementation of lexical 
analyzer

CO and PO Mapping:

COs /POs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1  

CO2
CO3

CO and PSO Mapping:

COs PSO1 PSO2 PSO3 PSO4 PSO5


/PSOs
CO1
CO2 
CO3 

5
System Programming TE-IT

Name of the Student: ______________________________________ Roll no: ___

CLASS: - T.E [IT] Division:_______ Course: - SL-V

Experiment No. 5
** Recursive Descent parser for assignment statement **

Date of Performance: Sign with Date Marks: / 10

/ /20

Aim : Recursive Descent parser for assignment statement

Objective: -
To understand the process of parsing & to study bottom-up & top down parsing.

Outcomes:
At the end of the assignment the students should have
1. Understood the design and implementation of parser

PEOs, POs, PSOs and COs satisfied

POs: i PEOs: I PSOs: 2 COs: 2

Infrastructure: Desktop/ laptop system with Linux or its derivatives.

Software used: LINUX/ Windows OS/ Virtual Machine/ IOS

Theory:
One of the most straightforward forms of parsing is recursive descent parsing. This is a top-down
process in which the parser attempts to verify that the syntax of the input stream is correct as it is read
from left to right. A basic operation necessary for this involves reading characters from the input stream
and matching then with terminals from the grammar that describes the syntax of the input. Our recursive
descent parsers will look ahead one character and advance the input stream reading pointer when proper
matches occur.

What a recursive descent parser actually does is to perform a depth-first search of the derivation tree for
the string being parsed. This provides the 'descent' portion of the name. The 'recursive' portion comes
from the parser's form, a collection of recursive procedures.

6
System Programming TE-IT

As our first example, consider the simple grammar

E  x+T
T  (E)
Tx

and the derivation tree in figure 2 for the expression x+(x+x)

Figure 2 - Derivation Tree for x+(x+x)

A recursive descent parser traverses the tree by first calling a procedure to recognize an E. This
procedure reads an 'x' and a '+' and then calls a procedure to recognize a T.

Input:
1. Grammar Rules
2. String

Output:
Evaluation of String

Algorithm:
1. Give input string.
2. Mention Grammar.
3. Compare each toke of string with grammar.
4. If token satisfies the grammar rule then return the evaluation of string else evaluation not
possible.

Conclusion: Thus we have studied Recursive Descent parser.

7
System Programming TE-IT

Write short answer of following questions : (4-5 Max)


1. What is Parser?
2. Explain functionality of Parser?
3. What are the different types of Parser?
4. Explain different types of Parsers?
5. What type of conflicts are encountered in Shift reduce parsers?

Viva Questions: (8-10)

1) What are the types of parsers?


2) Types of top down parsers.
3) Types of bottom up parsers.
4) Name a backtracking top down parser.
5) Name a non backtracking top down parser.
6) Why are top down parsers also called LL parsers?
7) List down the bottom up parsers.
8) Why are bottom up parsers call LR parsers?
9) What is handle and handle pruning?
10) Which is the most powerful bottom up( LR) parser?

 Mapping of CO, PO and PSO


Note : enter Correlation levels in the box, 1. slight(Low); 2: Moderate(medium); 3. Substantial
( High), If No correlation, Put “-”

Course Outcome (CO):

S Name of Experiment CO1 CO2 CO3


No
01 Recursive Descent Parser 
for Assignment Statement

Program Outcomes (PO):

S Name of PO PO PO PO PO PO PO PO PO PO1 PO1 PO1


No Experimen 1 2 3 4 5 6 7 8 9 0 1 2
t
01 Recursive 
Descent
Parser for
Assignment
Statement

8
System Programming TE-IT

Program Specific Outcomes (PSOs):

S Name of Experiment PSO1 PSO2 POS3 POS4 POS5


No
01 Recursive Descent Parser 
for Assignment Statement

CO and PO Mapping:

COs /POs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 

CO2
CO3

CO and PSO Mapping:

COs PSO1 PSO2 PSO3 PSO4 PSO5


/PSOs
CO1
CO2 
CO3

9
System Programming TE-IT

Name of the Student: ______________________________________ Roll no: ___

CLASS: - T.E [IT] Division:_______ Course: - SL-V

Experiment No. 6
** Implementation of Calculator using LEX and YACC **

Date of Performance: Sign with Date Marks: / 10

/ /20

Aim Implementation of Calculator using LEX and YACC.

Objective: -
1. To understand the role of LEX and YACC.

Outcomes:
At the end of the assignment the students should have
1. Understood implementation of LEX and YACC

PEOs, POs, PSOs and COs satisfied

POs: i PEOs: I PSOs: 2 COs: 3

Infrastructure: Desktop/ laptop system with Linux or its derivatives.

Software used: LINUX/ Windows OS/ Virtual Machine/ IOS

Theory:
Lex several tools have been built for constructing lexical analyzers from special purpose
notations based on regular expressions several algorithms exist for compiling regular expressions into
pattern matching algorithms Lex is a tool which uses such algorithm
Lex is a tool widely used to specify lexical analyzers for variety of languages. The tool shows
how the speciation of patterns using regular expression can be combined with actions that LA may be
required to perform.

10
System Programming TE-IT

Specification of Lexical Analyzer is prepared by creating a program lex.1 in Lex language. Lex.1 is run
through Lex compilers to produce yy.c which consist of a tabular representation of transition diagram
constructed from regular expression of Lex.1, together with a standard routine that uses the table to
recognize lexemes actions associated with regular expressions in Lex.l R pieces of C code and are
carried over directly to Lex.yy.c finally lex.yy.c is run through C compiler to produce a.out which is the
lexical analyzer that transforms an input stream into a sequence of tokens

Lex Specifications:

Lex program consist of 3 parts

Declarations
%%

Transition Rules
%%

Auxiliary Procedures

Declaration Section Includes

Declarations of variables manifest constants and regular expressions.

/* Example of Declaration */

%{

#include “yy.tab.h”
extern int yylval;
%}

/* Example of Regular Definitions */


delim [ \t \n ]
ws [ delim ] +

11
System Programming TE-IT

letter [a-z to A-Z]


digit [0-9]
id {letter} ( {letter} / {digit} )*
numbe
r {digit} + (\.{digit}+) ? (E [+\-] ? {digit} +) ?
%% /* Transiotion Rules */
{ws} { /* No action, no return */ }
{ yylval = install – num (); return
{number} (NUMBER); }
{id} {yylval = install – id (); return (ID); }
Auxiliary
%% /* Procedures */

install_id ()
{

/*

*/
}

install_num ()
{

/*

*/

Explanation

Declarations are surrounded by special brackets % { and % }. Anything appearing between


these brackets is copied directly into lex.yy.c & is not treated as a part of regular definitions or
translation rules. Same is applied to the auxiliary procedures in the 3 section & these procedures are
copied into lex.yy.c

Regular Definitions Each such definitions a name and a regular expression


consist of denoted
by that name.
?  meta symbol – Zero or More occurrences of
\. – decimal point , since „.‟ , itself represents character class of characters

[ +\- ] – slash is required before -, since – also represents range.

12
System Programming TE-IT

Translation Rules Structure of LA is such that it keeps trying to recognize tokens, until action
associated with one found causes a return.

Yylval is a variable whose definition appears in Lex output lex.yy.c and which is also available
to parser. Purpose of yylval is to hold the lexical value returned. „return‟ statement can only return a
code for token class.

Variable yytext corresponds to the pointer to the first character of lexeme & yyleng is integer telling
how long the lexeme is.

Parser Generators

YACC – yet another compiler – compiler facilitates the construction of front end of the Yacc
specification

YACC source prog has 3 parts

Declarations
%%

Transition rules

%%
supporting C routines

Declaration Part

1. Ordinary „C‟ declarations within % { %}


2. Token Declarations

Translation rules part  % % & then rules each translation rule consist of a grammar production &
associated semantic action.

13
System Programming TE-IT

E.g. expr : expr „+‟ expr { $$ = $1+ $3; } Expr


„-„ expr {$$ = $1 - $3; }

1) „+‟,‟-„ are quoted single characters of type „c‟ and are treated as terminal symbols
2) Unquoted strings of letters and digits not declared to be tokens are taken as non-terminals –expr.

3) Alternative right sides can be separated by a vertical bar and a semicolon follows each left side
with its alternatives & their semantic actions first left side is taken as start symbol.

YACC semantic action is a sequence of „C‟ statements. In semantic action $$ refers to the value
associated with non terminal on left and while $i refers to the value associated with ith grammar symbol
[T \ NT] on right.

Supporting C – routines - other procedures such as error recovery routines may be provided.

Communication between Lex and YACC


Yylex() produces pairs containing token and its associated value. If token NUMBER is return
token NUMBER must be declared in first section of YACC. The attribute value associated with a token
is communicated to the parser through YACC defined variable yylval.

Conclusion: Thus we have implemented a calculator using LEX and YACC.

14
System Programming TE-IT

Write short answer of following questions : (4-5 Max)


1. Explain Lex.
2. Explain Yacc.
3. Explain the parts of lex and yacc program.
4. Explain the structure of the lex program
5. explain the structure of a yacc program

Viva Questions: (8-10)

1. What is yylex()?
2. What does the ‘Definition section’ contain?
3. What does the ‘Rule section’ contain?
4. What does the last section contain?
5. What is the difference between yylex() and scanf()?
6. How is the instruction in the first section used?
7. Explain longest match.
8. What is the rule of lex to specify file
9. Explain more applicable rule
10. Is Yacc a compiler?

 Mapping of CO, PO and PSO


Note : enter Correlation levels in the box, 1. slight(Low); 2: Moderate(medium); 3. Substantial
( High), If No correlation, Put “-”

Course Outcome (CO):

S Name of Experiment CO1 CO2 CO3


No
01 Implementation of 
Calculator using LEX and
YACC

Program Outcomes (PO):

S Name of PO PO PO PO PO PO PO PO PO PO1 PO1 PO1


N Experiment 1 2 3 4 5 6 7 8 9 0 1 2
o
01 
Implementatio
n of Calculator
using LEX
and YACC

Program Specific Outcomes (PSOs):

15
System Programming TE-IT

S Name of Experiment PSO1 PSO2 POS3 POS4 POS5


No
01 Implementation of 
Calculator using LEX and
YACC
CO and PO Mapping:

COs /POs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1
CO2
CO3 

CO and PSO Mapping:

COs PSO1 PSO2 PSO3 PSO4 PSO5


/PSOs
CO1
CO2
CO3 

16
System Programming TE-IT

Name of the Student: ______________________________________ Roll no: ___

CLASS: - T.E [IT] Division:_______ Course: - SL-V

Experiment No. 7
** Intermediate code generation using LEX &YACC for Control Flow and Switch Case statements.**

Marks: / 10
Date of Performance: Sign with Date
/ /20

Aim : Intermediate code generation using LEX &YACC for Control Flow and Switch Case
statements.

Objective: -
1. To understand intermediate code generation using LEX and YACC

Outcomes:
At the end of the assignment the students should have
1. Understood intermediate code generation using LEX And YACC

PEOs, POs, PSOs and COs satisfied

POs: i PEOs: I PSOs: 2 COs: 3

Infrastructure: Desktop/ laptop system with Linux or its derivatives.

Software used: LINUX/ Windows OS/ Virtual Machine/ IOS

Theory:
LEX

The unix utility lex parses a file of characters. It uses regular expression matching; typically it is used to
„tokenize‟ the contents of the file. In that context, it is often used together with the yacc utility.
However, there are many other applications possible.

17
System Programming TE-IT

Structure of a lex file


A lex file looks like

...definitions...
%%
...rules...

%%
...code...

Definition Section:

C code Any indented code between %{ and %} is copied to the C file. This is typically used for defining
file variables, and for prototypes of routines that are defined in the code segment.

definitions A definition is very much like a #define cpp directive. For example
letter [a-zA-Z]

digit [0-9] punct


[,.:;!?] nonblank [ˆ \
t]

These definitions can be used in the rules section: one could start a rule
{letter}+ {...

state definitions If a rule depends on context, it‟s possible to introduce states and incorporate those in
the rules. A state definition looks like %s STATE, and by default a state INITIAL is already given.

Rules section

The rules section has a number of pattern-action pairs. The patterns are regular expressions, and
the actions are either a single C command, or a sequence enclosed in braces.

User code section

If the lex program is to be used on its own, this section will contain a main program. If you leave
this section empty you will get the default main:

int main()
{

yylex(); return
0;
}

where yylex is the parser that is built from the rules.

18
System Programming TE-IT

YACC

The unix utility yacc (Yet Another Compiler Compiler) parses a stream of token, typically generated by
lex, according to a user-specified grammar.

Structure of a yacc file:


A yacc file looks much like a lex file:

...definitions...
%%
...rules...

%%
...code...

Definitions :
All code between %{ and %} is copied to the beginning of the resulting C file.

Rules:

A number of combinations of pattern and action: if the action is more than a single command it needs to
be in braces.

Code:

This can be very elaborate, but the main ingredient is the call to yylex, the lexical analyser. If the code
segment is left out, a default main is used which only calls yylex.

Intermediate Code Generation

We could translate the source program directly into the target language. However, there are benefits to
having an intermediate, machine-independent representation.
1. A clear distinction between the machine-independent and machine-dependent parts of the
compiler
2. Retargeting is facilitated the implementation of language processors for new machines will
require replacing only the back-end.
3. We could apply machine independent code optimization techniques

Intermediate representations span the gap between the source and target languages.

3-ADDRESS CODE

The general form is x := y op z, where “op” is an operator, x is the result, and y and z are operands. x, y,
z are variables, constants, or “temporaries”. A three-address instruction consists of at most 3 addresses

19
System Programming TE-IT

for each statement.

A TAC instruction has at most one operator on the RHS of an instruction; no built-up arithmetic
expressions are permitted.
e.g. x + y * z can be translated as t1 =
y*z
t2 = x + t1
Where t1 & t2 are compiler–generated temporary names.

Types of three address code

There are different types of statements in source program to which three address code has to be
generated. Along with operands and operators, three address code also use labels to provide flow of
control for statements like if-then-else, for and while. The different types of three address code
statements are:

Assignment statement a = b
op c

In the above case b and c are operands, while op is binary or logical operator. The result of applying op
on b and c is stored in a.

Unary operation

a = op b This is used for unary minus or logical negation.


Example: a = b * (- c) + d

Three address code for the above example will be t1 =


-c

t2 = t1 * b

t3 = t2 + d a = t3

Copy Statement a = b

The value of b is stored in variable a.

Unconditional jump goto


L

Creates label L and generates three-address code „goto L‟

v. Creates label L, generate code for expression exp, If the exp returns value true then go to the
statement labelled L. exp returns a value false go to the statement immediately following the if

20
System Programming TE-IT

statement.

Function call

For a function fun with n arguments a1,a2,a3….an ie.,


fun(a1, a2, a3,…an),

the three address code will be


Param a1

Param a2

Param an Call
fun, n
Where param defines the arguments to function.

Array indexing
In order to access the elements of array either single dimension or

multidimension, three address code requires base address and offset value. Base address consists of the
address of first element in an array. Other elements of the array can be accessed using the base address
and offset value.

Example: x = y[i]

Memory location m = Base address of y + Displacement i x =


contents of memory location m

similarly x[i] = y

Memory location m = Base address of x + Displacement i The


value of y is stored in memory location m

Pointer assignment

x = &y x stores the address of memory location y x =


*y y is a pointer whose r-value is location

*x = y sets r-value of the object pointed by x to the r-value of y

Intermediate representation should have an operator set which is rich to implement most of the
operations of source language. It should also help in mapping to restricted instruction set of target
machine.

21
System Programming TE-IT

Implementation of 3 address code

Three address code is represented as record structure with fields for operator and operands. These
records can be stored as array or linked list. Most common implementations of three address code are-
Quadruples, Triples and Indirect triples.

Quadruples-

Quadruples consists of four fields in the record structure. One field to store operator op, two fields to
store operands or arguments arg1and arg2 and one field to store result res. res = arg1 op arg2 Example: a
=b+c

b is represented as arg1, c is represented as arg2, + as op and a as res.

Unary operators like „-„do not use agr2. Operators like param do not use agr2 nor result. For conditional
and unconditional statements res is label. Arg1, arg2 and res are pointers to symbol table or literal table
for the names.

Example: a = -b * d + c + (-b) * d

Three address code for the above statement is as follows t1 =


-b

t2 = t1 * d
t3 = t2 + c

t4 = - b
t5 = t4 * d

t6 = t3 + t5 a = t6

Quadruples for the above example is as follows

Triples

22
System Programming TE-IT

Triples uses only three fields in the record structure. One field for operator, two fields for operands
named as arg1 and arg2. Value of temporary variable can be accessed by the position of the statement
the computes it and not by location as in quadruples.

Example: a = -b * d + c + (-b) * d

Triples for the above example is as follows

Arg1 and arg2 may be pointers to symbol table for program variables or literal table for constant or
pointers into triple structure for intermediate results.

Example: Triples for statement x[i] = y which generates two records is as follows

Triples for statement x = y[i] which generates two records is as follows

Triples are alternative ways for representing syntax tree or Directed acyclic graph for program defined

23
System Programming TE-IT

names.

Indirect Triples

Indirect triples are used to achieve indirection in listing of pointers. That is, it uses pointers to triples
than listing of triples themselves.

Example: a = -b * d + c + (-b) * d

Syntax Trees

Syntax trees are high level IR. They depict the natural hierarchical structure of the source program.
Nodes represent constructs in source program and the children of a node represent meaningful
components of the construct. Syntax trees are suited for static type checking.

3 address code for control flow statements: If


then else

if(a<b) then
x=y+z

else p=q+r

3 address code:
1. if(a<b) goto 3

2. goto 6

3. t1=y+z
4. x=t1

5. goto 8

24
System Programming TE-IT

6. t2=q+r

7. p=t2
8.

While statement
while(a<b)

do x=y+z

3 address code
1. if(a<b) goto 3
2. goto 6

3. t1=y+z

4. x=t1
5. goto 1

do while statement do

x=y+z while
a<b

3 address code
1. t1=y+z
2. .x=t1

3. if a<b goto 1
4. goto....

For statement
for(i=1;i<=20;i++)
x=y+z

3 address code
1. i=1
2. if i<=20 goto 7

3. goto 10
4. t1=i+1
5. i=t1

6. goto 2

25
System Programming TE-IT

7. t2=y+z

8. x=t2

9. goto 4
10.

Conclusion: Thus we have implemented Intermediate code generation using LEX &YACC for Control
Flow and Switch Case statements.

26
System Programming TE-IT

Write short answer of following questions: (4-5 Max)


1. What is meant by triples & quadruples?
2. What are the different types of intermediate representations?
3. How is the parse tree generated using parser(YACC)?
4. Which type of parser does YACC implement?
5. Which type of grammar does YACC use to generate parse tree?

Viva Questions: (8-10)

1. List down different representations of Intermediate code generation in parsers.


2. What is YACC?
3. What is meant by yylex?
4. How is hierarchy of operators decided in YACC?
5. Explain the procedure for executing a lex prg
6. what is the difference between Lex and Yacc?
7. what does the lex prg contain?
8. what are the different parts of lex program?
9. What are different sections of yacc?

 Mapping of CO, PO and PSO


Note : enter Correlation levels in the box, 1. slight(Low); 2: Moderate(medium); 3. Substantial
( High), If No correlation, Put “-”

Course Outcome (CO):

S Name of Experiment CO1 CO2 CO3


No
01 Intermediate Code 
Generation using LEX And
YACC

Program Outcomes (PO):

S Name of PO PO PO PO PO PO PO PO PO PO1 PO1 PO1


No Experimen 1 2 3 4 5 6 7 8 9 0 1 2
t
01 
Intermediat
e Code
Generation
using LEX
And YACC

27
System Programming TE-IT

Program Specific Outcomes (PSOs):

S Name of Experiment PSO1 PSO2 POS3 POS4 POS5


No
01 Intermediate Code 
Generation using LEX And
YACC

CO and PO Mapping:

COs /POs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1
CO2
CO3 

CO and PSO Mapping:

COs PSO1 PSO2 PSO3 PSO4 PSO5


/PSOs
CO1
CO2
CO3 

28

You might also like