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

THE TECHNOLOGICAL INTITUTE OF TEXTILE AND SCIENCE’S

BIRLA COLONY, BHIWANI – 127021

COMPILER DESIGN
LAB
SUBMITTED TO SUBMITTED BY
NAME:- NITISH BANSAL
MR. AJIT SINGH ROLL NO:- 19CE076
UNV ROLL NO:
INDEX
Teacher’s
S.NO. EXPERIMENT Date
Signature
Study about Language Processing
1.
System
Study about Phases
2.
of Compiler
Program to Tokenize a String
3.
Program to Check a String for an
4.
Identifier
Program to Check a String for the
5.
Keyword
Program to Check a String for the
6.
Comment
Program to Print the Leading
7.
Terminals of all Production Rules
Program to Print the Trailing
8.
Terminals of all Production Rules
EXPERIMENT – 1
AIM: - Study about Language Processing System

THEORY: -
The Computer is an Intelligent Combination of Software and Hardware.
Hardware is simply a piece of mechanical equipment and its functions are being
compiled by the relevant Software. The Hardware considers instructions as
electronic charge, which is equivalent to the Binary Language in Software
Programming. The Binary Language has only 0s and 1s. To enlighten, the
Hardware code has to be written in binary format, which is just a series of 0s
and 1s. Writing such code would be an inconvenient and complicated task for
computer programmers, so we write programs in a High-Level Language (HLL),
which is Convenient for us to comprehend and memorize. These programs are
then fed into a series of devices and Operating System (OS) components to
obtain the desired code that can be used by the machine. This is known as
Language Processing System.
 Different Components of LPS
The Different Components of Language Processing System are: -

1. PREPROCESSOR
The Preprocessor includes all Header Files and also Evaluates whether a
Macro is included. It takes Source Code as input and produces Modified
Source Program (Pure HLL) as output. The Preprocessor is also known as a
Macro Evaluator.

2. COMPILER
The Compiler takes the Modified Source Program as input and produces the
Target Code (Assembly Language) as output.

The compiler specifies the errors at the end of the compilation with line
numbers when there are any errors in the Source Program.

3. ASSEMBLER
The Assembler takes the Target Code as input and produces Relocatable
Machine Code as output.

4. LINKER/LOADER
 LINKER
A Linker or Link Editor is a Program that takes a Collection of Objects
(created by Assemblers and Compilers) and combines them into an
Executable Program.
 LOADER
The Loader Keeps the Linked Program in the Main Memory.

5. EXECUTABLE CODE
It is the low level and machine specific code and machine can easily
understand. Once the job of linker and loader is done then object code
finally converted it into the executable code.
EXPERIMENT – 2
AIM: - Study about Phases of Compiler

THEORY: -
We basically have Two Phases of Compiler, namely the Analysis Phase and
Synthesis Phase. The Analysis phase creates an Intermediate Representation from
the given Source Code. The Synthesis Phase creates an equivalent Target Program
from the Intermediate Representation.

 Symbol Table
It is a Data Structure being used and maintained by the Compiler,
consisting of all the Identifier’s names along with their types. It helps the
Compiler to function smoothly by finding the Identifiers quickly.
 Different Phases of Compiler
The Compiler has two modules namely the Front-End and the Back-End.
Front-End constitutes the Lexical Analyzer, Syntax Analyzer, Semantic Analyzer
and Intermediate Code Generator. And the rest two are assembled to form
the Back-End.

1. Lexical Analyzer
It is also called a Scanner. It takes the output of the Preprocessor as the
input which is in a Pure High-Level Language. It reads the characters from
the Source Program and groups them into Lexemes (sequence of characters
that “go together”). Each Lexeme corresponds to a Token. Tokens are
defined by regular expressions which are understood by the Lexical
Analyzer. It also removes Lexical Errors, Comments, and White Space.

2. Syntax Analyzer
It is sometimes called a Parser. It constructs the Parse Tree. It takes all the
Tokens one by one and uses Context-Free Grammar to construct the Parse
Tree.
The Parse Tree is also called the Derivation Tree. Parse Trees are generally
constructed to check for ambiguity in the given Grammar. There are certain
Rules associated with the Derivation Tree: -
 Any Identifier is an Expression
 Any Number can be called an Expression
 Performing any operations in the given Expression will always
