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

Vedant 9564 Batch B

Uploaded by

Know Unknown
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)
18 views125 pages

Vedant 9564 Batch B

Uploaded by

Know Unknown
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/ 125

‭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:‬‭Vedant Pathare‬ ‭Roll No.:‬‭9564‬

‭ r.‬
S ‭Experiment Title‬ ‭ ubmission‬ ‭Remarks‬
S
‭No.‬ ‭Date‬

‭1.‬ ‭ o write a program for implementing‬


T ‭ 2/02/2024‬
0
‭Symbol Table.‬
‭2.‬ ‭Write‬ ‭a‬ ‭program‬ ‭to‬ ‭implement‬ ‭Lexical‬ ‭09/02/2024‬
‭analyzer.‬
‭3.‬ ‭Design recursive descent parser.‬ ‭16/02/2024‬

‭4.‬ ‭To generate an Intermediate code‬ ‭23/02/2024‬

‭5.‬ ‭ tudy of Lexical analyzer tool -Flex/Lex‬


S ‭01/03/2024‬
‭and Yacc‬
‭6.‬ ‭Generate‬ ‭a‬ ‭target‬ ‭code‬ ‭for‬ ‭the‬ ‭optimized‬ ‭15/03/2024‬
‭code.‬
‭7.‬ ‭Write a program to implement Two Pass‬ ‭22/03/2024‬
‭Assembler‬
‭8.‬ ‭Write a program to implement two pass‬ ‭12/04/2024‬
‭Macro Processor‬
‭9.‬ ‭Assignment 1‬ ‭26/02/2024‬

‭10.‬ ‭Assignment 2‬ ‭12/03/2024‬

‭11.‬ ‭Assignment 3‬ ‭22/03/2024‬


‭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‬

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

‭1‬
‭Practical No:‬

‭Title:‬ ‭Implementing Symbol Table‬

‭02/02/2024‬
‭Date of Performance:‬

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

‭Roll No:‬ ‭9564‬

‭Vedant Pathare‬
‭Name of the Student:‬

‭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: 23 - 24‬


‭Experiment No 1‬
‭ IM:‬
A
‭To write a program for implementing Symbol Table‬‭.‬

‭ALGORITHM‬

‭ tep1:‬‭Start the program for performing insert, display,‬‭delete, search and modify option‬
S
‭in symbol table‬
‭Step2: ‬‭Define the structure of the Symbol Table‬
‭Step3:‬‭Enter the choice for performing the operations‬‭in the symbol Table‬
‭Step4:‬‭If the entered choice is 1, search the symbol‬‭table for the symbol to be inserted. If‬
‭the symbol is‬
‭already present, it displays “Duplicate Symbol”. Else, insert the symbol and the‬
‭corresponding address in‬
‭the symbol table.‬
‭Step5:‬‭If the entered choice is 2, the symbols present‬‭in the symbol table are displayed.‬
‭Step6: ‬‭If the entered choice is 3, the symbol to be‬‭deleted is searched in the symbol table.‬
‭Step7: ‬‭If it is not found in the symbol table it displays‬‭“Label Not found”. Else, the‬
‭symbol is deleted.‬
‭Step8:‬‭If the entered choice is 5, the symbol to be‬‭modified is searched in the symbol‬
‭table.‬

‭Sample Input and Output:‬

‭Source Code:‬

i‭mport java.util.ArrayList;‬
‭import java.util.Scanner;‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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

‭public class SPCC_Exp_1 {‬


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

S‭ ymbolTable symbolTable = new SymbolTable();‬


‭Scanner sc = new Scanner(System.in);‬

‭while(true) {‬

S‭ ystem.out.println("1)Insert Token 2)Delete Token 3)Search Token 4)Modify 5)Display Table‬


‭6)Exit");‬
‭System.out.print("Enter your choice: ");‬
‭char choice = sc.next().charAt(0);‬
‭sc.nextLine();‬
‭System.out.println();‬

‭switch(choice) {‬

‭case '1':‬
‭System.out.print("Enter symbol to add: ");‬
‭char symbol = sc.next().charAt(0);‬
‭sc.nextLine();‬
‭Token token = new Token(symbol);‬
‭symbolTable.addToken(token);‬
‭break;‬

‭case '2':‬
‭System.out.print("Enter symbol to delete: ");‬
‭char symbol1 = sc.next().charAt(0);‬
‭sc.nextLine();‬
‭Token token1 = new Token(symbol1);‬
‭symbolTable.deleteToken(token1);‬
‭break;‬

‭case '3':‬
‭System.out.print("Enter symbol to search: ");‬
‭char symbol2 = sc.next().charAt(0);‬
‭sc.nextLine();‬
‭Token token2 = new Token(symbol2);‬
‭symbolTable.searchToken(token2);‬
‭break;‬

‭case '4':‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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


‭System.out.print("Enter symbol to modify: ");‬
‭char symbol3 = sc.next().charAt(0);‬
‭sc.nextLine();‬
‭Token token3 = new Token(symbol3);‬
‭symbolTable.modifyToken(token3);‬
‭break;‬

‭case '5':‬
‭symbolTable.displayTable();‬
‭break;‬

‭case '6':‬
‭System.out.println("Program terminated!");‬
‭sc.close();‬
‭System.exit(0);‬
‭break;‬

‭default:‬
‭System.out.println("Invalid choice!");‬
}‭ ‬
‭System.out.println();‬
‭}‬
‭}‬
‭}‬

