Uploaded by Alisha Waseem

Compiler Construction (219)

advertisement
Name: Alisha Waseem
Section: Green
Roll number: 2K20-BSCS-219
Subject: Compiler Construction (LAB)
Submitted to: Ma’am Ujala Saleem
LAB#1:
Regular Expression and their Examples in Python?
Regular Expression or RegEx:
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx
can be used to check if a string contains the specified search pattern. Regular expressions
(often referred to as regex or regexp) in Python are implemented using the re module. Regular
expressions are a powerful tool for pattern matching and searching within strings.
Examples:
LAB#2:
Lexical Analysis:
Lexical Analysis:
Lexical analysis, often referred to as lexing, is the first phase of the compilation process in
computer science. It is a fundamental step in processing source code written in programming
languages. The main purpose of lexical analysis is to break down the source code into a
sequence of tokens, which are the smallest meaningful units in a programming language. These
tokens represent keywords, identifiers, literals, and other language constructs. It converts the
High level input program into a sequence of Tokens. Lexical Analysis can be implemented with
the Deterministic finite Automata. The output is a sequence of tokens that is sent to the parser
for syntax analysis.
Example:
Even and Odd Program in C:
Java:
C++:
TASK:
Make two text files named as “input.txt” and “vowels.txt” input file contains all
alphabets of English language.
Write a program that reads the contents of input and check whether it is vowel
or not if it is vowel then write it in vowels.txt file.
Output:
LAB No. 3:
Postfix, Infix, Prefix and their Conversions with codes and output:
INFIX expressions:
The typical mathematical form of expression that we encounter generally is known as infix
notation. In infix form, an operator is written in between two operands.
For example:
An expression in the form of A * (B + C)/ D is in infix form. This expression can be simply
decoded as: “Add B and C, then multiply the result by A, and then divide it by D for the final
answer.”
POSTFIX expressions:
In postfix expression, an operator is written after its operands. This notation is also known as
“Reverse Polish notation”.
For example:
The above expression can be written in the postfix form as A B C + * D /. This type of expression
cannot be simply decoded as infix expressions.
PREFIX expressions:
In prefix expression, an operator is written before its operands. This notation is also known as
“Polish notation”.
For example:
The above expression can be written in the prefix form as / * A + B C D. This type of expression
cannot be simply decoded as infix expressions.
Codes and output:
 Infix to Postfix
 Postfix to Infix Conversion:
 Infix to prefix:
 Prefix to Infix
 Postfix to Prefix
 Prefix to Postfix
LAB#4:
Symbol Table:
Symbol Table is an important data structure created and maintained by the compiler in
order to keep track of semantics of variables i.e. it stores information about the scope and
binding information about names, information about instances of various entities such as
variable and function names, classes, objects, etc. In computer science, a symbol table is a
data structure used by a language translator such as a compiler or interpreter, where each
identifier, constant, procedure and function in a program's source code is associated with
information relating to its declaration or appearance in the source.



It is built-in lexical and syntax analysis phases.
The information is collected by the analysis phases of the compiler and is used by the
synthesis phases of the compiler to generate code.
It is used by the compiler to achieve compile-time efficiency.
Code:
Code2:
TASK:
TOKENIZATION:
Tokenization is the process of breaking text into individual words or tokens. In Python, you can
tokenize text using various libraries, with one of the most common options being the Natural
Language Toolkit (NLTK).
Linear Search:
Linear search is a search algorithm where a sequential search is made over all items one by one.
Every item is checked and if a match is found then that particular item is returned, otherwise
the search continues until the end of the data collection.
CODE:
Binary Search:
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by
repeatedly dividing in half the portion of the list that could contain the item, until you've
narrowed down the possible locations to just one.
LAB#5:
DFA, RegEx Exercises, String Matching:
QUESTIONS:
a)
c)
d)
e)
LAB#6:
DFA , NFA and state conversions:
DFA:
DFA stands for "Deterministic Finite Automaton." It is a mathematical model and abstract
machine used in computer science and formal language theory to recognize and process regular
languages. DFAs are part of automata theory, a branch of theoretical computer.
NFA:
NFA stands for "Nondeterministic Finite Automaton." It is another type of mathematical model
and abstract machine used in computer science and formal language theory, just like the DFA
(Deterministic Finite Automaton). NFAs are also part of automata theory and are used to
recognize and process regular languages.
LAB#7:
Parser and Stack:
Stack:
In Python, a stack is a linear data structure that follows the Last-In-First-Out (LIFO)
principle. It is a collection of elements with two main operations:
1. Push: This operation adds an element to the top of the stack.
2. Pop: This operation removes the top element from the stack.
Stack code:
Stack and Pop Code:
LAB TASK:
Stack Push and Pop modification to accept alphabets and string:
OUTPUT:
Download