result in an Expression. For example, the sum of two Expressions is
also an Expression.
 The Parse tree can be compressed to form a Syntax Tree.
Syntax Error can be detected at this level if the input is not in accordance
with the Grammar.

3. Semantic Analyzer
It verifies the Parse Tree, whether it’s meaningful or not. It furthermore
produces a Verified Parse Tree. It also does Type Checking, Label Checking,
and Flow Control Checking.
4. Intermediate Code Generator
It generates Intermediate Code, which is a form that can be readily
executed by a Machine. We have many popular Intermediate Codes.
Intermediate Code is converted to Machine Language using the last two
Phases which are platform dependent.
Till Intermediate Code, it is the same for every Compiler out there, but after
that, it depends on the platform. To build a new Compiler, we don’t need to
build it from Scratch. We can take the Intermediate Code from the already
Existing Compiler and build the last two parts.

5. Code Optimizer
It transforms the Code so that it consumes fewer resources and produces
more speed. The meaning of the Code being transformed is not altered.
Optimization can be categorized into two types: -
 Machine-Dependent
 Machine-Independent.

6. Target Code Generator


The main purpose of the Target Code Generator is to write a Code that the
Machine can understand and also Register Allocation, Instruction Selection,
etc. The output is dependent on the type of Assembler. This is the Final
Stage of Compilation. The Optimized Code is converted into Relocatable
Machine Code which then forms the input to the Linker and Loader.

All these Six Phases are associated with the Symbol Table and Error Handler as
shown in the above Block Diagram.
EXPERIMENT – 3
AIM: - Program to Tokenize a String

import java.util.*;
public class SampleTokenize
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("\n Give the String you want to Tokenize
for:\n");
String text = scan.nextLine();
text = text.replaceAll("\\+", " + ");
text = text.replaceAll("\\-", " - ");
text = text.replaceAll("\\/", " / ");
text = text.replaceAll("\\*", " * ");
text = text.replaceAll("\\=", " = ");
text = text.replaceAll("\\,", " , ");
text = text.replaceAll("\\;", " ; ");
String[] tokens = text.split(" +");
System.out.println(“\n”);
System.out.println(Arrays.toString(tokens));
}
}
OUTPUT
EXPERIMENT – 4
AIM: - Program to Check a String for an Identifier