‭class Token {‬

‭ rivate char symbol;‬


p
‭private String type;‬

‭public Token(char symbol) {‬


‭this.symbol = symbol;‬
‭setType();‬
‭}‬

‭public void setType() {‬


‭if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' || symbol == '^') {‬
‭type = "Operator";‬
‭} else if (symbol >= 65 && symbol < 91 || symbol >= 97 && symbol < 123) {‬
‭type = "Identifier";‬
‭} else {‬
‭type = "Invalid";‬
‭}‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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


‭}‬

‭public void setType(String type) {‬


‭this.type = type;‬
‭}‬

‭public char getSymbol() {‬


‭return symbol;‬
‭}‬

‭public String getType() {‬


‭return type;‬

‭}‬

‭}‬

‭class SymbolTable {‬

‭private ArrayList<Token> tokens;‬

‭public SymbolTable() {‬
‭tokens = new ArrayList<>();‬
‭}‬

‭public void addToken(Token token) {‬


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

‭public void deleteToken(Token t1) {‬


‭for (Token token : tokens) {‬
‭if (token.getSymbol() == t1.getSymbol()) {‬
‭tokens.remove(token);‬
‭System.out.println("Token deleted!");‬
‭return;‬
‭}‬
‭}‬
‭System.out.println("Token to be deleted not found!");‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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


‭}‬

‭public void searchToken(Token t1) {‬


‭if (tokenInTable(t1)) {‬
‭System.out.println("Token found!");‬
‭} else {‬
‭System.out.println("Token not found!");‬
‭}‬
‭}‬

‭public void modifyToken(Token t1) {‬


‭if (tokenInTable(t1)) {‬
‭for (Token token : tokens) {‬
‭if (token.getSymbol() == t1.getSymbol()) {‬
‭Scanner sc = new Scanner(System.in);‬
‭System.out.println("Token found!");‬
‭System.out.print("Enter modified type: ");‬
‭String type = sc.nextLine();‬
‭token.setType(type);‬
‭break;‬
‭}‬
‭}‬
‭} else {‬
‭System.out.println("Token not found!");‬
‭}‬
‭}‬

‭public void displayTable() {‬


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

‭private boolean tokenInTable(Token t1) {‬


‭for (Token token : tokens) {‬
‭if (token.getSymbol() == t1.getSymbol()) {‬
‭return true;‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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


‭}‬
‭}‬
‭return false;‬
‭}‬
‭}‬

‭Screenshots:‬

‭1. Insert‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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

‭2. Delete‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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

‭3. Search‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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

‭4. Modify‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

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

‭5. Display‬

‭Postlab Questions:‬

‭1.‬ E‭ xplain different phases of compiler. Illustrate all the output after each phase for the‬
‭following statement‬
‭a= b +c -d *5‬
‭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)‬


‭ ubject Name :‬‭System Programming and Compiler Construction‬
S
‭Subject Code :‬‭(CPC601)‬

‭Practical No:‬ ‭2‬

‭Title:‬ ‭Implementing Lexical Analyzer‬

‭Date of Performance:‬ ‭02/02/2024‬

‭09/02/2024‬
‭Date of Submission:‬
‭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: 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:‬
‭T‭H
‬ E‬‭R‭O
‬ LE OF‬‭L‭E
‬ XICAL‬‭A‭N
‬ ALYZER‬

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

c‭ haracters 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‬

‭ ince‬ ‭the‬ ‭lexical‬ ‭analyzer‬ ‭is‬ ‭the‬ ‭part‬‭of‬‭the‬‭compiler‬‭that‬‭reads‬‭the‬‭source‬‭text,‬‭it‬‭may‬‭also‬‭perform‬


S
‭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:‬

‭ .‬ E
1 ‭ xplain the role of automata theory in compiler design.‬
‭2.‬ ‭What are the errors that are handled by the Lexical analysis phase?‬
‭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‬

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

‭4‬
‭Practical No:‬
‭To generate an Intermediate code.‬
‭Title:‬
‭16/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: 23-24‬

‭Experiment No 4‬

‭Aim‬‭:‬‭To generate an Intermediate code.‬


‭Description:‬

‭●‬ I‭ f‬ ‭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.‬
‭●‬ I‭ ntermediate‬ ‭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.‬
‭●‬ I‭ t‬ ‭becomes‬ ‭easier‬ ‭to‬ ‭apply‬ ‭the‬ ‭source‬ ‭code‬ ‭modifications‬ ‭to‬ ‭improve‬ ‭code‬
‭performance by applying code optimization techniques on the intermediate code.‬

‭●‬ ‭Three-Address Code‬


I‭ ntermediate‬ ‭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‬
‭ ach‬ ‭instruction‬ ‭in‬ ‭quadruples‬‭presentation‬‭is‬‭divided‬‭into‬‭four‬‭fields:‬‭operator,‬‭arg1,‬‭arg2,‬
E
‭and result. The above example is represented below in quadruples format:‬

‭Op‬ ‭arg‬‭1‬ ‭arg‬‭2‬ ‭result‬

‭*‬ ‭c‬ ‭d‬ ‭r1‬

‭+‬ ‭b‬ ‭r1‬ ‭r2‬

‭+‬ ‭r2‬ ‭r1‬ ‭r3‬

‭=‬ ‭r3‬ ‭a‬

‭Triples‬
‭ ach‬ ‭instruction‬ ‭in‬ ‭triples‬ ‭presentation‬ ‭has‬ ‭three‬ ‭fields‬ ‭:‬ ‭op,‬ ‭arg1,‬ ‭and‬ ‭arg2.The‬ ‭results‬‭of‬
E
‭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‬ ‭arg‬‭1‬ ‭arg‬‭2‬

‭*‬ ‭c‬ ‭d‬

‭+‬ ‭b‬ ‭(0)‬

‭+‬ ‭(1)‬ ‭(0)‬

‭=‬ ‭(2)‬

‭ riples‬ ‭face‬ ‭the‬ ‭problem‬ ‭of‬ ‭code‬ ‭immovability‬ ‭while‬ ‭optimization,‬ ‭as‬ ‭the‬ ‭results‬ ‭are‬
T
‭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‬
‭ his‬‭representation‬‭is‬‭an‬‭enhancement‬‭over‬‭triples‬‭representation.‬‭It‬‭uses‬‭pointers‬‭instead‬‭of‬
T
‭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();‬

‭ tring[] terms = rhs.split(" ");‬


S
‭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;‬
‭}‬
‭}‬

