Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

JasonDsouza - 9537 - Batch A

Download as pdf or txt
Download as pdf or txt
You are on page 1of 114

Fr.

Conceicao Rodrigues College of Engineering


Fr. Agnel Ashram, Bandstand, Bandra(W)
Mumbai – 400050
Department of Computer Engineering
2023 - 24
Subject: SPCC (Sem VI) CSC601

Student Name: Jason James Dsouza Roll No.: 9537

Sr. Experiment Title Submission Remarks


No. Date

1. To write a program for implementing


Symbol Table.
2. Write a program to implement Lexical
analyzer.
3. Design recursive descent parser.

4. To generate an Intermediate code

5. Study of Lexical analyzer tool -Flex/Lex


and Yacc
6. Generate a target code for the optimized
code.
7. Write a program to implement Two Pass
Assembler
8. Write a program to implement two pass
Macro Processor
9. Assignment 1

10. Assignment 2

11. Assignment 3
Department of Computer Engineering

Academic Term : Jan-May 23-24

Class : T.E. (Computer)


Subject Name : System Programming and Compiler Construction
Subject Code : (CPC601)

Practical No: 1

Title: implementing a symbol table

Date of Performance: 2/2/24

Date of Submission: 9/2/24

Roll No: 9537

Name of the Student: jason dsouza

Evaluation:

Sr. No Rubric Grade


1 Time Line (2)
2 Output(3)
3 Code optimization (2)
4 Postlab (3)

Signature of the Teacher :


9537 jason
Department of Computer Engineering

Academic Term : Jan-May 23-24

Class : T.E. (Computer)


Subject Name : System Programming and Compiler Construction
Subject Code : (CPC601)

2
Practical No:
Implementing Lexical Analyzer
Title:
12/02/2024
Date of Performance:
26/2/24
Date of Submission:
9537
Roll No:
Jason dsouza
Name of the Student:

Evaluation:

Sr. No Rubric Grade


1 Timeline (2)
2 Output (3)
3 Code optimization (2)
4 Postlab (3)

Signature of the Teacher :


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Experiment No 2

Aim: Write a program to implement Lexical analyzer

Learning Objective: Converting a sequence of characters into a sequence of tokens.

Theory:
THE ROLE OF LEXICAL ANALYZER

The lexical analyzer is the first phase of a compiler. Its main task is to read the input

characters and produce as output a sequence of tokens that the parser uses for syntax analysis. Upon
receiving a “get next token” command from the parser, the lexical analyzer reads input characters
until it can identify the next token.

Figure 4.1 Interaction of Lexical Analyzer with Parser

Since the lexical analyzer is the part of the compiler that reads the source text, it may also perform
certain secondary tasks at the user interface. One such task is stripping out from the source program
comments and white spaces in the form of blank, tab, and new line characters. Another is correlating
error messages from the compiler with the source program. Sometimes lexical analyzers are divided
into a cascade of two phases first called “scanning” and the second “lexical analysis”. The scanner is
responsible for doing simple tasks, while the lexical analyzer proper does the more complex operations.
Implementation Details

1. Read the high level language as source program


2. Convert source program into categories of tokens such as Identifiers, Keywords, Constants,
Literals and Operators.
Test cases:

1. Input undefined token

Source Code:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.util.Arrays;

public class SPCC_Exp_2 {


public static void main(String[] args) {

RefinedSymbolTable symbolTable = new RefinedSymbolTable();


File file = new File("SPCC_Data.txt");
Scanner sc = new Scanner(System.in);
String[] skipSymbols = {"(", ")", "{", "}"};

System.out.println("Reading tokens from file...\n");


try (Scanner fileReader = new Scanner(file)) {
while (fileReader.hasNext()) {
String t1 = fileReader.next();
if (Arrays.asList(skipSymbols).contains(t1)) {
continue;
}
RefinedToken token = new RefinedToken(t1);
symbolTable.addToken(token);
}
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("File read successfully!\n");


symbolTable.displayTable();
}
}

class RefinedToken extends Token {

private String token;

public RefinedToken(String token) {


this.token = token;
setType();
}

public void setType() {


String[] literals = new String[] {"true", "false", "null"};
String[] keywords = new String[] {"int", "float", "char", "double", "boolean", "void", "if", "else",
"for", "while", "do", "switch", "case", "break", "continue", "return"};
if (Arrays.asList(keywords).contains(token)) {
type = "Keyword ";
} else if (Arrays.asList(literals).contains(token)) {
type = "Literal ";
} else if (token.matches("[a-zA-Z]+[0-9]*")) {
type = "Identifier";
} else if (token.matches("[0-9]+")) {
type = "Constant";
} else if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/") ||
token.equals("^") || token.equals("=")) {
type = "Operator";
} else {
type = "Undefined";
}
}

public String getToken() {


return token;
}
}

class RefinedSymbolTable extends SymbolTable {

private ArrayList<RefinedToken> tokens;

public RefinedSymbolTable() {
tokens = new ArrayList<>();
}

public void addToken(RefinedToken token) {


if (!tokenInTable(token)) {
tokens.add(token);
// System.out.println("Token added!");
} else {
// System.out.println("Duplicate token!");
}
}

private boolean tokenInTable(RefinedToken t1) {


for (RefinedToken token : tokens) {
if (token.getToken().equals(t1.getToken())) {
return true;
}
}
return false;
}

public void displayTable() {


if (tokens.isEmpty()) {
System.out.println("Symbol Table is empty!");
} else {
System.out.println("---------------Symbol Table -------------- ");
System.out.println("Symbol\t\tType\t\tAddress");
for (RefinedToken token : tokens) {
System.out.println(token.getToken() + "\t\t" + token.getType() + "\t" + token);
}
}
}
}

Output:

SPCC_Data.txt
Conclusion: Thus, we have successfully read file input consisting of various expressions and
converted them into tokens stored in the symbol table.
Post Lab Questions:

1. Explain the role of automata theory in compiler design.


2. What are the errors that are handled by the Lexical analysis pha
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬

‭Department of Computer Engineering‬

‭Academic Term : Jan-May 23-24‬

‭ lass‬
C ‭: T.E. (Computer)‬
‭Subject Name :‬‭System Programming and Compiler Construction‬
‭Subject Code :‬‭(CPC601)‬

‭3‬
‭Practical No:‬
‭Design recursive descent parser‬
‭Title:‬
‭09/02/2024‬
‭Date of Performance:‬

‭Date of Submission:‬ ‭16/02/2024‬

‭9564‬
‭Roll No:‬
‭Vedant Pathare‬
‭Name of the Student:‬

‭Evaluation:‬

