0% found this document useful (0 votes)
13 views78 pages

SPCC Lab Manual EXP ASGN Final Upload

The document outlines the lab file for the System Programming and Compiler Construction course at Rizvi College of Engineering. It includes course objectives, outcomes, rubrics for assessment, and guidelines for conducting experiments related to system programming. Additionally, it details the implementation of a two-pass assembler as part of the lab experiments.

Uploaded by

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

SPCC Lab Manual EXP ASGN Final Upload

The document outlines the lab file for the System Programming and Compiler Construction course at Rizvi College of Engineering. It includes course objectives, outcomes, rubrics for assessment, and guidelines for conducting experiments related to system programming. Additionally, it details the implementation of a two-pass assembler as part of the lab experiments.

Uploaded by

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

CSL601

RIZVI EDUCATION SOCIETY’S

Rizvi College of Engineering


Department of Computer Engineering

System Programming and


Compiler Construction Journal

NAME:

CLASS:

ROLL NO. / UIN:


Rizvi College of Engineering CSL601 (SPCC)

RIZVI EDUCATION SOCIETY’S

Rizvi College of Engineering


Department of Computer Engineering

Lab File

Class & Semester: TE (Sem 6)


Subject: System Programming and Compiler Construction Lab

Name: …………………………………………………………………

Roll No. : ……………..

UIN: …………………………….

Document Revised on: 10 / 01 / 2024

Page | 1
Rizvi College of Engineering CSL601 (SPCC)

RIZVI EDUCATION SOCIETY’S

Rizvi College of Engineering


Department of Computer Engineering

Certificate

This is to certify that ........................................................................................ of Third year Computer


Engineering Department has performed all the experiments of System Programming and Compiler Construction
with satisfactory results.

………………………………..

Subject In-charge

Page | 2
Rizvi College of Engineering CSL601 (SPCC)

INSTITUTE MISSION
INSTITUTE VISION
IM1. Impart Core Fundamental Principles: To impart core
fundamental principles of engineering and science that will enable
To become a leading entity in transforming the the learner to develop solutions to complex engineering problems,
diverse class of learners into innovators, analyzers through conventional and innovative teaching learning methods and
and entrepreneurs competent to develop eco-friendly mentoring.
sustainable solutions and work in multi-disciplinary IM2. Bridge the Technical Skill Gap: To bridge the technical
skill gap through curriculum enrichment activities for industry
environment to meet the global challenges and
readiness.
contribute towards nation building. IM3. Inculcate Professional Etiquettes and Ethics: To groom
the learner through dedicated training, placement and extension
activities and to inculcate professional etiquettes and ethics aimed
DEPARTMENT VISION at holistic development of the learners enabling them to acquire
distinguished positions in the leading industries or be eligible for
higher studies in globally recognized universities.
To become a center of excellence in the field of IM4. Research and Development: To provide modern
Computer Engineering to transform diverse class of infrastructure and the necessary resources, for planning and
learners into skilled professionals with ethical values implementing innovative ideas, leading to meaningful research and
capable of contributing towards nation building. development and entrepreneurship.

PROGRAM OUTCOMES DEPARTMENT MISSION

DM1. Quality Technical Education: To provide quality technical


PO1. Engineering knowledge education with the help of modern resources for finding solutions to
PO2. Problem analysis complex problems of computer engineering.
PO3. Design/development of solutions DM2. Leadership & Entrepreneurial skills: To inculcate moral
PO4. Conduct investigations of complex and ethical values while acquiring leadership, entrepreneurial skills
problems and overall personality development.
DM3. Professional Skills and Lifelong Learning: To inculcate
PO5. Modern tool usage
professional skills and lifelong learning for further education as well
PO6. The engineer and society
as acquiring distinguished positions in leading software industries.
PO7. Environment and sustainability DM4. Research and Development: To encourage creative
PO8. Ethics thinking for planning and implementing innovative ideas leading to
PO9. Individual and team work meaningful research and development considering economical,
PO10. Communication societal and environmental factors.
PO11. Project management and finance
PO12. Life-long learning
PROGRAM EDUCATIONAL OBJECTIEVES

PROGRAM SPECIFIC OUTCOMES


PEO1. Successful Career: To build a successful career in leading
industries related to the field of Computer Engineering wherein the
engineer will be able to provide the necessary solutions to the
PSO1. Open Source Tools: To encourage the challenges witnessed in existing and new business models.
students to work using open-source PEO2. Leadership Qualities: To exhibit the qualities of team
software’s & tools in diversified areas of spirit, leadership, and problem-solving skills to achieve top positions
computer science. in the organization or to enhance entrepreneurial skills.
PEO3. Adaptability to New Technology: To be able to adapt to
to acquire the necessary skill set required to new technologies and platforms and share their knowledge with their
develop, test, install, deploy, and maintain a peers in the allied fields.
complete software system for business and PEO4. Research & Higher Studies: To develop proficiency in
computer engineering and related fields to be able to work in multi-
other applications, that makes them industry
disciplinary areas with a strong focus on innovation and research.
ready.

Page | 3
Rizvi College of Engineering CSL601 (SPCC)

