IMS ENGINEERING COLLEGE
LABORATORY FILE
                COMPILER DESIGN LAB
                     (BCS-652)
                   B.TECH – III YEAR
                  (Even SEM, 2024-2025)
Name               Ayush Pratap Singh
                   2201430100073
Roll No.
Section-Batch 3CSE-1 – B3
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
                   IMS ENGINEERING COLLEGE
  (Affiliated to Dr A P J Abdul Kalam Technical University, Lucknow )
            Approved by AICTE - Accredited by NAAC – ‘A’ Grade
               NH#24, Adhyatmik Nagar, Ghaziabad, UP, India
                             www.imsec.ac.in
 VISION AND MISSION OF THE INSTITUTE AND DEPARTMENT
                                  VISION OF THE INSTITUTE
To make IMSEC an Institution of Excellence for empowering students through technical education,
incorporating human values, and developing engineering acumen for innovations and leadership
skills to upgrade society.
                                 MISSION OF THE INSTITUTE
• To promote academic excellence by continuous learning in                    core   and   emerging
Engineering domains using innovative teaching and learning methodologies.
• To inculcate values and ethics among the learners.
• To promote industry interactions and cultivate young minds for entrepreneurship.
•          To create a conducive learning ecosystem and research environment on a perpetual basis to
develop students as technology leaders and entrepreneurs who can address tomorrow’s societal needs.
                                VISION OF THE DEPARTMENT
To provide globally competent professionals in Computer Science & Engineering embedded with
sound technical knowledge, aptitude for research and innovation, and nurture future leaders with
ethical values to cater to industrial & societal needs.
                               MISSION OF THE DEPARTMENT
Mission 1: To provide quality education in the theoretical and applied foundations of Computer
Science & Engineering.
Mission 2: To Conduct research in Computer Science & Engineering resulting in innovations thereby
nurturing entrepreneurial thinking.
Mission 3: To inculcate team-building skills and promote life-long learning with high societal and
ethical values.
                                                    2
PROGRAM OUTCOMES (POS) AND PROGRAM SPECIFIC
OUTCOMES (PSOs)
 S. No.                   Program Outcomes / Program Specific Outcomes
          Engineering knowledge: Apply the knowledge of mathematics, science, engineering
 PO1.     fundamentals, and an engineering specialization to the solution of complex
          engineering problems.
          Problem analysis: Identify, formulate, review research literature, and analyze
 PO2.     complex engineering problems reaching substantiated conclusions using first
          principles of mathematics, natural sciences, and engineering sciences.
          Design/development of solutions: Design solutions for complex engineering
          problems and design system components or processes that meet the specified
 PO3.
          needs     with appropriate consideration for the public health and safety, and the
          cultural, societal, and environmental considerations.
          Conduct investigations of complex problems: Use research-based knowledge
 PO4.     and research methods including design of experiments, analysis and interpretation
          of data, and synthesis of the information to provide valid conclusions.
          Modern tool usage: Create, select, and apply appropriate techniques, resources,
 PO5.     and modern engineering and IT tools including prediction and modelling to
          complex engineering activities with an understanding of the limitations.
          The engineer and society: apply reasoning informed by the contextual knowledge to
 PO6.     assess societal, health, safety, legal and cultural issues and the consequent
          responsibilities relevant to the professional engineering practice.
          Environment and sustainability:           Understand the impact of the professional
 PO7.     engineering solutions in societal and environmental contexts, and demonstrate the
          knowledge of, and need for sustainable development.
          Ethics:     Apply ethical principles and commit to professional ethics and
 PO8.
          responsibilities and norms of the engineering practice.
          Individual and team work: Function effectively as an individual, and as a member
 PO9.
          or leader in diverse teams, and in multidisciplinary settings.
          Communication: Communicate effectively on complex engineering activities
          with the engineering community and with society at large, such as, being able to
 PO10.
          comprehend and write effective reports and design documentation, make effective
          presentations, and give and receive clear instructions.
          Project management and finance: Demonstrate knowledge and understanding of
          the engineering and management principles and apply these to one’s own work, as a
 PO11.
          member and leader in a team, to manage projects and in multidisciplinary
          environments.
          Life-long learning: Recognize the need for, and have the preparation and
 PO12.    ability to engage in independent and life-long learning in the broadest context of
          technological change.
          To analyze and demonstrate, the recent engineering practices, ethical values and
 PSO1.    strategies in real-time world problems to meet the challenges for the future.
          To develop adaptive computing system using computational intelligence strategies
          and algorithmic design to address diverse data analysis and machine learning
 PSO2.
          challenges.
                                         3
    CSE DEPARTMENT PROGRAM EDUCATIONAL OBJECTIVES (PEOs)