‭Sr. No‬ ‭Rubric‬ ‭Grade‬


‭1‬ ‭Timeline (2)‬
‭2‬ ‭Output (3)‬
‭3‬ ‭Code optimization (2)‬
‭4‬ ‭Postlab (3)‬

‭Signature of the Teacher‬ ‭:‬


‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬

‭Experiment No 3‬

‭Aim :‬‭Design recursive descent parser.‬


‭Theory :‬
‭ ‬ ‭recursive‬ ‭descent‬ ‭parser‬ ‭is‬ ‭a‬ ‭kind‬ ‭of‬ ‭top-down‬ ‭parser‬ ‭built‬ ‭from‬ ‭a‬ ‭set‬ ‭of‬
A
‭mutually-recursive‬ ‭procedures‬ ‭(or‬ ‭a‬ ‭non-recursive‬ ‭equivalent)‬ ‭where‬ ‭each‬ ‭such‬ ‭procedure‬
‭usually‬ ‭implements‬ ‭one‬ ‭of‬ ‭the‬ ‭production‬ ‭rules‬ ‭of‬ ‭the‬ ‭grammar.‬ ‭Thus‬ ‭the‬ ‭structure‬ ‭of‬ ‭the‬
‭resulting program closely mirrors that of the grammar it recognizes.‬
‭ his‬‭parser‬‭attempts‬‭to‬‭verify‬‭that‬‭the‬‭syntax‬‭of‬‭the‬‭input‬‭stream‬‭is‬‭correct‬‭as‬‭it‬‭is‬‭read‬‭from‬
T
‭left‬ ‭to‬ ‭right.‬ ‭A‬ ‭basic‬‭operation‬‭necessary‬‭for‬‭this‬‭involves‬‭reading‬‭characters‬‭from‬‭the‬‭input‬
‭stream‬‭and‬‭matching‬‭them‬‭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.‬
‭ hat a recursive descent parser actually does is to perform a depth-first search of the‬
W
‭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.‬
‭As our first example, consider the simple grammar‬
‭ ® x+T‬
E
‭T ® (E)‬
‭T ® x‬
‭and the derivation tree in figure 2 for the expression x+(x+x)‬

‭Derivation Tree for x+(x+x)‬


‭ recursive descent parser traverses the tree by first calling a procedure to recognize an E.‬
A
‭This procedure reads an 'x' and a '+' and then calls a procedure to recognize a T. This would‬
‭look like the following routine.‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬

‭Procedure for E‬
‭ ote‬‭that‬‭the‬‭'next'‬‭looks‬‭ahead‬‭and‬‭always‬‭provides‬‭the‬‭next‬‭character‬‭that‬‭will‬‭be‬‭read‬‭from‬
N
‭the‬‭input‬‭stream.‬‭This‬‭feature‬‭is‬‭essential‬‭if‬‭we‬‭wish‬‭our‬‭parsers‬‭to‬‭be‬‭able‬‭to‬‭predict‬‭what‬‭is‬
‭due to arrive as input.‬
‭ ote that 'errorhandler' is a procedure that notifies the user that a syntax error has been made‬
N
‭and then possibly terminates execution.‬
I‭ n order to recognize a T, the parser must figure out which of the productions to execute. This‬
‭is not difficult and is done in the procedure that appears below.‬

‭Procedure for T‬

I‭ n the above routine, the parser determines whether T had the form (E) or x. If not then the‬
‭error routine was called, otherwise the appropriate terminals and nonterminals were‬
‭recognized.‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬

‭Algorithm:‬
‭1. Make grammar suitable for parsing i.e. remove left recursion(if required).‬
‭2. Write a function for each production with error handler.‬
3‭ .‬ ‭Given‬ ‭input‬ ‭is‬ ‭said‬ ‭to‬ ‭be‬ ‭valid‬ ‭if‬ ‭input‬ ‭is‬ ‭scanned‬ ‭completely‬ ‭and‬ ‭no‬ ‭error‬ ‭function‬ ‭is‬
‭called.‬

‭Source Code:‬

‭import java.util.Scanner;‬

‭public class SPCC_Exp_3 {‬

‭public static void main(String[] args) {‬


‭RecursiveDecentParser parser = new RecursiveDecentParser();‬
‭parser.parse();‬
‭}‬

‭}‬

‭class RecursiveDecentParser {‬
‭private String input;‬
‭private Scanner inputScanner;‬

‭RecursiveDecentParser() {‬
‭acceptInputString();‬
‭inputScanner = new Scanner(input);‬
‭}‬

‭private void acceptInputString() {‬


‭Scanner sc = new Scanner(System.in);‬
‭System.out.print("Enter input string: ");‬
‭input = sc.nextLine();‬
‭sc.close();‬
‭}‬

‭public void parse() {‬


‭procedureE();‬
‭System.out.println("Input string parsed successfully!");‬
‭}‬

‭private void procedureE() {‬


‭try {‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬

‭ tring nextSymbol = inputScanner.next();‬


S
‭if (nextSymbol.equals("x")) {‬
‭nextSymbol = inputScanner.next();‬
‭if (nextSymbol.equals("+")) {‬
‭procedureT();‬
‭} else {‬
‭errorHandler();‬
‭}‬
‭} else {‬
‭errorHandler();‬
‭}‬
‭} catch(Exception e) {‬
‭errorHandler();‬
‭}‬
‭}‬

‭private void procedureT() {‬


‭try {‬
‭String nextSymbol = inputScanner.next();‬
‭if (nextSymbol.equals("(")) {‬
‭procedureE();‬
‭nextSymbol = inputScanner.next();‬
‭if (nextSymbol.equals(")")) {‬
‭return;‬
‭} else {‬
‭errorHandler();‬
‭}‬
‭} else if (nextSymbol.equals("x")) {‬
‭return;‬
‭} else {‬
‭errorHandler();‬
‭}‬
‭} catch(Exception e) {‬
‭errorHandler();‬
‭}‬
‭}‬

‭private void errorHandler() {‬


‭System.out.println("Input String not valid!");‬
‭System.exit(0);‬
‭}‬

‭}‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬

‭Output:‬

‭ onclusion:‬‭Thus,‬‭we‬‭have‬‭successfully‬‭implemented‬‭a‬‭recursive‬‭descent‬‭parser‬‭and‬‭verified‬
C
‭that it can correctly parse valid strings.‬

‭Postlab:‬
‭1.‬ ‭What is left Recursion ? Write the rules for removing left recursion.‬
‭2.‬ ‭What is left factoring ? Write rules for eliminating left factoring.‬
‭3.‬ ‭Difference between top down and Bottom up parsing‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬


‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬


‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 22-23‬


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Department of Computer Engineering

Academic Term : Jan-May 23-24

Class : T.E. (Computer)


Subject Name : System Programming and Compiler Construction
Subject Code : (CPC601)

Practical No: 4

Title: To generate an Intermediate code.

Date of Performance: 08/04/2024

Date of Submission: 08/04/2024

Roll No: 9537

Name of the Student: Jason Dsouza

Evaluation:

Sr. No Rubric Grade


1 Timeline (2)
2 Output (3)
3 Code optimization (2)
4 Postlab (3)

Signature of the Teacher :


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Experiment No 4

Aim : To generate an Intermediate code.


Description:

● If a compiler translates the source language to its target machine language without
having the option for generating intermediate code, then for each new machine, a full
native compiler is required.
● Intermediate code eliminates the need of a new full compiler for every unique
machine by keeping the analysis portion same for all the compilers.
● The second part of compiler, synthesis, is changed according to the target machine.
● It becomes easier to apply the source code modifications to improve code
performance by applying code optimization techniques on the intermediate code.

● Three-Address Code
Intermediate code generator receives input from its predecessor phase, semantic
analyzer, in the form of an annotated syntax tree. That syntax tree then can be
converted into a linear representation, e.g., postfix notation. Intermediate code tends
to be machine independent code. Therefore, code generator assumes to have
unlimited number of memory storage (register) to generate code.
● For example:
● a = b + c * d;
● The intermediate code generator will try to divide this expression into
sub-expressions and then generate the corresponding code.
● r1 = c * d;
● r2 = b + r1;
● a = r2

● r being used as registers in the target program.


● A three-address code has at most three address locations to calculate the expression.
A three-address code can be represented in two forms : quadruples and triples.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Quadruples
Each instruction in quadruples presentation is divided into four fields: operator, arg1, arg2,
and result. The above example is represented below in quadruples format:

Op arg1 arg2 result

* c d r1

+ b r1 r2

+ r2 r1 r3

= r3 a

Triples
Each instruction in triples presentation has three fields : op, arg1, and arg2.The results of
respective sub-expressions are denoted by the position of expression. Triples represent
similarity with DAG and syntax tree. They are equivalent to DAG while representing
expressions.

Op arg1 arg2

* c d

+ b (0)

+ (1) (0)

= (2)

Triples face the problem of code immovability while optimization, as the results are
positional and changing the order or position of an expression may cause problems.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Indirect Triples
This representation is an enhancement over triples representation. It uses pointers instead of
position to store results. This enables the optimizers to freely re-position the sub-expression
to produce an optimized code.

Source Code:
import java.util.Scanner;

public class SPCC_Exp_4 {

private static int tempCount = 1;

public static void main(String[] args)


{ Scanner scanner = new
Scanner(System.in);

while (true) {
System.out.println("Generate three-address code for: 1)Assignment 2)Conditional
3)Indexed Assignment 4)Copy 5)Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
System.out.println();

switch (choice)
{case 1:
generateAssignmentCode();
break;
case 2:
generateConditionalCode();
break;
case 3:
generateIndexedAssignmentCode();
break;
case 4:
generateCopyCode();
break;
case 5:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number from 1 to 5.");
}
System.out.println();
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

}
}

private static void generateAssignmentCode()