Course Objectives
Learner will be able to

Implement the system program functionality and implement parsers.

Course Outcomes
Learner will be able to…

CO1. Generate machine code by implementing two pass assemblers.


CO2. Implement Two pass macro processor
CO3. Parse the given input string by constructing Top down/Bottom-up parser.
CO4. Identify and Validate tokens for given high level language and Implement synthesis phase of compiler.
CO5. Explore LEX & YACC tools

Page | 4
Rizvi College of Engineering CSL601 (SPCC)

Rubrics
Following rubrics will be used to assess the work submitted by the students.

1. For Experiment 1 to 7

Criteria 1 : Program Logic and Algorithm Analysis


Basic understanding of the system program and how well the student is able to analyze the flow
of the algorithm.
5 4 3 2 1
Excellent Good Average Below Poor
Average
Student is very crisp Student has a Student has Student has below Student has a poor
in implementing logic good average average understanding of
and is able to analyze understanding of understanding understanding of program logic and
the algorithm program logic of program program logic and is is able to analyze
correctly and is able to logic and is able to analyze the the flow of the
analyze the flow able to flow of the algorithm algorithm
of the algorithm. analyze the
flow of the
algorithm
Criteria 2 : How well the student is able to deliver and present the algorithmic
Delivery approaches?
5 4 3 2 1
Excellent Good Average Below Poor
Average
Student is very crisp Student is good in Student is Student is below Student is poor in
in implementing logic implementing logic average in average in implementing logic
and is able to deliver and is able to implementing implementing logic and is able to deliver
the algorithm deliver the logic and is able and is able to deliver the algorithm
logic correctly algorithm to deliver the the algorithm logic
logic correctly algorithm logic decentl
logic decently. decently. y
Criteria 3 : Code How well is the code generated and optimized? And how well are
readability and resultstangible and interpreted?
interpretation of
results
5 4 3 2 1
Excellent Good Average Below Poor
Average
Student is very crisp Student is very Student is Student is below Student is poor in
in understanding of clear in average in average in understanding of
the code and understanding of understanding understanding of the the optimized code
is not
generates the the code and of the code and code and generates able to produce
optimized code and generates the generates the the optimized code results.
produces tangible optimized code and optimized code and produces
results. produces and produces interpretable results.
interpretable interpretable
results. results.

Page | 5
Rizvi College of Engineering CSL601 (SPCC)

2. For Assignments 1 & 2

Criteria 1 : Problem and Algorithm Analysis


Basic understanding of the system program and how well the student is able to analyze the flow
of the algorithm.
5 4 3 2 1
Excellent Good Average Below Poor
Average
Student is very crisp Student has a Student has Student has below Student has a poor
in implementing logic good average average understanding of
and is able to analyze understanding of understanding understanding of program logic and
the algorithm program logic of program program logic and is is able to analyze
correctly and is able to logic and is able to analyze the the flow of the
Analyze the flow able to flow of the algorithm algorithm
of the algorithm. Analyze the
flow of the
algorithm
Criteria 2 : How well the student is able to deliver and present the problem approaches?
Presentation

5 4 3 2 1
Excellent Good Average Below Poor
Average
Student is very crisp Student is good in Student is Student is below Student is poor in
in implementing logic implementing logic average in average in implementing logic
and is able to deliver and is able to implementing implementing logic and is able to deliver
the algorithm deliver the logic and is able and is able to deliver the algorithm
logic correctly algorithm to deliver the the algorithm logic
logic correctly algorithm logic decentl
logic decently. decently. y
Criteria 3 : How well is the solution generated and optimized? And how well are results
Problem tangible and interpreted?
readability and
interpretation of
results
5 4 3 2 1
Excellent Good Average Below Poor
Average
Student is very crisp Student is very Student is Student is below Student is poor in
in understanding of clear in average in average in understanding of
the code and understanding of understanding understanding of the the code and is not
generates the the code and of the code and code and generates able to produce
Optimized code and generates the generates the the optimized code results.
produces tangible Optimized code Optimized code and produces
and
results. produces and produces interpretable results.
interpretable interpretable
results. results.

Page | 6
Rizvi College of Engineering CSL601 (SPCC)

Lab Guidelines

 Do not go on unapproved sites.


 No food or drinks near the keyboard.
 Only use your assigned computer and workstation.
 Do not change the settings on the computer.
 Ask permission to download.
 Ask permission to print documents
 Save your work often.
 Report cyber-bullying.

Page | 7
Rizvi College of Engineering CSL601 (SPCC)

INDEX
Sr. Title Page Performed Sign & Rubrics
No. No. On Date Points
1 Exp 1 : Implementations of two pass
Assembler.
2 Exp2: Implementation of Two pass Macro
Processor
3 Exp3: Implementation of Lexical Analyzer.
4 Exp4: Implementation of Shift Reduce Parser
in Compiler.
5 Exp5: Implementation of Intermediate code
generation phase of compiler
6 Exp6: Implementation of code optimization
phase of compiler.
7 Study and implement experiments on LEX,
YACC.
8 Assignment 1
9 Assignment 2

TOTAL

Page | 8
Rizvi College of Engineering CSL601 (SPCC)