c‭ ode.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();‬

‭ tring trueLabel = "L" + tempCount++;‬


S
‭// 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(": ");‬

‭ ystem.out.println("Three-address code:");‬
S
‭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();‬

‭ tringBuilder code = new StringBuilder();‬


S
‭code.append(target).append(" = ").append(source).append(";");‬

‭ ystem.out.println("Three-address code:");‬
S
‭System.out.println(code.toString());‬
‭}‬

‭private static void generateIndexedAssignmentCode() {‬


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

‭ tring[] parts = expression.split(" = ");‬


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

i‭nt 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+");‬

‭ tring operand1 = operationParts[0];‬


S
‭String operator = operationParts[1];‬
‭String operand2 = operationParts[2];‬

‭ tring intermediateVar = "T" + tempCount++;‬


S
‭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‬

‭ ystem.out.println("Three-address code:");‬
S
‭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 V‬‭1‬‭: S‬‭1‬
‭case V‬‭2‬‭: S‬‭2‬
‭….‬
‭default: S‬‭n‬
‭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: 2023-24‬

‭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)‬

‭5‬
‭Practical No:‬
‭Study of Lexical analyzer tool -Flex/Lex‬
‭Title:‬
‭01/03/2024‬
‭Date of Performance:‬

‭Date of Submission:‬ ‭08/03/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: 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:‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭Aim‬‭:Study of Parser generator tool –‬‭Yacc‬

‭Theory:‬

‭ arser‬ ‭for‬ ‭a‬ ‭grammar‬ ‭is‬ ‭a‬ ‭program‬ ‭which‬ ‭takes‬ ‭in‬ ‭the‬ ‭language‬ ‭string‬ ‭as‬ ‭its‬ ‭input‬ ‭and‬
P
‭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‬

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

I‭ mplementation Details‬
‭1: Create a lex file‬

‭The general format for lex file consists of three sections:‬

‭1.‬ ‭Definitions‬

‭2.‬ ‭Rules‬

‭3.‬ ‭User subroutine Section‬

‭ efinitions‬‭consists‬‭of‬‭any‬‭external‬‭‘C’‬‭definitions‬‭used‬‭in‬‭the‬‭lex‬‭actions‬‭or‬‭subroutines.‬‭The‬
D
‭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‬

I‭ n‬ ‭declarations‬ ‭apart‬ ‭from‬ ‭the‬‭legal‬‭‘C’‬‭declarations‬‭here‬‭are‬‭few‬‭Yacc‬‭specific‬‭declarations‬