{ String expression = "a = b * c + d";
System.out.println("Expression: " + expression);
String[] parts = expression.split(" = ");
String lhs = parts[0].trim();
String rhs = parts[1].trim();

String[] terms = rhs.split(" ");


String result = lhs;
StringBuilder code = new StringBuilder();

for (int i = 0; i < terms.length; i++) {


if (terms[i].equals("+") || terms[i].equals("*"))
{String op = terms[i];
String operand1 = terms[i - 1];
String operand2 = terms[i + 1];
String temp = "t" + tempCount++;
code.append(temp).append(" = ").append(operand1).append("
").append(op).append(" ").append(operand2).append(";\n");
terms[i + 1] = temp;
}
}

code.append(result).append(" = ").append(terms[terms.length - 1]).append(";");


System.out.println("Three-address code:");
System.out.println(code.toString());
}

private static void generateConditionalCode() {


String expression = "if (a < b) then c = d + e else c = d - e";
System.out.println("Expression: " + expression);
String[] parts = expression.split(" then ");
String condition = parts[0].substring(3).trim();
String thenPart = parts[1].split(" else ")[0].trim();
String elsePart = parts[1].split(" else ")[1].trim();

String trueLabel = "L" + tempCount++;


// String falseLabel = "L" + tempCount++;
String endLabel = "L" + tempCount++;

StringBuilder code = new StringBuilder();


code.append("if ").append(condition).append(" goto
").append(trueLabel).append(";\n");
code.append(elsePart).append(" goto ").append(endLabel).append(";\n");
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

code.append(trueLabel).append(": ").append(thenPart).append(" goto


").append(endLabel).append(";\n");
code.append(endLabel).append(": ");

System.out.println("Three-address code:");
System.out.println(code.toString());
}

private static void generateCopyCode()


{String expression = "x = y";
System.out.println("Expression: " + expression);
String[] parts = expression.split(" = ");
String target = parts[0].trim();
String source = parts[1].trim();

StringBuilder code = new StringBuilder();


code.append(target).append(" = ").append(source).append(";");

System.out.println("Three-address code:");
System.out.println(code.toString());
}

private static void generateIndexedAssignmentCode()


{String expression = "x[2] = y + 5";
System.out.println("Expression: " + expression);

String[] parts = expression.split(" = ");


String target = parts[0].trim();
String operation = parts[1].trim();

int startIndex = target.indexOf('[');


int endIndex = target.indexOf(']');
String arrayName = target.substring(0, startIndex).trim();
String index = target.substring(startIndex + 1, endIndex).trim();

String[] operationParts = operation.split("\\s+");

String operand1 = operationParts[0];


String operator = operationParts[1];
String operand2 = operationParts[2];

String intermediateVar = "T" + tempCount++;


StringBuilder code = new StringBuilder();
code.append(intermediateVar).append(" = ").append(operand1).append("
").append(operator).append(" ").append(operand2).append(";\n");
code.append(arrayName).append("[").append(index).append("] =
").append(intermediateVar).append(";");
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

System.out.println("Three-address code:");
System.out.println(code.toString());
}
}

Output:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Postlab Question
1. Write the intermediate code generated for------while ( a<b ) do
If ( c< d) then
X= y+z
Else
X= y-z
2. Write the intermediate code generated for----- switch E
Begin
case V1 : S1
case V2 : S2
….
default: Sn
end
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


Department of Computer Engineering

Academic Term : Jan-May 23-24

Class : T.E. (Computer)


Subject Name : System Programming and Compiler Construction
Subject Code : (CPC601)

5
Practical No:

Title: Lec and Yacc

Date of Performance: 8.04.23

Date of Submission: 9.04.24

Roll No: 9537

Name of the Student: Jason Dsouza

Evaluation:

Sr. No Rubric Grade


1 Time Line (2)
2 Output(3)
3 Code optimization (2)
4 Postlab (3)

Signature of the Teacher :


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

Experiment No 5

Aim : Study of Lexical analyzer tool -Flex/Lex


Learning Objective: Recognise lexical pattern from given input file.
Theory:

Lex is a program generator designed for lexical processing of character input streams.
Itaccepts a high-level, problem oriented specification for character string matching,
andproduces a program in a general purpose language which recognizes regular
expressions.The regular expressions are specified by the user in the source specifications
given to Lex.The Lex written code recognizes these expressions in an input stream and
partitions theinput stream into strings matching the expressions. At the boundaries between
stringsprogram sections provided by the user are executed. The Lex source file associates
theregular expressions and the program fragments. As each expression appears in the input
tothe program written by Lex, the corresponding fragment is executed.The user supplies the
additional code beyond expression matching needed to complete histasks, possibly including
code written by other generators. The program that recognizesthe expressions is generated in
the general purpose programming language employed for theuser's program fragments. Thus,
a high level expression language is provided to write thestring expressions to be matched
while the user's freedom to write actions isunimpaired. This avoids forcing the user who
wishes to use a string manipulationlanguage for input analysis to write processing programs
in the same and ofteninappropriate string handling language.

Lex is not a complete language, but rather a generator representing a new language
featurewhich can be added to different programming languages, called ``host languages.'' Just
asgeneral purpose languages can produce code to run on different computer hardware,
Lexcan write code in different host languages. The host language is used for the output
codegenerated by Lex and also for the program fragments added by the user. Compatible
runtimelibraries for the different host languages are also provided. This makes Lex
adaptableto different environments and different users. Lex itself exists on UNIX, GCOS, and
OS/370; but the code generated by Lex may be taken anywhere where appropriate compilers
exist.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

Lex turns the user's expressions and actions (called source in this pic) into the hostgeneral-
purpose language; the generated program is named yylex. The yylex program willrecognize
expressions in a stream (called input in this pic) and perform the specifiedactions for each
expression as it is detected.

For a trivial example, consider a program to delete from the input all blanks or tabs at the

ends of lines.

%%

[ \t]+$ ;

is all that is required. The program contains a %% delimiter to mark the beginning of
therules, and one rule. This rule contains a regular expression which matches one or
moreinstances of the characters blank or tab (written \t for visibility, in accordance with the
Clanguage convention) just prior to the end of a line. The brackets indicate the characterclass
made of blank and tab; the + indicates ``one or more ...''; and the $ indicates ``end ofline,'' as
in QED. No action is specified, so the program generated by Lex (yylex) willignore these
characters. Everything else will be copied. To change any remaining string ofblanks or tabs to
a single blank, add another rule:

%%
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

[ \t]+$ ;

[ \t]+ printf(" ");

The finite automaton generated for this source will scan for both rules at once, observingat
the termination of the string of blanks or tabs whether or not there is a newlinecharacter, and
executing the desired rule action. The first rule matches all strings of blanksor tabs at the end
of lines, and the second rule all remaining strings of blanks or tabs.

Lex can be used alone for simple transformations, or for analysis and statistics gatheringon a
lexical level. Lex can also be used with a parser generator to perform the lexicalanalysis
phase.

The general format of Lex source is:

{definitions}

%%

{rules}

%%

{user subroutines}

where the definitions and the user subroutines are often omitted. The second %% isoptional,
but the first is required to mark the beginning of the rules. The absoluteminimum Lex
program is thus

%%

(no definitions, no rules) which translates into a program which copies the input to theoutput
unchanged.In the outline of Lex programs shown above, the rules represent the user's
controldecisions; they are a table, in which the left column contains regular expressions and
the rightcolumn contains actions, program fragments to be executed when the expressions
arerecognized. Thus an individual rule might appearinteger printf("found keyword INT");to
look for the string integer in the input stream and print the message ``found keyword
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

INT''whenever it appears. In this example the host procedural language is C and the C
libraryfunction printf is used to print the string. The end of the expression is indicated by the
firstblank or tab character. If the action is merely a single C expression, it can just be given on
theright side of the line; if it is compound, or takes more than a line, it should be enclosed
inbraces.

Implementation Details:

1. Open file in text editor

2. Enter keywords, rules for identifier and constant, operators and relational operators. In

the following format

a) %{

Definition of constant /header files

%}

b) Regular Expressions

%%

Transition rules

%%

c) Auxiliary Procedure (main( ) function)

3. Save file with .l extension e.g. Mylex.l

4. Call lex tool on the terminal e.g. [root@localhost]# lex Mylex.l This lex tool will convert

“.l” file into “.c” language code file i.e. lex.yy.c

5. Compile the file lex.yy.c e.g. gcc lex.yy.c .After compiling the file lex.yy.c, this will

create the output file a.out


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

6. Run the file a.out e.g. ./a.out

7. Give input on the terminal to the a.out file upon processing output will be displayed

Sample Code

%{
#include<stdio.h>
int key_word=0;
%}
%%
"include"|"for"|"define" {key_word++;}
%%
int main()
{
printf("enter the sentence");
yylex();
printf("keyword are: %d\n ",key_word);
}
int yywrap() { return 1; }

Example: Program for counting number of vowels and consonant

%{

#include <stdio.h>

int vowels = 0;

int consonants = 0;

%}

%%
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

[aeiouAEIOU] vowels++;

[a-zA-Z] consonants++;

[\n] ;

.;

%%

int main()

printf ("This Lex program counts the number of vowels and ");

printf ("consonants in given text.");

printf ("\nEnter the text and terminate it with CTRL-d.\n");

yylex();

printf ("Vowels = %d, consonants = %d.\n", vowels, consonants);

return 0;

Output:

#lex alphalex.l

#gcc lex.yy.c

#./a.out

This Lex program counts the number of vowels and consonants in given text.

Enter the text and terminate it with CTRL-d.


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

Iceream

Vowels =4, consonants =3.

Test Cases:

1. Input integer constant


2. Input special symbols
Conclusion:

Aim :Study of Parser generator tool –Yacc


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

Theory:

Parser for a grammar is a program which takes in the language string as its input and
produces either a corresponding parse tree or a error.Syntax of a LanguageThe rules which
tells whether a string is a valid program or not are called the syntaxSemantic s of Language
The rules which give meaning to programs are called the semantic of a languageTokensWhen
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.
Lexical Analysis

The function of lexical Analyzer is to read the input stream representing the source program ,
one character at a time and translate into valid tokens.

Implementation Details
1: Create a lex file

The general format for lex file consists of three sections:

1. Definitions

2. Rules

3. User subroutine Section

Definitions consists of any external ‘C’ definitions used in the lex actions or subroutines. The
other types of definitions are 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 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) grammer 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 here are few Yacc specific declarations
which begins with a % sign.
1. %union It defines the Stack type for the Parser.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