B. Tech Computer Science & Engineering Department has the following Program Educational
Objectives:
PEO1: Possess core theoretical and practical knowledge in Computer Science and Engineering for
successful career development in industry, pursuing higher studies or entrepreneurship.
       PEO2: Ability to imbibe lifelong learning for global challenges to impact society and the
environment.
       PEO3: To demonstrate work productivity, leadership and managerial skills, ethics, and human
value in progressive career path.
PEO4: To exhibit communication skill and collaborative skill plan and participate in multidisciplinary
Computer Science & Engineering fields.
               CSE DEPARTMENT PROGRAM SPECIFIC OUTCOMES (PSOs)
B.Tech Computer Science & Engineering Department has the following Program Specific Outcomes:
PSO1: To analyze and demonstrate, the recent engineering practices, ethical values and strategies in
real-time world problems to meet the challenges for the future.
PSO2: To develop adaptive computing system using computational intelligence strategies and
algorithmic design to address diverse data analysis and machine learning challenges.
                                                  4
PROGRM OUTCOMES
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
   fundamentals, and an engineering specialization to the solution of complex engineering
   problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
   engineering problems reaching substantiated conclusions using first principles of
   mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
   design system components or processes that meet the specified needs with appropriate
   consideration for the public health and safety, and the cultural, societal, and
   environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
   methods including design of experiments, analysis and interpretation of data, and
   synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
   modern engineering and IT tools including prediction and modelling to complex
   engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
   assess societal, health, safety, legal and cultural issues and the consequent responsibilities
   relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
   solutions in societal and environmental contexts, and demonstrate the knowledge of, and
   need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
   norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
   leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
    engineering community and with society at large, such as, being able to comprehend and
    write effective reports and design documentation, make effective presentations, and give
    and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
    engineering and management principles and apply these to one’s own work, as a member
    and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage
    in independent and life-long learning in the broadest context of technological change.
                                                5
GENERAL LABORATORY INSTRUCTIONS
1. Students are advised to come to the laboratory at least 5 minutes before (to the starting
time), those who come after 5 minutes will not be allowed into the lab.
2. Plan your task properly much before to the commencement, come prepared to the lab with
the synopsis / program / experiment details.
3. Student should enter into the laboratory with:
    •   Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
        Procedure, Program, Expected Output, etc.,) filled in for the lab session.
    •   Laboratory Record updated up to the last session experiments and other utensils (if
        any) needed in the lab.
    •   Proper Dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation
note book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain
the discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which
should be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract
severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go out; if
anybody found loitering outside the lab / class without permission during working hours will
be treated seriously and punished appropriately.
10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the
lab after completing the task (experiment) in all aspects. He/she must ensure the system / seat
is kept properly.
                                                4
               DETAILS OF THE EXPERIMENTS CONDUCTED
                       (TO BE USED BY THE STUDENTS IN THEIR RECORDS)
                                                INDEX
