Experiment 1
Experiment 1
simulation.
JFLAP, or Java Formal Languages and Automata Package, is a software tool designed for students
and educators in computer science. It specifically targets the field of formal languages and automata
theory, which explores theoretical models of computation.
JFLAP's primary function is to provide an interactive environment for simulating these concepts.
Here's a breakdown of what JFLAP offers:
Constructing Automata: JFLAP allows you to build various automata models, including
Finite State Automata (FSA), Pushdown Automata (PDA), and Turing Machines (TM). These
models are the building blocks for understanding how languages are recognized and
processed by computers.
Experimentation: Once you've constructed an automata model, JFLAP lets you test it with
different inputs. This allows you to visualize how the model behaves and verify its
functionality.
Conversions and Proofs: JFLAP goes beyond just simulation. It can help you explore the
relationships between different automata types. For instance, you can convert a Non-
Deterministic Finite Automata (NFA) to a Deterministic Finite Automata (DFA) and analyze
the process.
Grammar Exploration: JFLAP supports working with formal grammars, which are another
way to define languages. You can create different grammar types and see how they generate
specific sets of strings.
JFLAP provides a user-friendly interface to construct and simulate DFAs. Here's a general
guideline for implementing a DFA:
Start by clearly defining the language your DFA will recognize. This could be a set of
strings containing specific characters or patterns.
Open JFLAP and choose the "Finite Automaton" option to create a new automaton.
Use the "State Creator" tool to create states for your DFA. These states represent
different stages the DFA can be in while processing an input string.
Designate one state as the initial state, where processing begins. You can use the
"Attribute Editor" tool to mark this state.
Similarly, mark some states as final states using the "Attribute Editor" tool. These
states represent successful recognition of the language.
Use the "Transition Creator" tool to define transitions between states. Each transition
is labeled with a symbol from the input alphabet.
3. Define Transitions:
For each state, define transitions for all possible input symbols. Remember, a DFA
has a deterministic transition for every symbol in every state.
The transition label indicates the symbol that triggers the move and the destination
state after reading that symbol.
4. Consider an Example:
Imagine building a DFA that recognizes strings starting with '0' and ending with '1', with any
number of '0's in between.
Once your DFA is built, you can use JFLAP's simulation features to test it with
different input strings.
The simulation will show the path the DFA takes through the states based on the input
symbols.
Analyze if the DFA reaches a final state for valid strings and remains elsewhere for
invalid ones.
JFLAP allows you to build Non-deterministic Finite Automata (NFAs) as well, but the
process differs slightly from DFAs due to the inherent non-determinism. Here's how to
implement an NFA using JFLAP:
Similar to DFAs, start by outlining the language your NFA will recognize.
The initial steps are the same as building a DFA in JFLAP. Create states, designate an
initial state, and mark final states using the respective tools.
3. Define Transitions:This is where the key difference lies. Unlike DFAs with single
outgoing transitions per symbol, NFAs can have multiple outgoing transitions for a single
symbol from a state.
These transitions represent the non-deterministic choices the NFA can make during
processing.
4. Consider an Example:
Imagine building an NFA that recognizes strings containing either the substring "ab" or the
symbol "c".
You can design states q0 (initial), q1, q2 (for "ab"), and q3 (final).
From q0, create transitions labeled 'a' leading to both q1 and q2 (non-deterministic
choice for "ab").
q1 will have a transition labeled 'b' leading to q3 (reaching the final state after "ab").
q2 can have a transition labeled anything (including 'a' or 'b') leading back to itself
(representing unsuccessful "ab" but continuing processing).
Additionally, from q0, create a separate transition labeled 'c' leading directly to q3
(recognizing the symbol "c").
Simulating an NFA in JFLAP might involve showing multiple possible paths the
NFA could take for a given input string due to its non-determinism.
JFLAP might highlight all reachable states at each step based on the choices made.
A string is accepted if at least one path leads to a final state.
JFLAP streamlines the conversion process from Deterministic Finite Automata (DFAs) to
Regular Grammars. Here's a breakdown of how to achieve this:
JFLAP actually doesn't directly convert Deterministic Finite Automata (DFAs) to Regular
Expressions. However, it provides functionalities that can help you achieve this conversion
indirectly. Here's how:
EXPERIMENT 8: Design a Context Free Grammars (CFG) with Single Symbols based on JFLAP.
JFLAP can certainly help in designing and experiment with various CFGs that use both non-
terminal and terminal symbols. Here's a basic example:
Language: Set of strings with zero or more 'a's followed by a single 'b'
Grammar in JFLAP:
This CFG can generate strings like "b", "ab", "aab", "aaab", etc.
By experimenting with JFLAP and understanding the role of non-terminals, you can
effectively design and analyze Context-Free Grammars for various language representations.
EXPERIMENT 9: Design a Context Free Grammars (CFG) with Multiple Symbols based on JFLAP.
JFLAP is a great tool for creating and experimenting with Context-Free Grammars (CFGs)
that utilize multiple symbols (both terminals and non-terminals). Here's an example:
Language: Set of strings starting with 'a' and ending with 'b', with any number of '0's and '1's
in between.
Steps in JFLAP:
Explanation of Productions:
The first rule (S -> a X b) defines the overall structure: starting with 'a', followed by
something represented by X, and ending with 'b'.
The next two rules (X -> X 0 and X -> X 1) allow for any number of '0's or '1's by
letting X rewrite itself with either symbol.
The final rule (X -> ε) enables X to be empty, allowing the string to have zero
characters between 'a' and 'b'.
JFLAP supports functionalities related to LL(1) Parsing, a specific parsing technique for
Context-Free Grammars (CFGs). Here's a breakdown of how to implement LL(1) Parsing
using JFLAP:
Open JFLAP and choose the "Grammar" option. Define your non-terminal and
terminal symbols, and create productions (rules) for your desired language.
Once your CFG is defined, navigate to the "Input" menu in JFLAP. Select "Build
LL(1) Parse Table."
JFLAP will generate a table showing entries for each combination of a non-terminal
symbol (on the left) and a terminal symbol (on the top). These entries represent
parsing actions, which can be:
o A production rule (e.g., A -> aBb) indicating how to rewrite the non-terminal.
o An error message signifying an invalid combination.
JFLAP offers a parsing feature. You can enter an input string and use the parse table
to determine if the string can be generated by your grammar. JFLAP will guide you
through the parsing steps based on the table entries.
Understanding LR Parsing:
LR Parsing techniques (SLR, LALR, LR(0)) are bottom-up parsing approaches for
CFGs. They work by building the parse tree from the rightmost side of the input
string.
JFLAP doesn't directly build LR parsing tables. However, you can leverage its
features to gain insights into LR parsing:
o Build the CFG: Create your grammar in JFLAP, defining non-terminals,
terminals, and productions. Understanding the grammar is essential for any
parsing technique.
o Simulate DFA/NFA (Optional): If your grammar is ambiguous or requires
lookahead symbols, it might be related to an SLR or LALR parser. JFLAP
allows you to build Deterministic Finite Automata (DFAs) or Non-
deterministic Finite Automata (NFAs) .
EXPERIMENT 12: Design a Regular Expression through JFLAP.
JFLAP excels at designing Regular Expressions (REs). Here's how you can achieve this:
1. Open JFLAP: Launch JFLAP and choose the "Regular Expression" option.
2. Construct your RE:
o In the text box provided, enter your desired regular expression using the
following symbols:
Letters (a-z, A-Z) for literal characters.
Numbers (0-9) for literal digits.
Special characters like:
. (dot) for any single character.
* (asterisk) for zero or more repetitions of the preceding
element.
+ (plus) for one or more repetitions of the preceding element.
? (question mark) for zero or one repetition of the preceding
element.
| (pipe) for OR (alternative choices).
[] (brackets) for a character class (set of characters).
3. Validate (Optional):
o JFLAP might offer a basic validation check to ensure your RE is syntactically
correct.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
}
int isKeyword(char* str) {
if (strcmp(str, keywords[i]) == 0) {
return 1;
return 0;
int main() {
int i = 0;
char ch;
if (isLetterOrUnderscore(buffer[i])) {
i++;
if (isKeyword(buffer)) {
return 0;
}
EXPERIMENT 14: Implementation of Lexical Analyzer for IF statements. (Using C)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
int i = 0;
char ch;
if (isLetterOrUnderscore(buffer[i])) {
i++;
buffer[i] = '\0';
if (strcmp(buffer, "if") == 0) {
printf("IF Keyword Found\n");
break;
i = 0;
} else {
i++;
return 0;
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_SIZE 50
return isdigit(ch);
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
int stack[MAX_SIZE];
char ch, x;
ch = infix[i];
if (isOperand(ch)) {
strcat(postfix, &ch);
stack[++top] = ch;
strcat(postfix, &stack[top--]);
if (top == -1) {
// Pop '('
x = stack[top--];
} else {
strcat(postfix, &stack[top--]);
stack[++top] = ch;
strcat(postfix, &stack[top--]);
int main() {
return 0;