It is union of various datas/structures/objects.


2. % token These are the terminals returned by the yylex function to the yacc. A token
cal 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>
tokenName.
3. %type The type of non-terminal symbol in the grammar rule can be specified with
this. The format is %type <stack member> non termainal.
4. %noassoc Specifies that there is no associativity of a terminal symbol.
5. %left Specifies the left associativity of aterminal symbol.
6. %rightSpecifies the right associativity of a terminal symbol.
7. %start specifies the L.H.S. non-terminal symbol of a production rule which specifies
starting point of grammar rules.
8. %prac 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 of coding the rules is to have all Terminals in lower –case and all
non-terminals in upper –case.
To facilitate a proper syntax directed translation the Yacc has something calls pseudo-
variables which forms a bridge between the values of terminals/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, so is the $2 etc. The default
type for pseudo variables is integer unless they are specified by % type.
%token <type> etc.

Perform the following steps, in order, to create the desk calculator example program:

1. Process the yacc grammar file using the -d optional flag (which tells the yacc
command to create a file that defines the tokens used in addition to the C language
source code):
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

yacc -d calc.yacc

2. Use the li command to verify that the following files were created:
y.tab.c The C language source file that the yacc command created for the parser.
y.tab.h A header file containing define statements for the tokens used by the parser.
3. Process the lex specification file:
lex calc.lex

4. Use the li command to verify that the following file was created:

lex.yy.c The C language source file that the lex command created for the lexical
analyzer.

5. Compile and link the two C language source files:

cc y.tab.c lex.yy.c

6. Use the li command to verify that the following files were created:
y.tab.o The object file for the y.tab.c source file
lex.yy.o The object file for the lex.yy.c source file
a.out The executable program file

7. To then run the program directly from the a.out file, enter:
8. $ a.out
Conclusion:

Postlab:

1. Write the structure of Lex


2. Write the structure of Yacc
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

1. Find number of vowels and consonants in the sentence

%{

int vow_count=0;

int const_count =0;

%}

%%

[aeiouAEIOU] {vow_count++;}

[a-zA-Z] {const_count++;}

%%

int yywrap(){}

int main()

printf("Enter the string of vowels and consonants:");

yylex();

printf("Number of vowels are: %d\n", vow_count);

printf("Number of consonants are: %d\n", const_count);

return 0;

2. Operands Operations
%{
#include<stdio.h>
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

int opr=0, opd=0, n=0;

%}

%%

[\+\-\*\/=] { printf("OPERATOR: %s\n", yytext); opr++; }

[a-zA-Z]+ { printf("OPERAND: %s\n", yytext); opd++; }

[0-9]+ { printf("NUMBER: %s\n", yytext); opd++; }

[a-zA-Z]+[\+\-\*\/][a-zA-Z]+ { n=0; }

[0-9]+[\+\-\*\/][0-9]+ { n=0; }

%%

int yywrap()

{return 1;

int main()

printf("Enter the expression: ");

yylex();

printf("\nNumber of operators: %d", opr);

printf("\nNumber of operands: %d", opd);

if (n == 0 && opd == opr + 1)

printf("\nValid expression\n");

else

printf("\nInvalid expression\n");

return 0;

}
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

3. Find Positive and negative integers and positive and negative fractions in the text file

%{

int postiveno=0;

int negtiveno=0;

int positivefractions=0;

int negativefractions=0;

%}

DIGIT [0-9]

%%

\+?{DIGIT}+ postiveno++;

-{DIGIT}+ negtiveno++;

\+?{DIGIT}*\.{DIGIT}+ positivefractions++;

-{DIGIT}*\.{DIGIT}+ negativefractions++;

.;

%%

void main()

yylex();

printf("\nNo. of positive numbers: %d",postiveno);

printf("\nNo. of Negative numbers: %d",negtiveno);

printf("\nNo. of Positive fractions: %d",positivefractions);

printf("\nNo. of Negative fractions: %d\n",negativefractions);

}
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

input file - a.txt


a.txt - (+12,-123,1.1,-1.1,12,-2,-3,2.1,3.2,5.1,-5.5,-6.1,-7.7,-8.8)

4. Find number of lines ,words , small letters, capital letters , special character, digits and total
characters

%{

#include<stdio.h>

int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;

%}

%%

\n { lines++; words++;}

[\t ' '] words++;

[A-Z] c_letters++;

[a-z] s_letters++;

[0-9] num++;

. spl_char++;

%%

main(void)

yyin= fopen("test.txt","r");

yylex();

total=s_letters+c_letters+num+spl_char;

printf(" This File contains ...");

printf("\n\t%d lines", lines);

printf("\n\t%d words",words);

printf("\n\t%d small letters", s_letters);


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

printf("\n\t%d capital letters",c_letters);

printf("\n\t%d digits", num);

printf("\n\t%d special characters",spl_char);

printf("\n\tIn total %d characters.\n",total);

int yywrap()

return(1);

5. Find number of comment lines and eliminate the comment lines


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24

postlab
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023-24


Department of Computer Engineering

Academic Term : Jan-May 23-24

Class : T.E. (Computer)


Subject Name : System Programming and Compiler Construction
Subject Code : (CPC601)

Practical No: 6

Target Code Generator


Title:

Date of Performance:

Date of Submission:

Roll No: 9537

Name of the Student: Jason dsouza

Evaluation:

Sr. No Rubric Grade


1 Time Line (2)
2 Output(3)
3 Code optimization (2)
4 Postlab (3)

Signature of the Teacher :


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24

Experiment No 6
Aim : Generate a target code for the optimized code.
Algorithm:
The final phase in compiler model is the code generator. It takes as input an intermediate
representation of the source program and produces as output an equivalent target program.

The code generation algorithm takes as input a sequence of three address statements
constituting a basic block. For each three address statement of the form x=y op z we perform
following function:

1. Invoke a function getreg to determine the location L where the result of computation y op
z should be stored. ( L cab be a register or memory location .

2. Consult the address descriptor for y to determine y, the current locations of y. Prefer the
register for y if the value of y is currently both in memory and register. If value of y is not
already in L , generate the instruction MOV y, L to place a copy of y in L.

3. Generate instruction po z, L where z is a current location of z. Again address descriptor of


x to indicate that x is in location L. if L is a register, update its descriptor to indicate that it
contains the value of x, and remove x from all other register descriptor.

4. If the current values of y and z have no next uses , are not live on exit from the block , and
are in registers alter the register descriptor to indicate that , after execution of x=y op z, those
register no longer will contain y and z, resply.

The function getreg:


The function getreg returns the location L to hold the values of x for the assignment x= y opz.
1. If the name y is in a reg that holds the value of no other names, and y is not live and has no
next use after execution of x= y op z ,then return the register of y for L. Update the address
descriptor of y to indicate that y is no longer in L.
2. Failing (1), return an empty register for L if there one.

3. Failing (2) , if X has a next use in the block, or op is an operator , such as indexing, that
requires a register find an occupied register R. Store the values of R into a memory location
( MOV R ,M) if it is not already in the proper memory location M, update the address
descriptor for M , and return R. if R holds the value of several variables, a MOV
instruction must be generated for each variable that needs to be stored. A suitable register
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24

might be one whose data is referenced furthest in the future, or one whose value is also in
memory. We leave the exact choice unspecified, since there is no one proven best way to
make the selection.

4. If x is not used in the block , or no suitable occupied register can found, select the memory
location of x as L.

Conclusion:

Postlab:
Explain design issues of code generator phase?
What are basic blocks? State their properties
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24

import re

operatorTable = {

"+": "ADD",

"-": "SUB",

"*": "MUL",

"/": "DIV",

registerTable = {}

def getRegisterByIndex(index):

for [key, value] in enumerate(registerTable):

if value == 'R' + index:

return key

return -1

def getRegisterByOperand(operand):

if operand not in registerTable.keys():

registerTable[operand] = "R" + str(len(registerTable))

return registerTable[operand]

def parseLine(line):
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


line = line.strip().replace(" ", "")

print(line)

# Check for equals

if '=' not in line:

return

expression = line.split("=")

operands = re.split(r'[-+*/()]', expression[1])

operators = re.findall(r'[-+*/()]', expression[1])

# print(operands)

# print(operators)

setVarCode = ""

operationCode = ""

for op in operators:

if op not in operatorTable:

raise "Invalid Operator " + op

operationCode += operatorTable[op] + " "

# Get Operand 1 and its register

operand1 = operands.pop(0)

# Allocate Register to operand


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


if operand1 not in registerTable.keys():

setVarCode += f"MOV {operand1},"

register1 = getRegisterByOperand(operand1)

if len(setVarCode) > 1:

setVarCode += register1

# Get Operand 2 and its register

operand2 = operands.pop(0)

if operand2 in registerTable.keys():

operand2 = getRegisterByOperand(operand2)

operationCode += f"{operand2},{register1}"

if len(setVarCode) > 0:

print(setVarCode)

if len(operationCode) > 0:

print(operationCode)

print(f"{expression[0]} : {getRegisterByOperand(expression[0])}")

print()

code = open("code.txt").readlines()

for line in code[:]:

parseLine(line)

code text
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


t=b+c

v=d+e

u=t-v

w = t+u

a=w

Output
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023 - 24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Experiment No 7

Department of Computer Engineering

Academic Term : Jan-May 23-24

Class : T.E. (Computer)


Subject Name : System Programming and Compiler Construction
Subject Code : (CPC601)

Practical No: 7

Write a program to implement Two Pass Assembler


Title:

Date of Performance:

Date of Submission:

Roll No: 9537

Name of the Student: Jason Dsouza

Evaluation:

Sr. No Rubric Grade


1 Timeline (2)
2 Output (3)
3 Code optimization (2)
4 Postlab (3)

Signature of the Teacher :


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Aim: Write a program to implement Two Pass Assembler.

Learning Objective: Translating mnemonic operation codes to their machine language equivalents.
Assigning machine addresses to symbolic labels used by the programmer. Lastly to convert assembly
language to binary.

Algorithm:

Pass 1:

1. Start

2.Intialize location counter to zero

3.Read opcode field of next instruction.

4.search opcode in pseudo opcode Table(POT)

5.If opcode is found in POT

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

5.2 If it is ‘EQU’

Evaluate operand field

Assign values to symbol in label field

Go to step 3

5.3 If it is ‘USING’ or ‘DROP’ Go to step 3

5.4 If it is ‘END’

Assign value to symbol in label field

Go to step 3
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

SystemOpcode
6. Search opcode in Machine Programming
Table. and Compiler Construction

7. Assign its length to ‘L’. VI Semester ( Computer) Academic Year: 23-24

8. Process any literals and enter them into literal Table.

9.If symbol is there in the label field

Assign current value of Location Counter to symbol

10. Location Counter= Location Counter +L.

11.Go to step 3.

12. Stop.

Pass2:

1. Start

2. Intialize location counter to zero.

3. Read opcode field of next instruction.

4.Search opcode in pseudo opcode table.

5. If opcode is found in pseudo opcode Table

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 6.4

5.2 If it is ‘EQU’ or ‘START’ ignore it. Go to step 3

5.3 If it is ‘USING’

Evaluate operand and enter base reg no. and value into base table

Go to step 3

5.4 If it is ‘DROP’

Indicate base reg no . available in base table . Go to step 3

5.5 If it is ‘END’

Generate literals for entries in Literal Table

Go to step 12

6 Search opcode in MOT


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System
7. Get opcode byte and Programming and Compiler Construction
format code
VI Semester ( Computer) Academic Year: 23-24
8. Assign its length to ‘L’.

9. Check type of instruction.

10.If it is type ‘RR’ type

10.1 Evaluate both register expressions and insert into second byte.

10.2 Assemble instruction

10.3 Location Counter= Location Counter +L.

10.4.Go to step 3.

11. If it is ‘RX’ type

11.1 Evaluate register and index expressions and insert into second byte.

11.2 Calculate effective address of operand.

11.3 Determine appropriate displacement and base register

11.4 Put base and displacement into bytes 3 and 4

11.5 Location Counter= Location Counter +L.

11.6 Go to step 11.2

13 Stop.

Implementation Details

1. Read Assembly language input file.

2. Display output of Pass1 as the output file with Op-code Table, Symbol Table.

3. Display output of pass2 as the Op-code Table, Symbol Table , Copy file.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

Test Cases: System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


1 Input symbol which is not defined

2 Input Opcode which is not entered in MOT

Conclusion:

PASS-1

Code:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Input:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Output:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


PASS-2

Code:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Input:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24

Output:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

Post Lab Questions: System Programming and Compiler Construction


VI Semester ( Computer) Academic Year: 23-24
1. Define the basic functions of assembler.

2. What is the need of SYMTAB (symbol table) in assembler?

3. What is the need of MOT in assembler

4. What is meant by one pass assembler?


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 23-24


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24


Jason 9537

Experiment No 8

Aim: Write a program to implement two pass Macro Processor.

Learning Objective: To understand how the pre-processor replaces all the macros in the program by
its real definition prior to the compilation process of the program.

Algorithm:

Pass1:

1. Set the 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)

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)