S.No                TITLE OF THE EXPERIMENT                            DATE OF     FACULTY
                                                                     SUBMISSION   SIGNATURE
 1     Implement Lexical Analyzer to identify the identifiers.
 2     Implement Lexical Analyzer to identify the keywords.
 3     Write a program of a grammar, which accepts the odd no. of
       zero.
 4     Write a program of a grammar, which accepts the even no.
       of zero.
 5     Write a program to convert a given regular expression a
       (a|b)*b in to deterministic finite automata.
 6     Write a program to identify that the given grammar is Left
       recursive or not.
 7     Write a program to generate tokens for the given grammar.
 8     Develop an operator precedence parser for a given language.
 9     Write program to find Simulate First and Follow of any
       given grammar.
 10    Construct a recursive descent parser for an expression.
 11    Construct a Shift Reduce Parser for a given language.
 12    Write a program to perform loop unrolling.
 13    Implement intermediate code generation for simple
       expression.
 14    Implement the back end of the compiler which takes the
       three-address code and produces the 8086 assembly
       language instructions that can be assembled and run using
       an 8086 assembler. The target assembly instructions can
       be simple move, add, sub, jump etc.
                                                    5
                   STUDY AND EVALUATION SCHEME
Course         Course
                                 Teaching Scheme             Credits Assigned
  Code          Name
              Compiler     Theory Practical Tutorial Theory Practical   Tutorial Total
BCS-652 (P)   Design Lab
                            00         02          00   00      01        00      01
                                      (50
                                     Marks)
                                              6
                                        IMS Engineering College
                        NH-09, Adhyatmik Nagar, Near Dasna, Distt. Ghaziabad, U.P.
                                                Tel: (0120) 4940000
                   Department of Computer Science and Engineering
         Course Name: Compiler Design Lab                               Course Code: BCS-652
         Semester / Year: 6th /3rd                                       NBA Code: C 308
                                                                                                    Bloom’s
                                  COURSE OUTCOMES                                                    Level
              Identify patterns, tokens & regular expressions for lexical analysis.                 K2,K4
   C308.1
              Design Lexical analyser for given language using C and LEX /YACC tools’               K3,K5
   C308.2
              Design and analyze top down and bottom up parsers.                                    K4, K5
   C308.3
                                                                                                    K4, K5
   C308.4     Generate the intermediate code.
              Generate machine code from the intermediate code forms                                K3, K4
   C308.5
                                                   CO-PO Matrix
Course
            PO 1   PO2     PO3    PO4    PO5     PO6    PO7       PO8   PO9    PO10   PO11   PO12   PSO1     PSO2
Outcome
C308.1       2      2        2     1      2        1         1     1      2     1      2      2       2       1
C308.2       2      2        2     2      2        1         1     1      2     1      1      1       1       2
C308.3       2      2        2     2      1        1         1     1      2     1      1      1       2       1
C308.4       2      2        1     1      1        1         1     1      2     1      1      1       1       2
C308.5       2      2        1     2      1        1         1     1      2     1      1      2       2       2
 C308       2.0    2.0      1.6   1.6     1.4     1.0       1.0   1.0    2.0   1.0    1.2    1.4      1.6     1.6
                                                        7
                                 LIST OF PROGRAMS
     Subject: Compiler Design Lab                                              Code: BCS-552
S.NO.                          NAME OF EXPERIMENT                                      MAPPING
                                                                                       WITH CO
1.      Implement Lexical Analyzer to identify the identifiers.                          C308.2
2.      Implement Lexical Analyzer to identify the keywords.                             C308.2
3.      Write a program of a grammar, which accepts the odd no. of zero.                 C308.1
4.      Write a program of a grammar, which accepts the even no. of zero.                C308.1
5.      Write a program to convert a given regular expression a (a|b)*b in to            C308.3
        deterministic finite automata.
6.      Write a program to identify that the given grammar is Left recursive or not.     C308.3
7.      Write a program to generate tokens for the given grammar.                        C308.1
8.      Develop an operator precedence parser for a given language.                      C308.3
9.      Write program to find Simulate First and Follow of any given grammar.            C308.3
10.     Construct a recursive descent parser for an expression.                          C308.3
11.     Construct a Shift Reduce Parser for a given language.                            C308.3
12.     Write a program to perform loop unrolling.                                       C308.3
13.     Implement intermediate code generation for simple expression.                    C308.4
14.     Implement the back end of the compiler which takes the three-address code        C308.5
        and produces the 8086 assembly language instructions that can be assembled
        and run using an 8086 assembler. The target assembly instructions can be
        simple move, add, sub, jump etc.
                                                  8
                                    EXPERIMENT NO. - 1