EXPERIMENTS

Page | 9
Rizvi College of Engineering CSL601 (SPCC)

Expt. No. 1

Title: Implementation of a two pass assembler

Rubric Score (0 to 5)
Program Logic &
Algorithm Analysis
Delivery

Code Readability and


Interpretability of results

Total

Performed On:

Sign:

Page | 10
Rizvi College of Engineering CSL601 (SPCC)

Experiment No. 1
SPCC

Aim: To implement two pass assembler

Software: GCC Compiler, CodeBlocks

Pre-Lab Questions:

Question 1: What is the purpose of Pass 1 and Pass 2 Assembler?

Question 2: Explain different assembler directives?

Post-Lab Questions:

Question 1: What are the functions of POTGET, MOTGET, LTSTO, BTSTO, LITASS?

Question 2: What is the difference between MOT of pass1 &pass2?


Theory: An assembler is a translator that translates an assembler program into a conventional machine
language program. Basically, the assembler goes through the program one line at a time, and generates
machine code for that instruction. Then the assembler proceeds to the next instruction. In this way, the
entire machine code program is created. For most instructions this process works fine, for example for
instructions that only reference registers, the assembler can compute the machine code easily, since the
assembler knows where the registers are.

Consider an assembler instruction like the following

JMP LATER

...
...
LATER:
This is known as a forward reference. If the assembler is processing the file one line at a time, then it
doesn't know where LATER is when it first encounters the jump instruction. So, it doesn't know if the
jump is a short jump, a near jump or a far jump.
So, Assembler scan the code twice. The first time, just count how long the machine code instructions will
be, just to find out the addresses of all the labels. Also, create a table that has a list of all the addresses and
where they will be in the program. This table is known as the symbol table. On the second scan, generate
the machine code, and use the symbol table to determine how far away jump labels are, and to generate
the most efficient instruction.
This is known as a two-pass assembler. Each pass scans the program, the first pass generates the symbol
Page | 11
Rizvi College of Engineering CSL601 (SPCC)
table and the second pass generates the machine code. I have created a listing of an assembler
program that has the machine code listed, and the symbol table listed.

Algorithm for pass 1:


Step 1: Start
Step 2: Initialize location counter to zero.
Step 3: Read opcode field of next instruction.
Step 4: Search opcode in pseudo opcode table.
Step 5: If opcode is found in Pseudo Opcode Table
Step 5.1: If it is ‘DS’ or ‘DC’
Adjust location counter to proper alignment.
Assign length of data field to ‘L’
Go to step 9
Step 5.2: If it is ‘EQU’
Evaluate operand field
Assign value to symbol in label field
Go to step 3
Step 5.3: If it is ‘USING’ or ‘DROP’ Go to step 3
Step 5.4: If it is ‘END’
Assign storage locations to literals
Go to step 12
Step 6: Search opcode in Machine Opcode Table.
Step 7: Assign it’s length to ‘L’.
Step 8: Process any literals and enter them into Literal Table.
Step 9: If symbol is there in label field assign current value of Location Counter to symbol.
Step 10: Location Counter = Location Counter + L.
Step 11: Go to step 3
Step 12: Stop.

Algorithm for Pass 2:


Step 1: Start
Step 2: Initialize location counter to zero.
Step 3: Read opcode field of next instruction.
Step 4: Search opcode in pseudo opcode table.
Step 5: If opcode is found in Pseudo Opcode Table

Step 5.1: If it is ‘DS’ or ‘DC’


Adjust location counter to proper alignment.
If it is ‘DC’ opcode form constant and insert in assembled program
Assign length of data field to ‘L’
Go to step 3
Step 5.2: If it is ‘EQU’ or ‘START’ ignore it. Go to step 3
Step 5.3: If it is ‘USING’
Evaluate operand and enter base reg no. and value into base table
Go to step 3
Step 5.3: If it is ‘DROP’
Indicate base reg no. available in base table Go to step 3
Step 5.4: If it is ‘END’
Generate literals for entries in Literal table
Go to step 12
Step 6: Search opcode in Machine Opcode Table.
Page | 12
Rizvi College of Engineering CSL601 (SPCC)
Step 7: Get opcode byte and format code.
Step 8: Assign it’s length to ‘L’.
Step 9: Check type of instruction.
Step 10: If it is of ‘RR’ type
Step 10.1: Evaluate both register expressions and insert into second byte.
Step 10.2: Assemble instruction.
Step 10.3: Location Counter = Location Counter + L.
Step 10.4: Go to step 3
Step 11: If it is of ‘RX’ type
Step 11.1: Evaluate register and index expressions and insert into second byte.
Step 11.2: Calculate effective address of operand.
Step 11.3: Determine appropriate displacement and base register
Step 11.2: Put base and displacement into bytes 3 and 4
Step 11.3: Location Counter = Location Counter + L.
Step 11.4: Go to step 12
Step 12: Stop.

Stepwise-Procedure:
1. Read a Source Program as input.
2. Consider Mnemonic Opcode Table (MOT), Pseudo-op table (POT), Location Counter (LC).
3. Create symbols Table (SYMTAB) and literal Table (LITAB), and intermediate code(IC).
4. Update the Location Counter (LC).