7. Increment MNTC by 1.

8. Prepare ALA

9. Enter macro name into MDT at index MDTC

10.Increment MDTC by 1.

11. Read source statement from source program

12. Create and substitute index notation for arguments in the source statement if any.

13. Enter this line into MDT

14. Increment MDTC by 1.

15. Check if currently read source statement is pseudo-opcode MEND. If yes then goto step 3 else
goto step 11.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

16. Write source program statement as it is in the file

17.Check if pseudo-opcode END is encountered. If yes goto step 18 else goto step 19

18. Goto Pass2

19. Go to step 3

20. End of PASS1.

Pass2:

1. Read next statement from source program

2. Search in MNT for match with operation code

3. If macro name found then goto step 4 else goto step 11.

4. Retrieve MDT index from MNT and store it in MDTP.

5. Set up argument list array

6. Increment MDTP by one.

7. Retrieve line pointer by MDTP from MDT

8. Substitute index notation by actual parameter from ALA if any.

9. Check if currently retrieved line is pseuodo-opcode MEND, if yes goto step 1 else goto step 10

10. Write statement formed in step 8 to expanded source file and goto step 6

11. Write source statement directly into expanded source file

12. Check if pseudo-opcode END encountered, if yes goto step 13 else goto step 1

13. End of PASS II

Implementation Details

1. Read input file with Macros


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

2. Display output of Pass1 as the output file, MDT, MNT, and ALA tables.

3.Display output of pass2 as the expanded source file, MDT, MNT and ALA tables.

Test Cases :

1. Call macro whose definition is not present


2. Define macro without MEND

Conclusion:

PASS-1

Code:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

Input:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

Output:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

PASS-2

Code:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

Input:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

Output:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24

Post Lab Questions:

1. What is meant by macro processor?

2. What are the features of macro processor?


FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING

System Programming and Compiler Construction

VI Semester ( Computer) Academic Year: 2023- 24


Jason Diouga Page No.
453ate.

Shep moke ugmented state

s.Aa, 1
s A

S-5bdatdra,
15dc,tsdc: S

Step 3
Sbats
Actipn
S3

2 actept
6

84
Sle

The pneblem bas


LRCI) talale
The able table is aleo LAlRC)tab le
Page No.
Date.

A
A) J

A S8 a

Jtate
Actio A
2
S3 Sy
2 SS

There is ship reduce

fala Co)-plula)
Page No.
Date.

çoloCa] fit Ca) a


gallotu(e) p t Colediops'be

pllou(p= polowlrl lat


psstla) pbrstlo)ibs4

fallalc)=geslo)ºla,)ofsele)
Follsu Le = follae Cg)=SgeA
Fotto D)=sb3
fellau lelpiagtle)= E}ugalkalod

extle)=S6,13
s3aAb6Aagle
A folsula) la,bi
Page No.
Date.

b
SaAbB
AS

ShepIi Aogment

S 5.(L)Csft)

tep3: Tahle
Stade petien
)

Arcept
S2
Se

8
Yy
Paga No.
Data.

SCDe

p5d
step Li pugment
Stepi
To
stDr a(0-ss
(s>acbeADE

lexical aalyis
b)Semantit analyeis
c Syrhase analys
dexieal analysi
lexical analytis
Semant t anas
Semantic nalyei'e
H)Syre ahaysi's
uklized
3\Likals f in Aith he Eanslat
Cyn Pass i
2
encsunted INerds|deing Passti datu
proqam
f lecaion encounted H
em
n asomdy Synbal inknedateatmesate
passa tianslated
codenaddeses bexal helps syrbcl 3na
taci stucues
in si Ha
1able' seslwe
blq tahle'- taaes
to the countey:9
being able:
pass
instsotlim coiesposng o
ode allesing
for acdesses fn pass cason
eslae Tae assembly assembled synbols B
sSimlax 2 maine
epreAkttation synbol stakes souxce This asemblet to
to smlns smbol tatbe-ck process
ssian a into akulaton keeps
to adaess
instctiang Jostctions chcct and This all and code 4537
tade Ss
tade talteHcxats coleolate along the passl
botn
eses pass)
maclaecode and of code taale inciemezled QSAemblsq
tneix ot of is soith all FR.
hsgenemted the t ade encoxtie CONCEICAO
iate adtesses Cuent neessaty taeix sqmbcls
K
kode. theix and
toadtfesses
itcsal -2
ntengediate nchix cddæsi RODP
Code. pass
itorals in This apass instuchions cocesponding(Lhds, IGUES
fo r locaton ia
posslis
table shcing taule code 2 fon uilize 2 COLLEGE
enlont th e the Hae yoriable,
s 84mbolsSoze OF
cnde stants or
Ironsatoa alse QSsenbky calcolatg ate In ENGINEERING
qenemtg addueses. difftent
ICd ued alg ebe)
e and code
fatemenbs localion
af9f
6f
of
locatton
hex sp s in ite yaloe
daop asldvess)psevdo
advesso)
sorce of a
<equted
1space
Detcmine locatbn
counte 'of curtent
length
the a find presehce
frocess to elativenot to the coitth
litralsupdate
(elativeJR
set 3pecifies
Sedsched
examined. fox aleng
ceonterc)is sromned
table pioqsam
Ctee
in
label Loith
LC
valee
Foond statenenksp
ode enty table
LITSTO is
|Symbol
STSTO
is opcode(M)
MoTis sumbol
in field feld.
otiontion
6p-codefield
'mathed
cprond
is
Inilialae Gctof
length lecnstuc opcede
inst¯tions it Coont.
Read labe |Se.asclh
Pesevd
o
MCof
Serkh HOTeETJ
ofptable
SnitallySoUsce, machine foond'
The
Table 1* The. The
used
psog that
Befor
LNGSLRG
counter
and thal
o symbo! teomioad
OL
fotnatinstucicn.
saxed fidd.
to
KODPGULSOLLIGL
the ssined
td Xandlocatiornpasslis
us define
tpe
icis
to in both ce
CONCKAO
haueexpuesion encaunkied
R
stoikdepented afkct loratians
we
fostaucfians cps
op
sequenteisot is pas2
pseo code op
eualuaion
to
pseudo ENDpseudo
of Eqo conttols
sonne
IengthSomeof sequiNeS
OE
tzansks
pass2 case and Hhe
Then So Ihus cohen
Ds
Teqisteo
unavibte
Indisate
base
Exit Country
octton
Updak
assenbler. avoilable
Tncate "teqistes
base
End (Lc)
paas Type? Constant space
thoo Convett
and
Datemine
ef
ctpass2 fand
by
Snstwetier peand
ket
sqmbel
¬xpressions
Evalvate taqet
p'sct
parts'
of
an'iny fSearhing
o Snstucon
Inihaixe Search
Mc lengtth
kype STEETleAssemb
Psevduep GET
MOT the
her
Design Read
PoSTGETcf
table
Code
volues
coffegn asin insboctibaf
evalualed
o anefeckieotfes base ENe
ENGINEERING
same EQu
staztiorn. is
zequine for necessoX 2 alzady anly
campk bese
maching
OF is evaleokd The poss
COLLEGE
field done in toble copplett
fomats qacakebase-xeqisBex nextinae
in
xeqistea is base to in
definiticn
pat to pocesing
RODRIGUES
ielkdsaie 2
similargzocesrg
goint thm pas 2to
nprands
CONCEICAQ toneach to is souce
af
nskuc euoluotel
sitableit to special ealuakd in pas
FR.
index
fetmat, assembled
lsadez. Sgnbol
incemented stoage
eSsenially
puocesed
puoceKe
eent eques Yn
and is a by a8 eha
exknsaels
aèe
RR opcanfinddetcimine is pracesing tot
egistex nstrcion cp Piel Pes the
(ountens
of fos
iets
t
atesscdhecked thus
epeand
pseusde indkateA
the latex least displacennenk
nsed
Ad is Aftty. Iocationinpass1
sBoxoge is displaement
Por tade op
opC. fonot BI equles
1pasá
Fcspoat
Firaly psecalo
RARRThe Then FortYat As in ayd bose
he
3
femp If
bs Inlex MoT
thre
A, Ci, w 4emp three Two PG2 Symbol
table
R,22 Son2 oStatPaSource Syob
2824 22 SR ST Ingtrucion
AAR

32 2829 22 22 Le

m6jao,32(o)4)
2
Sh ST
2,2 A)-Co4)|
2 outeut R A R
I,-0,yps23 Inde
o2