‭which begins with a % sign.‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭1.‬ ‭%union It defines the Stack type for the Parser.‬


‭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*/}‬

‭ he‬ ‭general‬ ‭style‬ ‭of‬ ‭coding‬ ‭the‬ ‭rules‬ ‭is‬ ‭to‬ ‭have‬ ‭all‬ ‭Terminals‬ ‭in‬ ‭lower‬ ‭–case‬ ‭and‬ ‭all‬
T
‭non-terminals in upper –case.‬
‭ o‬ ‭facilitate‬ ‭a‬ ‭proper‬ ‭syntax‬ ‭directed‬ ‭translation‬ ‭the‬ ‭Yacc‬ ‭has‬ ‭something‬ ‭calls‬
T
‭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:‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭1.‬ P
‭ rocess 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):‬

yacc -d calc.yacc‬

‭2.‬ U ‭ se 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.‬ U
‭ se 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‬

‭Source Code:‬

‭1. Program to count the number of vowels and consonants in a given string.‬

‭%{‬
#‭ include<stdio.h>‬
‭int vowels=0;‬
‭int consonants=0;‬
‭ }‬
%
‭%%‬
‭[aeiouAEIOU] vowels++;‬
‭[a-zA-Z] consonants++;‬
‭[\n] ;‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

.‭ ;‬
‭%%‬
‭int main()‬
‭{‬
‭printf("This‬ ‭Lex‬ ‭program‬ ‭counts‬ ‭the‬ ‭number‬ ‭of‬ ‭vowels‬ ‭and‬ ‭consonants‬ ‭in‬ ‭given‬
‭text.");‬
‭printf("\nEnter the text and terminate it with CTRL-d.\n");‬
‭yyin = stdin;‬
‭yylex();‬
‭printf("Vowels = %d, consonants = %d.\n", vowels, consonants);‬
‭return 0;‬
‭}‬
‭int yywrap() {‬
‭return 1;‬
‭}‬

2‭ .‬ ‭Program‬ ‭to‬ ‭count‬ ‭the‬ ‭number‬ ‭of‬ ‭characters,‬ ‭words,‬ ‭spaces,‬ ‭end‬ ‭of‬ ‭lines‬‭in‬‭a‬‭given‬‭input‬
‭file.‬

‭%{‬
#‭ include<stdio.h>‬
‭int c=0, w=0, s=0, l=0;‬
‭ }‬
%
‭WORD [^ \t\n,\.]+‬
‭EOL [\n]‬
‭BLANK [ ]‬
‭%%‬
‭{WORD} {w++; c=c+yyleng;}‬
‭{BLANK} {s++;}‬
‭{EOL} {l++;}‬
‭, {c++;}‬
‭%%‬
‭int yywrap() {‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭return 1;‬
}‭ ‬
‭int main(int argc, char *argv[]) {‬
‭if(argc!=2) {‬
‭printf("Usage: <./a.out> <sourcefile>\n");‬
‭exit(0);‬
‭}‬
‭yyin = fopen(argv[1], "r");‬
‭yylex();‬
‭printf("No‬‭of‬‭characters‬‭=‬‭%d\nNo‬‭of‬‭words‬‭=‬‭%d\nNo‬‭of‬‭spaces‬‭=‬‭%d\nNo‬‭of‬‭lines‬‭=‬
‭%d\n", c,w,s,l);‬
‭}‬

‭3. Program to count no of +ve and -ve integers and fractions.‬

‭%{‬
#‭ include<stdio.h>‬
‭int posint=0, negint=0, posfraction=0, negfraction=0;‬
‭ }‬
%
‭%%‬
‭[-][0-9]+ {negint++;}‬
‭[+]?[0-9]+ {posint++;}‬
‭[+]?[0-9]*\.[0-9]+ {posfraction++;}‬
‭[-][0-9]*\.[0-9]+ {negfraction++;}‬
‭%%‬
‭int yywrap() {‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭return 1;‬
}‭ ‬
‭int main(int argc, char *argv[]) {‬
‭if(argc!=2) {‬
‭printf("Usage: <./a.out> <sourcefile>\n");‬
‭exit(0);‬
‭}‬
‭yyin = fopen(argv[1], "r");‬
‭yylex();‬
‭printf("No‬ ‭of‬ ‭+ve‬ ‭integers=%d\nNo‬ ‭of‬ ‭-ve‬ ‭integers=%d\nNo‬ ‭of‬ ‭+ve‬
‭fractions=%d\nNo of -ve fractions=%d\n", posint, negint, posfraction, negfraction);‬
‭}‬

4‭ .‬‭Program‬‭to‬‭count‬‭the‬‭number‬‭of‬‭comment‬‭lines‬‭in‬‭a‬‭given‬‭C‬‭program,‬‭eliminate‬‭them‬‭and‬
‭copy that program into separate file.‬

‭%{‬
#‭ include<stdio.h>‬
‭int com=0;‬
‭ }‬
%
‭%s COMMENT‬
‭%%‬
‭"/*"[.]*"*/" {com++;}‬
‭"/*" {BEGIN COMMENT ;}‬
‭<COMMENT>"*/" {BEGIN 0; com++ ;}‬
‭<COMMENT>\n {com++;}‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭ COMMENT>. {;}‬
<
‭.|\n {fprintf(yyout, "%s", yytext);}‬
‭%%‬
‭int yywrap() {‬
‭return 1;‬
‭}‬
‭int main(int argc, char *argv[]) {‬
‭if(argc!=3) {‬
‭printf("Usage: a.out source destination\n");‬
‭exit(0);‬
‭}‬
‭yyin=fopen(argv[1], "r");‬
‭yyout=fopen(argv[2], "w");‬
‭yylex();‬
‭printf("No of comment lines=%d\n", com);‬
‭}‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭5. Program to count number of operators and operands in a given expression.‬

‭%{‬
#‭ include<stdio.h>‬
‭int opr=0, opd=0;‬
‭ }‬
%
‭%%‬
‭[\+\-\*\/\=] {printf("OPERATORS ARE %s\n", yytext); opr++;}‬
‭[a-zA-Z]+ {printf("OPERANDS ARE %s\n", yytext); opd++;}‬
‭[0-9]+ {printf("OPERANDS ARE %s\n", yytext); opd++;}‬
‭%%‬
‭int yywrap() {‬
‭return 1;‬
‭}‬
‭int main() {‬
‭printf("Enter the Expression:\n");‬
‭yylex();‬
‭printf("\nNUMBER OF OPERATORS ARE %d\n", opr);‬
‭printf("NUMBER OF OPERANDS ARE %d\n", opd);‬
‭}‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023-24‬

‭Conclusion:‬

‭ e‬ ‭have‬ ‭successfully‬‭experimented‬‭with‬‭the‬‭Lex‬‭&‬‭Yacc‬‭tool,‬‭enhancing‬‭our‬‭understanding‬
W
‭of compiler construction and lexical analysis.‬

‭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‬


‭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‬

‭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)‬

‭Practical No:‬ ‭6‬

‭To generate a target code for the optimized code.‬


‭Title:‬
‭15/03/2024‬
‭Date of Performance:‬

‭Date of Submission:‬ ‭22/03/2024‬

‭Roll No:‬ ‭9564‬

‭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: 2023 - 24‬

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

‭ he‬ ‭code‬ ‭generation‬ ‭algorithm‬ ‭takes‬ ‭as‬ ‭input‬ ‭a‬ ‭sequence‬ ‭of‬ ‭three‬ ‭address‬ ‭statements‬
T
‭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:‬


‭ he‬‭function‬‭getreg‬‭returns‬‭the‬‭location‬‭L‬‭to‬‭hold‬‭the‬‭values‬‭of‬‭x‬‭for‬‭the‬‭assignment‬‭x=‬‭y‬‭op‬
T
‭z.‬
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‬

‭ ight‬ ‭be‬ ‭one‬ ‭whose‬ ‭data‬ ‭is‬ ‭referenced‬ ‭furthest‬ ‭in‬ ‭the‬‭future,‬‭or‬‭one‬‭whose‬‭value‬‭is‬‭also‬‭in‬
m
‭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.‬

‭Source Code:‬

#‭ Input must be in 3 address code form‬


‭import re # Regex for splitting‬

r‭ egisters = {} # Dictionary to keep track of register assignment‬


‭output_code = [] # Output lines‬

‭def allocateRegister(operand):‬
‭if operand in registers.values(): # Operand already present in one of the registers, use it‬
‭for key, value in registers.items():‬
‭if operand == value:‬
‭return key‬
‭else:‬
‭register_name = "R" + str(len(registers)) # Allocate a new register for the operand‬
‭registers[register_name] = operand # Assign operand to register‬
‭output_code.append("MOV‬‭"+operand+","+register_name)‬ ‭#‬‭Add‬‭MOV‬‭statement‬‭to‬
‭output‬
‭return register_name‬

‭def renameReg(location,operand):‬
‭if operand in registers.values():‬
‭for key, value in registers.items():‬
‭if operand==value:‬
‭registers[key]=location‬
‭#print(registers)‬

‭input_code = list(line.strip() for line in open("code_gen_input.txt"))‬


‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023 - 24‬

‭for index,line in enumerate(input_code):‬


‭line = re.split("([\=\+\-\*\/])", line) # Split the TAG into operands and operator‬
‭LHS, eq, op1, operator, op2 = line‬
‭oper=op1‬
‭if op2 in registers.values():‬
‭op2=allocateRegister(op2)‬
‭op1 = allocateRegister(op1)‬
‭if operator.strip() == "+":‬
‭output_line = "ADD "+str(op2)+","+str(op1)‬
‭#print(oper,LHS)‬
‭renameReg(LHS,oper) # Register now hold output i.e. LHS‬
‭#print(registers)‬
‭elif operator.strip() == "-":‬
‭output_line = "SUB " + str(op2) + "," + str(op1)‬
‭renameReg(LHS,oper)‬
‭#registers[op2] = LHS‬
‭if(index==len(input_code)-1):‬
‭output_line=output_line+("\nMOV "+op1+","+LHS)‬

‭output_code.append(output_line)‬

‭for line in output_code:‬


‭print(line)‬

‭Output:‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023 - 24‬

‭Conclusion:‬
‭ hus,‬‭we‬‭have‬‭successfully‬‭generated‬‭target‬‭code‬‭(Assembly‬‭language‬‭code)‬‭from‬‭the‬‭given‬
T
‭intermediate‬ ‭code‬ ‭and‬ ‭have‬ ‭satisfactorily‬ ‭demonstrated‬ ‭the‬ ‭code‬ ‭generation‬ ‭phase‬ ‭of‬ ‭a‬
‭compiler.‬

‭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‬


‭Department of Computer Engineering‬

‭Academic Term : Jan-May 23-24‬

‭Class‬ ‭: T.E. (Computer)‬


‭ ubject Name :‬‭System Programming and Compiler Construction‬
S
‭Subject Code :‬‭(CPC601)‬

‭Practical No:‬ ‭7‬

‭Write a program to implement Two Pass Assembler.‬


‭Title:‬
‭22/03/2024‬
‭Date of Performance:‬

‭Date of Submission:‬ ‭29/03/2024‬

‭Roll No:‬ ‭9564‬

‭Name of the Student:‬ ‭Vedant Pathare‬

‭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 7‬

‭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‬
‭6. Search opcode in Machine Opcode Table.‬

‭7. Assign its length to ‘L’.‬

‭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‬


‭7. Get opcode byte and format code‬

‭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.‬
‭Test Cases:‬

‭1‬‭Input symbol which is not defined‬

‭2 Input Opcode which is not entered in MOT‬

‭Source Code:‬

‭1. Pass1:‬

‭#include <stdio.h>‬
‭#include <string.h>‬
‭#include <stdlib.h>‬

‭void main()‬
‭{‬
‭char opcode[10], mnemonic[10], operand[10], label[10], code[10];‬
‭int locctr = 0, start, length, flag = 0;‬
‭FILE *fp1, *fp2, *fp3, *fp4;‬
‭fp1 = fopen("INPUT.txt", "r");‬
‭fp2 = fopen("st.txt", "w");‬
‭fp3 = fopen("out.txt", "w");‬
‭fp4 = fopen("MOT.txt", "r");‬

‭fscanf(fp1, "%s %s %s", label, opcode, operand);‬


‭if (strcmp(opcode, "START") == 0)‬
‭{‬
‭start = atoi(operand);‬
‭fprintf(fp3, "%s\t %s\t %s\n", label, opcode, operand);‬
‭fscanf(fp1, "%s %s %s", label, opcode, operand);‬
‭}‬
‭else‬
‭locctr = 0;‬
‭while (strcmp(opcode, "END") != 0)‬
‭{‬
‭// printf("inside the loop");‬
‭fprintf(fp3, "%d\t", locctr);‬
‭if (strcmp(label, "**") != 0)‬
‭fprintf(fp2, "%s\t%d\n", label, locctr);‬
‭fscanf(fp4, "%s", mnemonic);‬
‭// printf("%s\n",mnemonic);‬
‭// fscanf(fp1,"%s%s%s",label,opcode,operand);‬
‭if (strcmp(opcode, "DC") == 0)‬
‭{‬
‭if (operand[0] == 'F')‬
‭{‬
‭locctr += 4;‬
‭}‬
‭else‬
‭{‬
‭locctr += 2;‬
‭}‬
‭}‬
‭else if (strcmp(opcode, "DS") == 0)‬
‭{‬
‭if (operand[1] == 'F')‬
‭{‬
‭locctr += 4;‬
‭}‬
‭else‬
‭{‬
‭locctr += 2;‬
‭}‬
‭}‬
‭else if (strcmp(opcode, "USING") == 0)‬
‭{‬
‭locctr += 0;‬
‭// printf("\nusing\n");‬
‭}‬
‭// else if(strcmp(opcode,"BYTE")==0)‬
‭//++locctr;‬
‭while (strcmp(mnemonic, "END") != 0)‬
‭{‬
‭// printf("inside the loop");‬
‭printf("%s\t%s\n", opcode, mnemonic);‬
‭// printf("%s\n",mnemonic);‬
‭if (strcmp(opcode, mnemonic) == 0)‬
‭{‬
‭printf("\ninside the loop\n");‬
‭locctr += 4;‬
‭flag = 1;‬
‭// printf("%d\n",locctr);‬
‭// printf("%s\n",opcode);‬
‭break;‬
‭}‬
‭fscanf(fp4, "%s", mnemonic);‬
‭}‬
‭printf("%d\n", flag);‬
‭flag = 0;‬
‭fprintf(fp3, "%s\t%s\t%s\n", label, opcode, operand);‬
‭fscanf(fp1, "%s %s %s", label, opcode, operand);‬
‭rewind(fp4);‬
‭}‬
‭fprintf(fp3, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);‬
‭length=locctr-start;‬
‭printf("\nThe length of the program is %d", length);‬
‭fclose(fp1);‬
‭fclose(fp2);‬
‭fclose(fp3);‬
‭fclose(fp4);‬
‭}‬
‭Input:‬

‭MOT Source Program‬

‭Output:‬

‭Symbol Table Output of Pass 1‬

‭2. Pass2:‬
‭#include <stdio.h>‬
‭#include <string.h>‬
‭#include <stdlib.h>‬
‭void main()‬
‭{‬
‭char opcode[10], mnemonic[10], operand[10], operand1[10], label[10], code[10], address[10],‬
‭sizeAddress[10], Motlabel[10];‬
‭int locctr = 0, start, length, flag = 0;‬
‭FILE *fp1, *fp2, *fp3, *fp4, *fp5, *fp6, *fp411;‬
‭fp1 = fopen("INPUT.txt", "r");‬
‭fp2 = fopen("st.txt", "r");‬
‭fp3 = fopen("out.txt", "r");‬
‭fp4 = fopen("MOT.txt", "r");‬
‭fp411 = fopen("MOT.txt", "r");‬
‭fp5 = fopen("bt.txt", "w");‬
‭fp6 = fopen("outTable.txt", "w");‬

‭fscanf(fp1, "%s %s %s", label, opcode, operand1);‬

‭if (strcmp(opcode, "START") == 0)‬


‭{‬
‭fscanf(fp1, "%s %s %s", label, opcode, operand1);‬
‭}‬
‭else‬
‭locctr = 0;‬
‭while (strcmp(opcode, "END") != 0)‬
‭{‬
‭fscanf(fp4, "%s", mnemonic);‬
‭fscanf(fp411, "%s", Motlabel);‬
‭while (strcmp(Motlabel, opcode) != 0 && strcmp(Motlabel, "END") != 0)‬
‭{‬
‭fscanf(fp411, "%s", Motlabel);‬
‭}‬

‭fscanf(fp411, "%s", Motlabel);‬


‭if (strcmp(Motlabel, "END") != 0)‬
‭{‬
‭char sub = operand1[0];‬
‭char substring[4];‬
‭int j = 0;‬
‭for (int i = 2; i < 6; i++)‬
‭{‬
‭substring[j++] = operand1[i];‬
‭}‬
‭fprintf(fp6, "%s %c, ", Motlabel, operand1[0]);‬
‭for (int i = 0; i < 4; i++)‬
‭{‬
‭fprintf(fp6, "%c", substring[i]);‬
‭}‬
‭fprintf(fp6, "\n");‬
‭}‬
‭rewind(fp411);‬
‭if (strcmp(opcode, "DC") == 0)‬
‭{‬
‭if (operand1[0] == 'F')‬
‭{‬
‭locctr += 4;‬
‭}‬
‭else‬
‭{‬
‭locctr += 2;‬
‭}‬
‭fprintf(fp6, "%d %c\n", locctr, operand1[2]);‬
‭}‬
‭else if (strcmp(opcode, "DS") == 0)‬
‭{‬
‭if (operand1[1] == 'F')‬
‭{‬
‭locctr += 4;‬
‭}‬
‭else‬
‭{‬
‭locctr += 2;‬
‭}‬
‭fprintf(fp6, "%d _\n", locctr);‬
‭}‬
‭else if (strcmp(opcode, "USING") == 0)‬
‭{‬
‭locctr += 0;‬
‭fprintf(fp5, "%c%c", operand1[2], operand1[3]);‬
‭printf("\nusing\n");‬
‭}‬
‭while (strcmp(mnemonic, "END") != 0)‬
‭{‬
‭printf("%s\t%s\n", opcode, mnemonic);‬
‭if (strcmp(opcode, mnemonic) == 0)‬
‭{‬
‭printf("\ninside the loop\n");‬
‭locctr += 4;‬
‭flag = 1;‬
‭break;‬
‭}‬
‭fscanf(fp4, "%s", mnemonic);‬
‭}‬
‭printf("%d\n", flag);‬
‭flag = 0;‬
‭fscanf(fp1, "%s %s %s", label, opcode, operand1);‬
‭rewind(fp4);‬
‭}‬
‭printf("\nThe length of the program is %d", length);‬
‭}‬
‭Output:‬

‭Base Table Output of Pass 2‬


‭Conclusion:‬

‭Thus, we have successfully implemented a two-pass assembler by developing pass1 and pass2‬
‭separately and running the programs sequentially.‬

‭Post Lab Questions:‬

‭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: 2023- 24‬

‭Department of Computer Engineering‬

‭Academic Term : Jan-May 23-24‬

‭Class‬ ‭: T.E. (Computer)‬


‭ ubject Name :‬‭System Programming and Compiler Construction‬
S
‭Subject Code :‬‭(CPC601)‬

‭Practical No:‬ ‭8‬

‭Write a program to implement a two pass Macro Processor.‬


‭Title:‬

‭Date of Performance:‬ ‭12/04/2024‬

‭12/04/2024‬
‭Date of Submission:‬
‭9564‬
‭Roll No:‬

‭Name of the Student:‬ ‭Vedant Pathare‬

‭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: 2023- 24‬

‭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‬

‭Source Code:‬

‭ include <stdio.h>‬
#
‭#include <string.h>‬
‭#include <stdlib.h>‬
‭int checkIfPresent(char inputOpcode[]) { // this function will return position of macroname in mnt if‬
‭present‬
‭char macroname[10], mdtpointer[10]; char label[10], opcode[10], operand[10];‬
‭FILE *mnptr;‬
‭mnptr = fopen("MNT.txt", "r");‬
‭fscanf(mnptr, "%s%s", macroname, mdtpointer);‬
‭int i = 0;‬
‭while (strcmp(macroname, "END") != 0) {‬
‭if (strcmp(inputOpcode, macroname) == 0) {‬
‭return i;‬
‭}‬
‭fscanf(mnptr, "%s%s", macroname, mdtpointer);‬
‭i++;‬
‭}‬
‭return -1;‬
‭}‬
‭int main() {‬
‭char opcode[10], mnemonic[10], operand[10], label[10], code[10],macroname[10], opcodemdt[10],‬
‭operandmdt[10];‬
‭int mdtline[10],locctr = 0, start, length, flag= 0;‬
‭FILE *fp1, *fp2, *fp3, *fp4;‬
‭fp1 = fopen("Exp8_Input.txt", "r");‬
‭fp2 = fopen("MNT.txt", "r");‬
‭fp3 = fopen("MDT.txt", "r");‬
‭fp4 = fopen("expsrc.txt", "w"); // Ignoring macro definition lines‬
‭while (strcmp(opcode,"Start") != 0) {‬
‭fscanf(fp1, "%s%s%s", label, opcode, operand);‬
‭}‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023- 24‬

f‭ printf(fp4, "%s %s %s\n", label, opcode, operand);‬


‭while (strcmp(opcode, "END") != 0) {‬
‭fscanf(fp1, "%s%s%s", label, opcode, operand);‬
‭int x = checkIfPresent(opcode);‬
‭if (x != -1) {‬
‭fscanf(fp2, "%s%d", macroname, mdtline);‬
‭for (int i = 0; i < x; i++) {‬
‭fscanf(fp2, "%s%d", macroname, mdtline);‬
‭}‬
‭for (int i = 0; i < *mdtline; i++) {‬
‭fscanf(fp3, "%s%s", opcodemdt, operandmdt);‬
‭}‬
‭while (strcmp(opcodemdt, "MEND") != 0) {‬
‭fprintf(fp4, "%s %s %s\n", "**", opcodemdt, operandmdt);‬
‭fscanf(fp3, "%s%s", opcodemdt, operandmdt);‬
‭}‬
‭rewind(fp3);‬
‭rewind(fp2);‬
‭continue;‬
‭}‬
‭fprintf(fp4,"%s %s %s\n", label, opcode, operand);‬
‭}‬
‭printf("Expanded Source Code generated successfully!\n");‬
‭fclose(fp1);‬
‭fclose(fp2);‬
‭fclose(fp3);‬
‭fclose(fp4);‬
‭return 0;‬
‭}‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023- 24‬

‭Input:‬

‭Source Code Macro Name Table (MNT) Macro Definition Table (MDT)‬

‭Output:‬
‭FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING‬

‭System Programming and Compiler Construction‬

‭VI Semester ( Computer) Academic Year: 2023- 24‬

‭Expanded Source Code‬

‭ onclusion‬‭: Thus, we have implemented a two pass macro processor which reads the source program,‬
C
‭the MNT and the MDT and generates the expanded source code successfully.‬

‭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‬

You might also like