Program & Output:

Page | 13
Rizvi College of Engineering CSL601 (SPCC)

Page | 14
Rizvi College of Engineering CSL601 (SPCC)

Page | 15
Rizvi College of Engineering CSL601 (SPCC)

Page | 16
Rizvi College of Engineering CSL601 (SPCC)

Page | 17
Rizvi College of Engineering CSL601 (SPCC)

Pre-Lab & Post-Lab Answer:

Conclusion:

………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

CO’s Covered:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

Page | 18
Rizvi College of Engineering CSL601 (SPCC)

Expt. No. 2

Title: Implementation of a two pass macroprocessor

Rubric Score (0 to 5)
Program Logic &
Algorithm Analysis
Delivery

Code Readability and


Interpretability of results

Total

Performed On:

Sign:

Page | 19
Rizvi College of Engineering CSL601 (SPCC)

Experiment 2
Aim: To learn working of two pass macro preprocessor and databases required for implementation of
Macro Processor

Pre Lab/ Prior Concepts:


How macros are used in assembly language program?
Theory:

 Macro represents a group of commonly used statements in the source programming language.
 Macro Processor replaces each macro instruction with the corresponding group of source
language statements. This is known as the expansion of macros.
 Using Macro instructions programmer can leave the mechanical details to be handled by the
macro processor.
 Macro Processor designs are not directly related to the computer architecture on which it runs.
 Macro Processor involves definition, invocation, and expansion.

Algorithm for PASS – I


1. Set MDTC (macro definition table counter) to 1
2. Set MNTC (macro name table counter) to 1
3. Read next statement from source program
4. If this source statement is pseudo-opcode MACRO (start of macro definition) then go to step 5
elsego to step 16
5. Read next statement from source program(macro name line)
6. Enter macro name found in step 5 in name field of MNT(macro name table) and Also enter current
value of MDTC in MDT index field of MNT
7. Increment MNTC by 1
8. Prepare argument list array
9. Enter macro name into MDT at index MDTC
10. Increment MDTC by one
11. Read next statement from source program.
12. Create and substitute index notation for arguments in the source statement if any
13. Enter this line into the MDT
14. Increment MDTC by one
15. Check if currently red source statement is pseudo-opcode MEND. If yes then go to step 3 else go
to step 11
16. Write source program statement as it is in file (output of pass 1 s.p.)
17. Check if pseudo-opcode END (end of source program) is encountered. If yes go to step 18 else go
to step 19
18. Go to PASS – II
19. Go to step 3
20. end of PASS –I

Page | 20
Rizvi College of Engineering CSL601 (SPCC)
Algorithm for PASS – II
1. Read next statement from source program (from output s.p. file of pass I)
2. Search in MNT for match with operation code(mnemonic)
3. If macro name found the go to step 4 else go to step 11
4. Retrieve MDT index from MNT and store it in MDTP (macro definition table pointer).
5. Set up argument list array (store actual parameter in ala)
6. Increment MDTP by 1
7. Retrieve line pointer by MDTP from MDT
8. Substitute index notation by actual parameter from argument list array if any
9. Check if currently retrieved line is pseudo-opcode MEND, if yes go to step 1 else go to step 10
10. Write the statement formed in step 8 to expanded source file and go to step 6
11. Write source statement directly into expanded source file
12. Check if pseudo-opcode END (end of s.p. output of pass I) encountered, if yes go to step 13 else go
to step 1
13. End of PASS-II

Stepwise-Procedure:
a. Read a Source Program as input.
b. Recognize the Macro Definition and
c. Create Macro Name Table (MNT), and Macro Definition Table (MDT), and Prepare ALA.
d. Recognize the Macro Call statement
e. Update the Macro Definition Table Pointer (MDTP), and Prepare ALA.
f. Expand the macro call by reading instructions from the MDT.

Program & Output:

Page | 21
Rizvi College of Engineering CSL601 (SPCC)

Page | 22
Rizvi College of Engineering CSL601 (SPCC)

Page | 23
Rizvi College of Engineering CSL601 (SPCC)

Page | 24
Rizvi College of Engineering CSL601 (SPCC)

Pre-Lab & Post-Lab Answer:

Conclusion:

………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

CO’s Covered:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

Page | 25
Rizvi College of Engineering CSL601 (SPCC)

Expt. No. 3

Title: Implementation of a Lexical Analyzer

Rubric Score (0 to 5)
Program Logic &
Algorithm Analysis
Delivery

Code Readability and


Interpretability of results.

Total

Performed On:

Sign:

Page | 26
Rizvi College of Engineering CSL601 (SPCC)

Experiment 3

Aim: To understand the implementation of Lexical analyzer and what all databases are used in the design
of lexical analyzer.

Pre-Lab Questions:

Question 1: Explain the need of a Lexical Analyzer?


Post-Lab Questions:

Question1: Explain the use of different databases in process of lexical analyzer design?
Theory:
Lexical analysis is the process of converting a sequence of characters into a sequence of tokens. Programs
performing lexical analysis are called lexical analyzers or lexers. The specification of a programming
language will include a set of rules, often expressed syntactically, specifying the set of possible character
sequences that can form a token or lexeme. The whitespace characters are often ignored during lexical
analysis.
A token can look like anything; it just needs to be a useful part of the structured text. Consider this
expression in the C programming language:

newval: =oldval+12;
Tokenized in the following table:

Lexeme Token type


newval IDENT
:= ASSIGN_OP
oldval IDENT
+ ADD_OP
12 NUMBER
; SEMICOLON

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.

Following tokenizing is parsing. From there, the interpreted data may be loaded into data structures, for
general use, interpretation, or compiling.

Stepwise-Procedure:
1. Read a ’C’ file as input.
2. Read input files of functions, operators, keywords.
3. Display a token for every lex unit.
4. Create files for symbols and literals.

Page | 27
Rizvi College of Engineering CSL601 (SPCC)

Program & Output:

Page | 28
Rizvi College of Engineering CSL601 (SPCC)

Page | 29
Rizvi College of Engineering CSL601 (SPCC)

Pre-Lab & Post-Lab Answer:

Conclusion:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..
CO’s Covered:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

Page | 30
Rizvi College of Engineering CSL601 (SPCC)

Expt. No. 4

Title: Implementation of Shift Reduce Parser in Compiler.

Rubric Score (0 to 5)
Program Logic &
Algorithm Analysis
Delivery

Code Readability and


Interpretability of results.

Total

Performed On:

Sign:

Page | 31
Rizvi College of Engineering CSL601 (SPCC)

Experiment 4
Aim: To learn Parsing and how Shift Reduce Parser in Compiler is used for validation of Input string.

Pre lab questions:


Question1: What is the stack used for in Shift-Reduce parsing?
Question2: How is the parsing table constructed for a given grammar?

Post-Lab Questions:

Question1: How does the algorithm decide whether to shift or reduce at a given parsing step?
Question2: Explain the concept of a reduce-reduce conflict and how it is addressed

Theory: Shift Reduce parser attempts for the construction of parse in a similar manner as done in bottom-
up parsing i.e. the parse tree is constructed from leaves(bottom) to the root(up). A more general form of
the shift-reduce parser is the LR parser.
This parser requires some data structures i.e.
 An input buffer for storing the input string.
 A stack for storing and accessing the production rules.

Basic Operations –

 Shift: This involves moving symbols from the input buffer onto the stack.
 Reduce: If the handle appears on top of the stack then, its reduction by using appropriate
production rule is done i.e. RHS of a production rule is popped out of a stack and LHS of a
production rule is pushed onto the stack.
 Accept: If only the start symbol is present in the stack and the input buffer is empty then, the
parsing action is called accept. When accepted action is obtained, it is means successful parsing is
done.
 Error: This is the situation in which the parser can neither perform shift action nor reduce action
and not even accept action.

Shift-reduce parsing –

 Shift reduce parsing is a process of reducing a string to the start symbol of a grammar.
 Shift reduce parsing uses a stack to hold the grammar and an input tape to hold the string.


 Sift reduce parsing performs the two actions: shift and reduce. That's why it is known as shift
reduces parsing.
 At the shift action, the current symbol in the input string is pushed to a stack.
 At each reduction, the symbols will replaced by the non-terminals. The symbol is the right side of
the production and non-terminal is the left side of the production.

Shift Reduce Parser requires two Data Structures

Page | 32
Rizvi College of Engineering CSL601 (SPCC)

 Input Buffer
 Stack

There are the various steps of Shift Reduce Parsing which are as follows −
There are the various steps of Shift Reduce Parsing which are as follows −
 It uses a stack and an input buffer.
 Insert $ at the bottom of the stack and the right end of the input string in Input Buffer.

 Shift − Parser shifts zero or more input symbols onto the stack until the handle is on top of the
stack.
 Reduce − Parser reduce or replace the handle on top of the stack to the left side of production, i.e.,
R.H.S. of production is popped, and L.H.S is pushed.
 Accept − Step 3 and Step 4 will be repeated until it has detected an error or until the stack includes
start symbol (S) and input Buffer is empty, i.e., it contains $.

Consider the grammar:

Program & Output:

Page | 33
Rizvi College of Engineering CSL601 (SPCC)

Page | 34
Rizvi College of Engineering CSL601 (SPCC)

Page | 35
Rizvi College of Engineering CSL601 (SPCC)

Page | 36
Rizvi College of Engineering CSL601 (SPCC)

Page | 37
Rizvi College of Engineering CSL601 (SPCC)

Pre-Lab & Post-Lab Answer:

Conclusion:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..
CO’s Covered:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

Page | 38
Rizvi College of Engineering CSL601 (SPCC)

Expt. No. 5

Title: Implementation of intermediate code generation

Rubric Score (0 to 5)
Program Logic &
Algorithm Analysis
Delivery

Code Readability and


interpretability of results

Total

Performed On:

Sign:

Page | 39
Rizvi College of Engineering CSL601 (SPCC)

Experiment 5
Aim: Implement any one type of intermediate code generation

Pre lab questions:

Question 1: Explain different types of intermediate code generation in compilerdesign?

Theory:
If we generate machine code directly from source code then for n target machine we will have
optimizers and n code generator but if we will have a machine-independent intermediate code,
we will have only one optimizer. Intermediate code can be either language-specific (e.g.,
Bytecode for Java) or language. independent (three-address code). The following are commonly
used intermediate code representations:
1. Postfix Notation: Also known as reverse Polish notation or suffix notation. The
ordinary (infix) way of writing the sum of a and b is with an operator in the middle: a
+ b the postfix notation for the same expression places the operator at the right end
as ab +. In general, if e1 and e2 are any postfix expressions, and + is any binary
operator, the result of applying + to the values denoted by e1 and e2 is postfix
notation by e1e2 +. No parentheses are needed in postfix notation because the
position and arity (number of arguments) of the operators permit only one way to
decode a postfix expression. In postfix notation, the operator follows the operand.
Example 1: The postfix representation of the expression (a + b) * c is: ab + c *
Example 2: The postfix representation of the expression (a – b) * (c + d) + (a – b) is
: ab – cd + *ab -+

2. Three-Address Code: A statement involving no more than three references (two for
operands and one for result) is known as a three-address statement. A sequence of
three address statements is known as a three address code. Three address statement is
of form x = y op z, where x, y, and z will have address (memory location).
Sometimes a statement might contain less than three references but it is still called a
three address statement.
Example: The three-address code for the expression a + b * c + d: T 1 = b * c T 2 =a
+ T 1 T 3 = T 2 + d T 1, T 2, T 3 are temporary variables.
There are 3 ways to represent a Three-Address Code in compiler design:
i) Quadruples
ii) Triples
iii) Indirect Triples

Page | 40
Rizvi College of Engineering CSL601 (SPCC)

3. Syntax Tree: A syntax tree is nothing more than a condensed form of a parse tree.
The operator and keyword nodes of the parse tree are moved to their parents and a
chain of single productions is replaced by the single link in the syntax tree the
internal nodes are operators and child nodes are operands. To form a syntax tree put
parentheses in the expression, this way it’s easy to recognize which operand should
come first.

Program & Output:

Page | 41
Rizvi College of Engineering CSL601 (SPCC)

Page | 42
Rizvi College of Engineering CSL601 (SPCC)

Page | 43
Rizvi College of Engineering CSL601 (SPCC)

Pre-Lab & Post-Lab Answer:

Conclusion:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..
CO’s Covered:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

Page | 44
Rizvi College of Engineering CSL601 (SPCC)

Expt. No. 6

Title: Implementation of code generation

Rubric Score (0 to 5)
Program Logic &
Algorithm Analysis
Delivery

Code Readability and


Interpretability of results

Total

Performed On:

Sign:

Page | 45
Rizvi College of Engineering CSL601 (SPCC)

Experiment 6
Aim: Implementation of code generator for an optimized code

Theory: Intermediate code: Three Address Code


It is a sequence of statements of the general form
X=Y op Z

Where x, y and z are names, constants or compiler generated temporaries. Op stands for any operator
such as fixed or floating point arithmetic operator, logical operator on Boolean valued data. Variable
names can appear directly in three address statements

Quadruples

It is a record structure with four fields, which we call op, arg1, arg2 and result. The op field contains an
internal code for the operator. Three address statement x=y op z is represented by placing y in arg1, z in
arg2 and x in result statement with unary operators like x=-y or x=y do not use arg2. Operators like param
use neither arg2 nor result.

Triples

To avoid entering temporary names into the symbol table, we might refer to a temporary value by the
position of the statement that computes it. So three address statements can be represented by records with
only three fields: op, arg1, arg2 as shown above

Stepwise-Procedure:

Code generation Algorithm:

Example:

The assignment statement d=(a-b)+(a-c)+(a-c) might be translated into the following three-address code
sequence.

t=a-b
u=a-c
v=t+u
d=v+u

for three address code statements following code sequence is generated using code generation algorithm

Statement Code generated Register descriptor Address descriptor

Register empty
t=a-b MOV a,R0 R0 contains t t in R0
SUB b,R0

Page | 46
Rizvi College of Engineering CSL601 (SPCC)

u=a-c MOV a, R1 R0 contain t t in R0


SUB c,R1 R1 contains u u in R1
v=t+u ADD R1,R0 R0 contains v v in R0
R1 contains u u in R1
d=v+u ADD R1, R0 R0 contains d d in R0
MOV R0,d d in R0 & memory

Implementation details:

Program & Output:

Page | 47
Rizvi College of Engineering CSL601 (SPCC)

Page | 48
Rizvi College of Engineering CSL601 (SPCC)

Page | 49
Rizvi College of Engineering CSL601 (SPCC)

Page | 50
Rizvi College of Engineering CSL601 (SPCC)

Page | 51
Rizvi College of Engineering CSL601 (SPCC)

Pre-Lab & Post-Lab Answer:

Conclusion:

………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..
CO’s Covered:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..

Page | 52
Rizvi College of Engineering CSL601 (SPCC)

Expt. No. 7

Title: Study and implement YACC and LEX respectively