2 D6
2 03 pass 2
2 oltput
- 124 (nStructon
Sfavt PoT
2 2.
(os4) (0,
4)
heval
|iteal
table
le
length
4

R/A
CATL

Tadle

statmMnt NO netuchia
FIVe

aseslur

lasinaahen
MOT

02
0

M
ST
SK

|POT
dndex

K
29
2

THREG 244

TeMP 29 R
titexl Zatle

PAM 2
utpue etput

Pa6TRTO
LI,-C04)
02) 2 ylo)
91,-lo,4) 03) 24 to,4)
I, THREE
04 32 (0/4)
sHI, F2
20 STI,(o, 4) 05, ) 23(04)
STI, TeMP

SR 2, 2 22 5R 212
24
2 22 2
2 24

AND
Spce ECONCEK SO RODPILES OLLEGE OF ENGINLERING

a631
Fune tisA
ae reçraceSed Taey ave lemçiled

Uing untiaa Keeps the


Cede length unapfected
de eEseh at lahey giages

Eoatex. slouex

dces nct check any J6) Function checks Campile

fuactins are er

hese are
a texhual subkhtitn:
nacre çexfazms eytual suh replacing
fracro fo aleuate

Lmul AX, nu m
mov AX, s(Imul ax)
Darametri3ed macros: barod
Patemehers allowing for dynamic behaviour
macros Can tak
Valure
input valves
¬9- depine may (asb) a)> Cb) ? Ca):(b))
main()

nt ma va lue = max

c Con ditiona (o
macro rocessoY Can mpiLaton:
enable oY disable parts op Co de bas on
Conditional ireciives.
E:- tt define DEBuG -ENABLED
Fee EBUa-EnaBlED
t* define
t# elSe DEBUhLoncmsa)print f DE8 U6: 1S \n'msg)

'nt main )l DEBuhLoh C Debug9 mess age


retun o,
dllsde generaton:
macros Can generate ode snippers entie bleck oF (ode base on
pr edened rules
eyi * depine Dedare. Vartable Ctype, name) tyre name
int main () 2
DEclare -VariaLle Ci nt,num);

eiteration and looping :


Macro processots can be used For iteratve toakes ad looping construcs
A:- define Repeat lGobunt ,acH'on) (
doe
aten,I
3whi le 6)

Rereat (3, pmintf Chello wevd" ));


reurn o
3
FR.CONCICAORODPIGUES COLLEGEOF ENGINJERING

laatian and ade xeuie..nacras facilitateAhicby entasuloting


Comman Aunctisnality into reusable units
- *de_ine Aray length (arr) Sie og_Cazd) op a)
bi~e cg
int main (0S

int length e Aryau length Cyombera),

fomusd epenre peoalem the ascemhler cpeisiec that the


axywhee in the preqram
Se there Can be chanes of macro all befase ite desiae
which qiue ne to forward reeYence pe phcblenn ad

Recogaigem definiion Saue macra depinahia a


Recognige maro call gefarm macre expasisg

oy ile -44 is a pile it cantains the out giuenfranm ag


2)MNI St is ysed for xeiognizing macYa name

SBLA- 44 &osdseate e indeI nctation ay atual valve

fes Fu ther pre cesging


flow chart
(Pass 2
Read rem

fvom he macYO
hame

ame
Poun yes
wnite

pssemSler

|schup mLA

Teme pybm

JSubotitute
AMND Nuite
ad) Varinble
Canghant
xee
Contant yoe Cangth alee Syabal 2
table erdion Data
ree
alue Symos
ee table
ÎNCR
2
Toshutian 4ing
tble t'sne întruç machine ahle, ans
)tinstuch' Pseudo
Vaniable tem
R
Conatant Ahree
R
Constant
ic 2 S R
Contant 2
2
lheral name regram
table Litesale ype (Rangh alye Sym
Symbalable
2
Qu0macros
tohle
NGNEERING COLLEGE ODRIGUES
OF CONCHCA
R

You might also like