import java.util.*;
import java.util.regex.*;
public class SampleIdentifier
{
static final String keywords[] = { "abstract", "assert", "boolean", "break",
"byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"extends", "false", "final", "finally", "float",
"for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long",
"native", "new", "null", "package", "private",
"protected", "public", "return", "short",
"static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws",
"transient", "true", "try", "void", "volatile",
"while"};
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("\n Give the String you want to Check for:\n");
String text = scan.nextLine();
Pattern regex = Pattern.compile("[$_]*[a-zA-Z]+[a-zA-Z0-9_$]*");
Matcher temp = regex.matcher(text);
boolean isidentifier = temp.matches();
int iskeyword = Arrays.binarySearch(keywords, text);
if(iskeyword>0)
{
isidentifier = false;
}
if(isidentifier==true)
{
System.out.println("\n**This is a Valid Identifier in case of Java
Programming Language**");
}
else
{
System.out.println("\n**This is an Invalid Identifier in case of Java
Programming Language**");
}
}
}
OUTPUT
EXPERIMENT – 5
AIM: - Program to Check a String for the Keyword

import java.util.*;
public class SampleKeyword
{
static final String keywords[] = { "abstract", "assert", "boolean", "break",
"byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"extends", "false", "final", "finally", "float",
"for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native",
"new", "null", "package", "private",
"protected", "public", "return", "short",
"static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws",
"transient", "true", "try", "void", "volatile",
"while"};
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("\n Give the String you want to Check for:\n");
String text = scan.nextLine();
int iskeyword = Arrays.binarySearch(keywords, text);
if(iskeyword>0)
{
System.out.println("\n** This is a Keyword in case of Java
Programming Language **");
}
else
{
System.out.println("\n** This is not a Keyword in case of Java
Programming Language **");
}
}
}
OUTPUT
EXPERIMENT – 6
AIM: - Program to Check a String for the Comment

import java.util.*;
import java.util.regex.*;
public class SampleComment
{
public static void main(String args[])
{
int flag = 0;
Scanner scan = new Scanner(System.in);
System.out.println("\nGive the String you want to Check for:\n");
String text = scan.nextLine();
Pattern regexsingle = Pattern.compile("^(//).*");
Matcher tempsingle = regexsingle.matcher(text);
boolean iscommentsingle = tempsingle.matches();
if(iscommentsingle==true)
{
flag = 1;
System.out.println("\nThis is a Single Line Comment in case of
Java Programming Language");
}
if(text.startsWith("/*") && text.endsWith("*/"))
{
flag = 1;
System.out.println("\nThis is a Multi-Line Comment in case of
Java Programming Language");
}
if(flag==0)
{
System.out.println("\nThis is not a Comment in case of Java
Programming Language");
}
}
}
OUTPUT
EXPERIMENT – 7
AIM: - Program to Print the Leading Terminals of all the
Production Rules

import java.util.*;
import java.io.*;
public class SampleLeading
{
public static void main(String args[])
{
int productions = 0;
char temp;
char[] nonterminal = new char[10];
char[] leading = new char[10];
String[] prorule = new String[10];
Scanner scan = new Scanner(System.in);
System.out.println("\nGive the Number of Production Rules:\n");
productions = scan.nextInt();
System.out.println("\nNow, Enter the Production Rules One by One:\n");
for(int i=0; i<=productions; i++)
{
prorule[i] = scan.nextLine();
}
for(int i=0; i<=productions;i++)
{
char[] ch = prorule[i].toCharArray();
for(int j=0; j<ch.length; j++)
{
nonterminal[i] = ch[0];
if(ch[j]=='>')
{
for(int k=j+1; k<ch.length; k++)
{
if(!Character.isUpperCase(ch[k]))
{
leading[i] = ch[k];
break;
}
}
}
}
}
System.out.println("\nNow, The Leading Terminals of all Production
Rules are given as:\n");
for(int i=1; i<=productions; i++)
{
System.out.println(nonterminal[i]+" = { "+leading[i]+" }");
}
}
}
OUTPUT
EXPERIMENT – 8
AIM: - Program to Print the Trailing Terminals of all the
Production Rules

import java.util.*;
import java.io.*;
public class SampleTrailing
{
public static void main(String args[])
{
int productions = 0;
char temp;
char[] nonterminal = new char[10];
char[] leading = new char[10];
String[] prorule = new String[10];
Scanner scan = new Scanner(System.in);
System.out.println("\nGive the Number of Production Rules:\n");
productions = scan.nextInt();
System.out.println("\nNow Enter the Production Rules One by One:\n");
for(int i=0; i<=productions; i++)
{
prorule[i] = scan.nextLine();
}
for(int i=0; i<=productions; i++)
{
char[] ch = prorule[i].toCharArray();
for(int j=0; j<ch.length; j++)
{
nonterminal[i] = ch[0];
if(ch[j]=='>')
{
for(int k=j+1; k<ch.length; k++)
{
if(!Character.isUpperCase(ch[k]))
{
leading[i] = ch[k];
}
}
}
}
}
System.out.println("\nNow, The Trailing Terminals of all Production
Rules are given as:\n");
for(int i=1; i<=productions; i++)
{
System.out.println(nonterminal[i]+" = { "+leading[i]+" }");
}
}
}
OUTPUT
EXPERIMENT – 9
AIM: - Program to Check a String for a particular Regular
Expression (a*b+)

import java.util.regex.*;
import java.util.*;
public class SampleExpression
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("\nGive the String you want to Check for:\n");
String temp = scan.nextLine();
Pattern regex = Pattern.compile("a*b+");
Matcher text = regex.matcher(temp);
boolean result = text.matches();
if(result)
{
System.out.println("\n* Yes, The String belongs to the Regular
Expression (a*b+) *");
}
else
{
System.out.println("\n* No, The String does not belongs to
the Regular Expression (a*b+) *");
}
}
}
OUTPUT

You might also like