AIM: Implement Lexical Analyzer to identify the identifiers.
Code:
#include <stdio.h>
#include <string.h>
void main() {
  char a[10];
  int i, l, flag = 0;
  printf("Name : Ayush Pratap singh , Roll no : 2201430100073\n");
  printf("Enter the string to be matched: ");
  fgets(a, sizeof(a), stdin);
    a[strcspn(a, "\n")] = 0;
    l = strlen(a);
    if ((a[0] >= 'a' && a[0] <= 'z') ||
       (a[0] >= 'A' && a[0] <= 'Z') ||
        a[0] == '_')
    {
      for (i = 1; i < l; i++) {
         if (!((a[i] >= 'a' && a[i] <= 'z') ||
              (a[i] >= 'A' && a[i] <= 'Z') ||
              (a[i] >= '0' && a[i] <= '9') ||
               a[i] == '_'))
         {
            flag = 1;
            break;
         }
      }
       if (flag == 1) {
          puts("This is an invalid identifier.");
       } else {
          puts("Valid identifier.");
       }
    } else {
       puts("This is an invalid identifier.");
    }
}
                                                    9
Output:
          10
                                   EXPERIMENT NO. - 2
AIM: Implement Lexical Analyzer to identify the keywords.
Code:
#include <stdio.h>
#include <string.h>
void main() {
printf("Name: Ayush Pratap Singh , Roll No.: 2201430100073\n");
    char a[32][10] = {
       "auto", "break", "case", "char", "const", "continue", "default",
       "do", "double", "else", "enum", "extern", "float", "for", "goto",
       "if", "int", "long", "register", "return", "short", "signed",
       "sizeof", "static", "struct", "switch", "typedef", "union",
       "unsigned", "void", "volatile", "while"
    };
    char b[30];
    int i, c;
    printf("Enter the string to be matched:\n");
    fgets(b, sizeof(b), stdin);
    b[strcspn(b, "\n")] = 0;
    for (i = 0; i < 32; i++) {
       c = strcmp(a[i], b);
       if (c == 0) {
          printf("This string is a valid keyword\n");
          return;
       }
    }
    printf("Given string is an invalid keyword\n");
}
                                                    11
Output:
          12
                                     EXPERIMENT NO. - 3
AIM: Write a program of a grammar which accepts the odd no. of zero
Code:
#include <stdio.h>
#include <string.h>
#define q0 0
#define q1 1
void main() {
  char a[10];
  int s, i;
  printf("Enter a binary string: ");
   printf("Name: Ayush Pratap Singh
  , Roll No.: 2201430100073\n");
  fgets(a, sizeof(a), stdin);
  a[strcspn(a, "\n")] = 0;
  s = q0;
  for (i = 0; a[i] != '\0'; i++) {
     if (a[i] == '0' && s == q0)
         s = q1;
     else if (a[i] == '0' && s == q1)
        s = q0;
     else if (a[i] == '1')
        continue;
     else { printf("Invalid binary string.\n");
        return; }}
  if (s == q1)
     printf("Grammar is accepted.\n");
  else printf("Grammar is not accepted.\n"); }
Output:
                                                  13
                                  EXPERIMENT NO. - 4
AIM: Write a program of a grammar which accepts the even no. of zero
Code:
 #include <stdio.h>
 #include <string.h>
 #define q0 0
 #define q1 1
 void main() {
   char a[20];
   int s, i;
   printf("Enter a binary string: ");
     printf("Name: Ayush Pratap
     Singh , Roll No.:
     2201430100073\n");
     fgets(a, sizeof(a), stdin);
     a[strcspn(a, "\n")] = 0;
     s = q0;
     for (i = 0; a[i] != '\0'; i++) {
        if (a[i] == '0' && s == q0)
            s = q1;
        else if (a[i] == '0' && s == q1)
            s = q0;
        else if (a[i] == '1')
            continue;
        else {
            printf("Invalid binary string.\n");
            return; }}
     if (s == q0)
        printf("Grammar is accepted.\n");
     else
        printf("Grammar is not accepted.\n");
 }
Output:
                                                  14