Rubric Score (0 to 5)
Program Logic &
Algorithm Analysis
Delivery

Code Readability
And interpretability of
results
Total

Performed On:

Sign:

Page | 53
Rizvi College of Engineering CSL601 (SPCC)

Experiment No. 7

Aim: Using Lex and YACC develop a simple parser for


a) Creating desktop calculator.
b) SQL statement

Software: GCC Compiler, Code Blocks

Pre-Lab Question: How does Yacc contribute to the development of a parser?


Post-Lab Question: Compare LEX and YACC approaches

Theory: When a string representing a program is broken into sequence of substrings, such that each
substring represents a constant, identifier, operator, keyword etc of the language, these substrings are
called the tokens of the Language.The Tokens in a Language are represented by a set of Regular
Expressions. A regular expression specifies a set of strings to be matched. It contains text characters and
operator characters. The Advantage of using regular expression is that a recognizer can be automatically
generated The tokens which are represented by an Regular Expressions are recognized in an input string
by means ofa state transition Diagram and Finite Automata.

Lexical Analysis:
The Function of a lexical Analyzer is to read the input stream representing the Source program, one
character at a a time and to translate it into valid tokens.
The Lexical Analysis and parsing can form two different passes of a Parser. The Lexical analysis can
storeall the recognized tokens in an intermediate file and give it to the Parser as an input. However it is
more convenient to have the lexical Analyzer as a coroutine or a subroutine which the Parser calls
whenever it requires a token.

Regular Expressions

The following are the most general notations used for expressing a R.E.

| OR (alternation)
() Group of Subexpression
* 0 or more Occurrences
? 0 or 1 Occurrence
+ 1 or more Occurrences
{n,m} m Occurrences

Page | 54
Rizvi College of Engineering CSL601 (SPCC)
Suppose we want to express the 'C' identifiers as a regular Expression:-
identifier=letter(letter|digit)*
Where letter denotes the character set a-z & A-Z
In LEX we can write it as [a-zA-Z][a-zA-Z0-9]*
The Rules for writing R.E are:-
* An operator character may be turned into a text character by enclosing it in quotes, or by preceding it
with a \ (backslash).
* a/b matches "a" but only if followed by b (the b is not matched)
* a$ matches "a" only if "a" occurs at the end of a line
* ^a matches "a" only if "a" occurs at the beginning of a line
* [abc] matches any character that is an "a", "b" or "c"
* [^abc] matches any character but "a", "b" and "c".
* ab?c matches abc and ac
* Within the square brackets most operators lose their special meanings except "\" and "-". the "^" which
takes there special meaning.
* "\n" always matches newline, with or without the quotes. If you want to match the character "\"
followed by "n", use \\n.

Parse Trees
Parse trees are the Graphical representation of the grammar which filters out
the choice for replacement order of the Production rules.
e.g.
for a production P->ABC the parse tree would be

P
/ \\
/ \\
A BC

Terminals and non-Terminals in a grammar


Terminals:- All the basic symbols or tokens of which the language is composed of are called Terminals.
In a Parse Tree the Leafs represents the Terminal Symbol.
Non-Terminals:- These are syntactic variables in the grammar which represents a set of strings the
grammar is composed of. In a Parse tree all the inner nodes represents the Non-Terminal symbols.

Ambiguous Grammars
A Grammar that produces more than one Parse Tree for the same sentences or the Production rules in a
grammar is said to be ambiguous.
e.g. consider a simple mathematical
expressionE->E*E
This can have two Parse tree according to associativity of the operator

'*'
E E
/ \ / \
* E E *
/ \ /\
E E E E

Page | 55
Rizvi College of Engineering CSL601 (SPCC)
Bottom-up parsing:

The Parsing method in which the Parse tree is constructed from the input language string beginning
fromthe leaves and going up to the root node.
Bottom-Up parsing is also called shift-reduce parsing due to it's implementation.
The YACC supports shift-reduce pasing.
e.g. Suppose there is a grammar G having a production E
E->E*E
and an input string x*y.
The left-hand side of any production are called Handles. thus the handle for this example is E.
The shift action is simply pushing an input symbol on a stack. When the R.H.S of a production is matched
the stack elements are popped and replaced by the corresponding Handle. This is the reduce action.
Thus, in the above example, the parser shifts the input token 'x' onto the stack. Then again it shifts the
token '*' on the top of the stack. Still the production is not satisfied so it shifts the next token 'y' too. Now
the production E is matched so it pops all the three tokens from the stack and replaces it with the handle
'E'. Any action that is specified with the rule is carried out.
If the input string reaches the end of file /line and no error has occurred then the parser executes the
'Accept' action signifing successful completion of parsing. Otherwise it executes an'Error' action.

The shift reduce Parsing has a basic limitation. A grammar which can represent a left-sentential parse tree
as well as right-sentential parse tree cannot be handled by shift reduce parsing. Such a grammar ought to
have two non-terminals in the production rule. So the Terminal sandwitched between
these two non-terminals must have some associativity and precedence. This will help the parser to
understand which non-terminal would be expanded first.

