Download Project Report - Building A Lexical Analyzer and more Papers Compiler Design in PDF only on Docsity!
BUILDING A LEXICAL ANALYZER
A MINI PROJECT REPORT
Submitted by
DIVESH MEHTA [RA2011003011111]
TANISHQ KASHYAP [RA2011003011104]
Under the guidance of
Dr. C. Pretty Diana Cyril
Assistant Professor
In partial satisfaction of the requirements for the degree of
BACHELOR OF TECHNOLOGY
in
COMPUTER SCIENCE & ENGINEERING
SCHOOL OF COMPUTING
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
COLLEGE OF ENGINEERING AND TECHNOLOGY
KATTANKULATHUR - 603203
APRIL 2023
SRM INSTITUTE OF SCIENCE & TECHNOLOGY COLLEGE OF ENGINEERING & TECHNOLOGY S.R.M. NAGAR, KATTANKULATHUR โ 603 203 BONAFIDE CERTIFICATE Certified that this project report โ Building A Lexical Analyzerโ is the bonafide work of โ Divesh Mehta [RA2011003011111], Tanishq Kashyap[RA2011003011104]โ of III Year/VI
Sem B.tech(CSE) who carried out the mini project work under my supervision for the course
18CSC304J- Compiler Design in SRM Institute of Science and Technology during the academic year
2022-2023(Even sem).
SIGNATURE SIGNATURE
Dr. C. Pretty Diana Cyril Dr. Pushpalatha M
Assistant Professor Head of Department
Department of Computing Department of Computing
Technologies Technologies
TABLE OF CONTENTS Chapter No. Title Page No.
ABSTRACT iii
TABLE OF CONTENTS iv
LIST OF FIGURES v
1 INTRODUCTION
1.1 Project Aims and Objectives 1
1.2 Problem Statement
1.3 Scope and Applications
1.4 Software Requirements Specification
2 LITERATURE SURVEY 4
2.1 Existing system 4
2.2 Existing vs Proposed system 5
3 SYSTEM ARCHITECTURE AND DESIGN 6
3.1 Architecture Design 6
4 MODULES AND FUNCTIONALITIES 7
4.1 Module Description 7
5 CODING AND TESTING
5.1 CODES
5.1.1 Lexer Code
5.1.2 Parser Code
5.2 TESTING 5.2.1 Outputs 5.2.2 Unit Testing 5.2.3 Integration Testing
6 CONCLUSION AND FUTURE ENHANCEMENT 62
7 REFERENCES 63
iv
LIST OF FIGURES
Chapter No. Title Page No.
3.1.1 Architecture Diagram 6
4.1.1 Module Description 7
5.2.1.1.1 Base test output 56
5.2.1.2.1 Loop test output 57
5.2.1.3.1 Function test output 58
5.2.1.4.1 Invalid test 1 output 58
5.2.1.5.1 Invalid test 2 output 59
5.2.1.6.1 Invalid test 3 output 59
v
1.3 SCOPE AND APPLICATIONS
A lexical analyzer plays a crucial role in the compilation process by analyzing the source code of a
programming language and breaking it down into meaningful tokens. Here are some key points regarding the
scope and application of a lexical analyzer:
Programming language support
A lexical analyzer can be designed to support a specific programming language or a subset of
programming languages. The scope can range from simple languages like C to more complex ones like
Java or Python.
Token recognition
The primary purpose of a lexical analyzer is to recognize and categorize tokens in the source code.
Tokens can include keywords, identifiers, literals, operators, punctuation, and special symbols.
Error handling
A lexical analyzer should be capable of detecting and handling lexical errors, such as invalid
characters, unrecognized tokens, or malformed identifiers. It should provide informative error
messages to aid in debugging.
Efficiency and performance
The efficiency of a lexical analyzer is crucial as it is often the first phase of the compilation process. It
should be designed to process large codebases efficiently and minimize the time taken for tokenization.
Integration with the compiler
The output of the lexical analyzer is typically passed on to the subsequent phases of the compiler.
Therefore, it should provide a well-defined interface or output format that can be easily consumed by
the parser and other compiler components.
Language extensions and customization
Lexical analyzers can be extended to support language-specific features or customized to meet specific
requirements. This allows developers to tailor the analyzer for domain-specific languages or add
additional token categories as needed.
Tooling and development environments
Lexical analyzers are often integrated into development tools and IDEs to provide syntax highlighting,
code completion, and other code analysis features. The scope can include building plugins or
extensions for popular development environments.
Building a reliable and efficient lexical analyzer requires a thorough understanding of programming
languages, formal language theory, and compiler design principles. It is an integral component in the
development of compilers, interpreters, and various code analysis tools.
1.4 SOFTWARE REQUIREMENTS SPECIFICATION
This section describes the software and hardware requirements of the system
Software Requirements:
1. Programming Language:
Choose a programming language suitable for implementing the lexical analyzer. Common choices
include C, C++, Java, Python, or any language that provides the necessary features for string
processing, regular expressions, and file handling.
2. Development Environment:
Install an integrated development environment (IDE) or a text editor with syntax highlighting and
debugging capabilities. Some popular options include Visual Studio Code, Eclipse, IntelliJ IDEA, or
Sublime Text.
3. Libraries and Frameworks:
Depending on the programming language chosen, you may need to utilize specific libraries or
frameworks for string manipulation, regular expression matching, or other relevant functionalities. For
example, in Java, you might use libraries like ANTLR or JFlex.
4. Compiler Tools:
Consider utilizing compiler-related tools such as lex (for C and C++) or yacc/bison (for parser
generation) if they are relevant to the implementation approach.
Hardware Requirements:
1. Processor:
A modern processor with multiple cores and a decent clock speed will help in processing large source
code files efficiently.
2. Memory:
Sufficient RAM is required to store the source code being processed and any data structures used for
tokenization. The exact memory requirement depends on the size of the input files and the complexity
of the lexical analyzer.
3. Storage:
Adequate storage space is needed to store the source code files and any generated intermediate files
during the lexical analysis process.
4. Input/Output Devices:
Standard input and output devices such as a keyboard and a monitor are necessary for interacting with
the lexical analyzer during development and testing.
2.2 EXISTING VS PROPOSED SYSTEM
Existing System:
โ Manual tokenization process performed by developers. โ Time-consuming, error-prone, and lacks consistency. โ Limited error handling mechanisms. โ Heavy reliance on manual effort, makes the process inefficient. โ Lack of automation and adaptability to different programming languages.
Proposed System:
โ Automated tokenization using a lexical analyzer. โ Reduction in manual effort, improved accuracy, and consistency. โ Robust error handling with accurate error reporting. โ Language independence and extensibility for multiple programming languages. โ Improved efficiency and performance with optimized algorithms and data structures. โ Seamless integration with the compiler workflow, facilitating further compilation phases. โ Integration with development tools and IDEs for enhanced developer experience.
The proposed system aims to overcome the limitations of the existing manual tokenization process by
automating the tokenization phase with a lexical analyzer. This would lead to improved efficiency, accuracy,
and consistency in the compilation process. The proposed system also addresses the need for robust error
handling, adaptability to different programming languages, and integration with development tools for better
developer productivity.
CHAPTER 3 SYSTEM DESIGN 3.1 ARCHITECTURE DIAGRAM
Architecture Diagram for a Lexical Analyzer:
3.1.1 Architecture Diagram
CHAPTER 5
CODING AND TESTING
5.1 CODES
5.1.1 Lexer code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "y.tab.h" #define ANSI_COLOR_RED #define ANSI_COLOR_GREEN #define ANSI_COLOR_YELLOW #define ANSI_COLOR_BLUE #define ANSI_COLOR_MAGENTA #define ANSI_COLOR_CYAN #define ANSI_COLOR_RESET struct symboltable { char name[100]; char class[100]; char type[100]; char value[100]; int nestval; int lineno; int length; int params_count; } ST[1001]; struct constanttable { char name[100]; char type[100]; int length; } CT[1001]; int hash(char *str) {
int value = 0; for (int i = 0; i < strlen(str); i++) { value = 10 * value + (str[i] - 'A'); value = value % 1001; while (value < 0) value = value + 1001; } return value; } int lookupST(char *str) { int value = hash(str); if (ST[value].length == 0) { return 0; } else if (strcmp(ST[value].name, str) == 0) { } else { return value; for (int i = value + 1; i != value; i = (i + 1) % 1001) { if (strcmp(ST[i].name, str) == 0) { return i; } } return 0; } }
if (lookupST(str1)) { if (strcmp(ST[lookupST(str1)].class, "Identifier") == 0 && strcmp(str2, "Array Identifier") == 0) { printf("Error use of array\n"); exit(0); } return; } else { int value = hash(str1); if (ST[value].length == 0) { strcpy(ST[value].name, str1); strcpy(ST[value].class, str2); ST[value].length = strlen(str1); ST[value].nestval = 9999; ST[value].params_count = -1; insertSTline(str1, yylineno); return; } int pos = 0; for (int i = value + 1; i != value; i = (i + 1) % 1001) { if (ST[i].length == 0) { pos = i; break; } }
strcpy(ST[pos].name, str1); strcpy(ST[pos].class, str2); ST[pos].length = strlen(str1); ST[pos].nestval = 9999; ST[pos].params_count = -1; } } void insertSTtype(char *str1, char *str2) { for (int i = 0; i < 1001; i++) { if (strcmp(ST[i].name, str1) == 0) { strcpy(ST[i].type, str2); } } } void insertSTvalue(char *str1, char *str2) { for (int i = 0; i < 1001; i++) { if (strcmp(ST[i].name, str1) == 0 && ST[i].nestval == currnest) { strcpy(ST[i].value, str2); } } } void insertSTnest(char *s, int nest) { if (lookupST(s) && ST[lookupST(s)].nestval != 9999) { int pos = 0; int value = hash(s); for (int i = value + 1; i != value; i = (i + 1) % 1001) {
int getSTparamscount(char *s) { for (int i = 0; i < 1001; i++) { if (strcmp(ST[i].name, s) == 0) { return ST[i].params_count; } } return -1; } void insertSTF(char *s) { for (int i = 0; i < 1001; i++) { if (strcmp(ST[i].name, s) == 0) { strcpy(ST[i].class, "Function"); return; } } } void insertCT(char *str1, char *str2) { if (lookupCT(str1)) return; else { int value = hash(str1); if (CT[value].length == 0) {
strcpy(CT[value].name, str1); strcpy(CT[value].type, str2); CT[value].length = strlen(str1); return; } int pos = 0; for (int i = value + 1; i != value; i = (i + 1) % 1001) { if (CT[i].length == 0) { pos = i; break; } } strcpy(CT[pos].name, str1); strcpy(CT[pos].type, str2); CT[pos].length = strlen(str1); } } void deletedata(int nesting) { for (int i = 0; i < 1001; i++) { if (ST[i].nestval == nesting) { ST[i].nestval = 99999; } } } int checkscope(char *s) { int flag = 0;