LR Parser
The LR means Left-to- Right signifying the parser which reads the input string from left to right. An LR
parser can be written for almost all Programming constructs.
An LR parser consists of two parts:- Driver Routine & Parsing Table. The Driver routine is
same for all the Parsers, only the Parsing Table changes. The Parsing Table is essentially a form of
representing the State- Transition Diagram for the language. It consists the entries for all
possible States and the input symbols. In each state there in a predetermined next state depending upon
the input symbol. If there is any duplicate entry or two next states for the same symbol, then there is an
ambiguity in the grammar.

LALR Parser
LALR is Look-ahead LR parser. It differs from LR in the fact that it will look ahead one symbol in the
input string before going for a reduce action. Look- ahead helps in knowing if the complete rule has been
matched or not.
e.g. Consider a grammar G with
productionP->AB|ABC
When the Parser shifts the Symbol B it can reduce to P but if the next Symbol was C then it has not

matched the complete rule. A LALR parser will shift one extra token and then take a decision to reduce or
shift.

LEX
1: create a lex file

The General Format of a Lex File consists of three sections:

Page | 56
Rizvi College of Engineering CSL601 (SPCC)
1. Definitions
2. Rules
3. User Subroutines

Definitions consist of any external 'C' definitions used in the lex actions or subroutines. e.g. all
preprocessor directives like #include, #define macros etc. These are simply copied to the lex.yy.c
file. The other types of definitions are Lex definitions which are essentially the lex substitution strings,
Lex start states and lex table size declarations. The Rules is the basic part which specifies the regular
expressions and their corresponding actions. The User Subroutines are the function definitions of the
functions that are used in the Lex actions.

2: Yacc is the Utility which generates the function 'yyparse' which is indeed the Parser. Yacc describes a
context free, LALR (1) grammar and supports both bottom-up and top-down parsing. The general format
for the YACC file is very similar to that of the Lex file.
1. Declarations
2. Grammar Rules
3. Subroutines
In Declarations apart from the legal 'C' declarations there are few Yacc specific declarations which
begins with a %sign.
1. %union It defines the Stack type for the Parser. It is a union of various data/structures/objects.
2. %token These are the terminals returned by the yylex function to the yacc. A token can also have
type associated with it for good type checking and syntax directed translation. A type of a token can be
specified as
%token <stack member> token Name.
3. %type the type of a non-terminal symbol in the Grammar rule can be specified with this. The formatis
%type <stack member> non-terminal.
4. %noassoc Specifies that there is no associativity of a terminal symbol.
5. %left Specifies the left associativity of a Terminal Symbol
6. %right Specifies the right associativity of a Terminal Symbol.
7. %start Specifies the L.H.S non-terminal symbol of a production rule which should be taken as
the starting point of the grammar rules.
8. %prec Changes the precedence level associated with a particular rule to that of the following token
name or literal. The grammar rules are specified as follows:

Context-free grammar production- p->AbC


Yacc Rule- p: A b C { /* 'C' actions */}

The general style for coding the rules is to have all Terminals in upper-case and all non-terminals in
lower-case.
To facilitate a proper syntax directed translation the Yacc has something called pseudo-variables which
forms a bridge between the values of terminal/non-terminals and the actions. These pseudo variables are
$$, $1, $2, $3. .... The $$ is the L.H.S value of the rule whereas $1 is the first R.H.S value of the rule and

Page | 57
Rizvi College of Engineering CSL601 (SPCC)

so is $2 etc. The default type for pseudo variables is integer unless they are specified by %type,
%token <type> etc.

Implementation

Using lex and yacc design a simple parser for parsing arithmetic expression In addition to simple expression
involving operators addition and subtraction, the generated grammar should also handle operator’s like
multiplication, division unary minus and parenthesized expression. Precedence and associativity of
operators should be taken care.

Using lex and yacc design a simple parser for parsing SQL statement. The generated grammar should be
able parse simple form of SELECT statements.

Program & Output:

Page | 58
Rizvi College of Engineering CSL601 (SPCC)

Page | 59
Rizvi College of Engineering CSL601 (SPCC)

Page | 60
Rizvi College of Engineering CSL601 (SPCC)

Page | 61
Rizvi College of Engineering CSL601 (SPCC)

Page | 62
Rizvi College of Engineering CSL601 (SPCC)

Pre-Lab & Post-Lab Answer:

Conclusion:
………………………………………………………………………………………………..........................
………………………………………………………………………………………………………………..
CO’s Covered:
………………………………………………………………………………………………..........................
……………………………………………………………………………………………………………….

Page | 63
Rizvi College of Engineering CSL601 (SPCC)
Rizvi College of Engineering CSL601 (SPCC)

ASSIGNMENTS
Rizvi College of Engineering CSC601

Assignment. No. 1

Rubric Score (0 to 5)
Problem solving and
Algorithm Analysis
Presentation
Problem readability and
interpretation of results
Total

Submitted On:

Sign:
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601

Assignment. No. 2

Rubric Score (0 to 5)
Problem solving and
Algorithm Analysis
Presentation
Problem readability and
interpretation of results
Total

Submitted On:

Sign:
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601
Rizvi College of Engineering CSC601

You might also like