BACHELOR OF COMPUTER SCIENCE
ENGINEERING & TECHNOLOGY
SCHOOL OF COMPUTER SCIENCE AND ENGINEERING
GALGOTIAS UNIVERSITY, GREATER NOIDA
UTTAR PRADESH
Lab File
Compiler Design
Course Code: R1UC627B
Name: Shruti Khanna
Admission No: 22SCSE1010087
Semester : VI
1|P a g e
Experiment 1
Aim/Objective:
To understand the basics of JFLAP (Java Formal Languages and Automata Package) tool for
simulation.
Tool:
JFLAP
Theory:
JFLAP is a software tool designed for experimenting with formal languages and automata theory. It
supports the creation and simulation of various automata, including Deter-ministic Finite Automata
(DFA), Non-deterministic Finite Automata (NFA), Pushdown Automata (PDA), and Turing machines.
Additionally, it facilitates conversions between different representations and enables working with
grammars and regular expressions.
Simulation:
1. Install JFLAP and explore its interface to understand its functionalities.
2. Create a simple DFA that accepts strings over the alphabet {0, 1} ending in “1”:
• States: q0 (start state), q1 (accepting state).
• Transitions: q0 0 q0, q0 1 q1, q1 0 q0, q1 1 q1.
• Start State: q0.
• Accepting State: q1.
3. Test the DFA with the following strings: 01, 11, 00.
Observation:
• JFLAP provides an intuitive interface for creating states and transitions, making it user-
friendly for beginners.
• Test Results:
– 01: Accepted (ends in state q1).
– 11: Accepted (ends in state q1).
– 00: Rejected (ends in state q0).
• The DFA accurately accepts strings ending in “1”, demonstrating JFLAP’s effec-
tiveness in simulating automata.
1
Output
Experiment 2
Aim/Objective:
To implement a Deterministic Finite Automaton (DFA) using JFLAP.
Tool:
JFLAP
Theory:
A Deterministic Finite Automaton (DFA) is a finite state machine where each state has exactly
one transition for each input symbol, and there are no epsilon (ϵ) transitions. A DFA accepts a
string if, after processing the entire input, it ends in an accepting state.
Simulation:
1. Design a DFA in JFLAP that accepts strings over the alphabet {a, b} ending with the
substring “ab”:
• States: q0 (start state), q1, q2 (accepting state).
• Transitions: q0 a q1, q0 b q0, q1 a q1, q1 b q2, q2 a q1, q2 b q0.
• Start State: q0.
• Accepting State: q2.
2. Test the DFA with the following strings: aab, abab, aba, abb.
Observation:
• The DFA was constructed with 3 states, where q2 is the accepting state for strings
ending in “ab”.
• Test Results:
– aab: Accepted (ends in state q2).
– abab: Accepted (ends in state q2).
– aba: Rejected (ends in state q1).
– abb: Rejected (ends in state q0).
• The DFA correctly identifies and accepts only those strings that end with “ab”,
validating its design.
Output
2
Experiment 3
Aim/Objective:
To implement a Non-deterministic Finite Automaton (NFA) using JFLAP.
Tool:
JFLAP
Theory:
A Non-deterministic Finite Automaton (NFA) allows multiple transitions for the same input
symbol from a given state and can include epsilon (ϵ) transitions. An NFA accepts a string if
there exists at least one path through the automaton that leads to an accepting state after
processing the input.
Simulation:
1. Design an NFA in JFLAP that accepts strings over the alphabet {0, 1} containing the
substring “01”:
• States: q0 (start state), q1, q2 (accepting state).
• Transitions: q0 0 q0, q0 1 q0, q0 0 q1, q1 1 q2, q2 0 q2, q2 1 q2.
• Start State: q0.
• Accepting State: q2.
2. Test the NFA with the following strings: 101, 010, 111, 000.
Observation:
• JFLAP’s interface supports non-determinism by allowing multiple transitions, such as
from q0 on input “0”.
• Test Results:
– 101: Accepted (contains substring “01”).
– 010: Accepted (contains substring “01”).
– 111: Rejected (does not contain “01”).
– 000: Rejected (does not contain “01”).
• The NFA accurately accepts strings containing “01” by exploring possible paths.
Output
3
Experiment 4
Aim/Objective:
To convert a Non-deterministic Finite Automaton (NFA) to a Deterministic Finite Au-
tomaton (DFA) using JFLAP.
Tool:
JFLAP
Theory:
The subset construction algorithm converts an NFA to an equivalent DFA by treating each
subset of NFA states as a single DFA state. JFLAP provides an automated tool to perform
this conversion, ensuring the resulting DFA accepts the same language as the original NFA.
Simulation:
1. Use the NFA from Experiment 3, which accepts strings over {0, 1} containing the
substring “01”.
2. Convert the NFA to a DFA using JFLAP’s conversion tool:
3. Test the resulting DFA with the following strings: 101, 010, 111, 000
Observation:
• The original NFA had 3 states, while the converted DFA has 4 states due to subset
• construction.
• Test Results:
– 101: Accepted (contains “01”).
– 010: Accepted (contains “01”).
– 111: Rejected (no “01”).
– 000: Rejected (no “01”).
• The DFA accepts the same language as the NFA, confirming the correctness of
JFLAP’s conversion process.
Output
4
Experiment 5
Aim/Objective:
To convert a Deterministic Finite Automaton (DFA) to a Regular Grammar using JFLAP.
Tool:
JFLAP
Theory:
A regular grammar is a formal grammar equivalent to a DFA, with productions of the form A →
aB or A → a, where A and B are non-terminals and a is a terminal. JFLAP can automatically
derive a regular grammar from a given DFA, reflecting its state transitions.
Simulation:
1. Use the DFA from Experiment 2, which accepts strings over {a, b} ending with “ab”.
2. Convert the DFA to a regular grammar using JFLAP:
3. Generate strings such as ab and aab using the grammar
Observation:
• The grammar’s productions directly correspond to the DFA’s transitions, with q2 (the
accepting state) allowing an ϵ production.
• Generated Strings: Strings like ab and aab were generated and confirmed to be accepted
by the DFA.
• The regular grammar accurately generates strings ending in “ab”, consistent with the
DFA’s language.
Output
5
Experiment 6
Aim/Objective:
To convert a Deterministic Finite Automaton (DFA) to a Regular Expression using JFLAP.
Tool:
JFLAP
Theory:
A regular expression describes the same language as a DFA. JFLAP employs the state
elimination method to convert a DFA into an equivalent regular expression, which can then
be used to represent the language in a more compact form.
Simulation:
1. Use the DFA from Experiment 2, which accepts strings over {a, b} ending with “ab”.
2. Convert the DFA to a regular expression using JFLAP:
• Resulting Regular Expression: (a + b)∗ ab.
3. Test the regular expression by converting it back to a DFA in JFLAP and testing with
strings aab and abab.
Observation:
• The resulting regular expression (a + b)∗ ab accurately describes the language of
strings ending in “ab”.
• Test Results: The converted DFA accepts aab and abab, consistent with the original
DFA’s behavior.
• JFLAP’s conversion process is efficient and produces a correct regular expression.
6
Experiment 7
Aim/Objective:
To convert a Regular Expression to a Deterministic Finite Automaton (DFA) using JFLAP.
Tool:
JFLAP
Theory:
A regular expression can be converted to an NFA using Thompson’s construction al-gorithm,
and subsequently to a DFA using the subset construction method. JFLAP automates this
process, allowing users to visualize and test the resulting DFA.
Simulation:
1. Use the regular expression (a + b)∗ b, which represents strings over {a, b} ending in
“b”.
2. Convert the regular expression to an NFA, then to a DFA using JFLAP:
• NFA Structure: Includes states for the (a + b)∗ loop and a transition on b to an
accepting state.
• Resulting DFA States: q0 (start state), q1 (accepting state).
• Transitions: q0 a q0, q0 b q1, q1 a q0, q1 b q1.
3. Test the DFA with the following strings: ab, bb, aa, b.
Observation:
• The NFA initially had multiple states, but the DFA simplified to 2 states after
conversion.
• Test Results:
– ab: Accepted (ends in state q1).
– bb: Accepted (ends in state q1).
– aa: Rejected (ends in state q0).
– b: Accepted (ends in state q1).
• The DFA correctly accepts strings ending in “b”, matching the language defined by the
regular expression.
Output
7
Experiment 8
Aim/Objective:
To design a Context-Free Grammar (CFG) with single symbols using JFLAP.
Tool:
JFLAP
Theory:
A Context-Free Grammar (CFG) consists of variables, terminals, production rules, and a start
symbol, generating languages that can be recognized by a Pushdown Automaton (PDA). In
this context, “single symbols” refers to terminals being individual characters (e.g., a, b).
Simulation:
1. Design a CFG in JFLAP for the language {anbn | n ≥ 0}:
• Productions: S → a S b | ϵ.
2. Generate strings such as ϵ, ab, and aabb using the CFG.
Observation:
• The CFG’s productions (S → a S b | ϵ) successfully generate strings like ϵ, ab, and aabb.
• PDA Test Results:
– aabb: Accepted (balanced a’s and b’s).
– aab: Rejected (unbalanced a’s and b’s).
• The PDA correctly validates the CFG’s language, accepting only strings with equal
numbers of a’s and b’s in the correct order.
Output
8
Experiment 9
Aim/Objective:
To design and simulate a Context Free Grammar (CFG) that accepts a language with multiple
symbols (e.g., strings of the form anbmck, where n, m, k ≥ 0) using JFLAP.
Tool:
JFLAP (Java Formal Languages and Automata Package).
Theory:
A Context Free Grammar (CFG) consists of variables, terminals, production rules, and a start
symbol. It generates context-free languages, such as those with nested or balanced structures.
The language anbmck includes strings with any number of a’s, followed by any number of b’s,
followed by any number of c’s, where terminals a, b, and c are distinct symbols.
Simulation:
1. Open JFLAP and select “Grammar” from the main menu.
2. Define the CFG for the language {anbmck | n, m, k ≥ 0}:
• Variables: S
• Terminals: a, b, c
• Productions: S → aS | bS | cS | ϵ
3. Enter the grammar in JFLAP’s grammar editor.
4. Use the “Parse” option to test strings such as ϵ, “abc”, “aabbcc”, and “aaab”.
5. Simulate the derivation using JFLAP’s “Derivation Table” or “Parse Tree” feature.
Observation:
• JFLAP’s grammar editor is intuitive, allowing easy input of productions.
• Test Results:
– ϵ: Accepted (empty string, via S → ϵ).
– “abc”: Accepted (via S → aS → abS → abcS → abc).
– “aabbcc”: Accepted (correct derivation with multiple symbols).
– “cba”: Rejected (does not follow production order).
• The CFG correctly accepts strings matching the pattern anbmck.
Output
Conclusion:
The experiment successfully demonstrated the design and simulation of a CFG
for the language a^n b^m c^k using JFLAP. The tool’s derivation and parsing
features effectively validated the grammar’s correctness, accepting valid strings
and rejecting invalid ones.
Experiment 10
Aim/Objective:
To implement and simulate LL(1) parsing for a given Context Free Grammar using JFLAP.
Tool:
JFLAP.
Theory:
LL(1) parsing is a top-down parsing technique where:
• L: Left-to-right scanning of input.
• L: Leftmost derivation.
• 1: One lookahead symbol.
An LL(1) grammar must be unambiguous, free of left recursion, and factored to ensure a
unique production for each terminal. The parsing table is built using FIRST and FOLLOW
sets.
Simulation:
1. Open JFLAP and select “Grammar”.
2. Define a CFG for the language {anbn | n ≥ 0}:
• Variables: S
• Terminals: a, b
• Productions: S → aSb | ϵ
3. Verify the grammar is LL(1) (no left recursion, FIRST sets are disjoint).
4. Select “LL(1) Parse” and generate the parsing table.
5. Test strings such as ϵ, “ab”, “aabb”, and “aab” using the parsing table.
6. Observe stack operations and derivation steps.
Observation:
• The parsing table correctly maps productions based on FIRST and FOLLOW sets.
• Test Results:
– ϵ: Accepted (via S → ϵ).
– “ab”: Accepted (via S → aSb → ab).
– “aabb”: Accepted (via S → aSb → aaSbb → aabb).
– “aab”: Rejected (unbalanced a and b).
• JFLAP’s visualization of the parsing process clarifies stack-based decisions.
Output:
Conclusion:
The LL(1) parser was successfully implemented and tested in JFLAP, correctly parsing
strings in the language a^n b^n . The tool’s parsing table and stack visualization confirmed
the effectiveness of top-down parsing for the given CFG.
Experiment 11
Aim/Objective:
To implement and simulate LR(0) or SLR(1) parsing for a given Context Free Grammar using
JFLAP.
Tool:
JFLAP.
Theory:
LR parsing is a bottom-up parsing technique:
• L: Left-to-right scanning.
• R: Rightmost derivation (in reverse).
• 0 or 1: Number of lookahead symbols (LR(0) or SLR(1)).
LR parsing uses a DFA to recognize viable prefixes and a parsing table with actions (shift, reduce,
accept, or error). SLR(1) uses FOLLOW sets to resolve conflicts.
Simulation:
1. Open JFLAP and select “Grammar”.
2. Define a CFG for the language {anbn | n ≥ 0}:
• Variables: S
• Terminals: a, b
• Productions: S → aSb | ϵ
3. Select “SLR(1) Parse” and generate the LR(0)/SLR(1) automaton.
4. Build the parsing table.
5. Test strings such as ϵ, “ab”, “aabb”, and “aab” with shift/reduce actions.
6. Observe stack and action transitions.
Observation:
• The SLR(1) automaton has multiple states corresponding to grammar items.
• Test Results:p
– ϵ: Accepted (reduce by S → ϵ).
– “ab”: Accepted (shift and reduce actions).
– “aabb”: Accepted (multiple shift/reduce steps).
– “aab”: Rejected (parsing error due to imbalance).
• JFLAP’s automaton and table visualization clarifies the parsing process.
Output:
Conclusion:
The experiment successfully implemented an SLR(1) parser in JFLAP, accurately parsing
strings in the language a n b n . JFLAP’s visualization of the parsing table and automaton
demonstrated the efficiency of bottom-up parsing.
Experiment 12
Aim/Objective:
To design and simulate a regular expression for strings containing the substring “ab” over the
alphabet {a, b} using JFLAP.
Tool:
JFLAP.
Theory:
A regular expression (RE) describes a regular language using literals (a, b), concatena- tion,
union (|), and Kleene star (∗ ). The RE (a|b)∗ ab(a|b)∗ represents strings over {a, b}
containing “ab”. JFLAP converts REs to NFAs or DFAs for simulation.
Simulation:
1. Open JFLAP and select “Regular Expression”.
2. Enter the regular expression (a|b)∗ ab(a|b)∗ .
3. Convert the RE to an NFA using JFLAP’s conversion tool.
4. Optionally, convert the NFA to a DFA.
5. Test strings such as “ab”, “aab”, “baba”, and “bb” using the “Input” feature.
6. Use the “Step” option to observe transitions.
Observation:
• The NFA and DFA correctly recognize the substring “ab”.
• Test Results:
– “ab”: Accepted (contains “ab”).
– “aab”: Accepted (contains “ab”).
– “baba”: Accepted (contains “ab”).
– “bb”: Rejected (no “ab”).
• JFLAP’s visualization shows clear state transitions for the substring.
Output:
Conclusion:
The regular expression (a|b) ∗ ab(a|b) ∗ was successfully designed and simulated in JFLAP,
correctly identifying strings containing the substring “ab”. The tool’s conversion and testing
capabilities verified the RE’s accuracy.
Experiment 13
Aim/Objective:
To develop a lexical analyzer using JFLAP to recognize patterns such as identifiers,
keywords, and numbers.
Tool:
JFLAP.
Theory:
A lexical analyzer (lexer) converts source code into tokens, such as:
• Identifiers: [a − zA − Z][a − zA − Z0 − 9]∗
• Keywords: e.g., “if”, “while”
• Numbers: [0 − 9]+
Regular expressions define these patterns, implemented as finite automata.
Simulation:
1. Open JFLAP and select “Finite Automaton”.
2. Design DFAs for:
• Identifier: [a − zA − Z][a − zA − Z0 − 9]∗
• Keyword: “if” or “while”
• Number: [0 − 9]+
3. Combine DFAs into a single automaton using union.
4. Test strings such as “x1”, “if”, “123”, and “12a” using the “Input” feature.
5. Observe token recognition.
Observation:
• The combined DFA distinguishes between pattern types.
• Test Results:
– “x1”: Accepted (identifier).
– “if”: Accepted (keyword).
– “123”: Accepted (number).
– “12a”: Rejected (invalid number).
• JFLAP’s automaton visualizes token recognition effectively.
Conclusion:
The lexical analyzer was successfully developed in JFLAP, accurately recognizing identifiers,
keywords, and numbers. The combined DFA and JFLAP’s visualization effectively
demonstrated the lexical analysis process.
Experiment 14
Aim/Objective:
To implement a lexical analyzer using JFLAP to recognize tokens in “if” statements (e.g., “if
(condition) then”).
Tool:
JFLAP.
Theory:
A lexical analyzer for “if” statements identifies tokens such as:
• Keyword: “if”, “then”
• Symbols: “(”, “)”
• Condition: Simplified as identifiers ([a − zA − Z]+) The
lexer uses regular expressions and DFAs to tokenize input.
Simulation:
1. Open JFLAP and select “Finite Automaton”.
2. Design DFAs for:
• Keyword “if”: if
• Keyword “then”: then
• Symbols: “(”, “)”
• Condition: [a − zA − Z]+
3. Combine DFAs into a single automaton.
4. Test strings such as “if (x) then”, “if (abc) then”, and “if x then”.
5. Observe token sequence.
Observation:
• The DFA recognizes tokens in the correct order.
• Test Results:
– “if (x) then”: Accepted (tokens: “if”, “(”, “x”, “)”, “then”).
– “if (abc) then”: Accepted (valid tokens).
– “if x then”: Rejected (missing parentheses).
• JFLAP’s visualization confirms tokenization accuracy.
Conclusion:
The lexical analyzer for “if” statements was successfully implemented in JFLAP, correctly
tokenizing valid inputs and rejecting invalid ones. JFLAP’s DFA visualization confirmed the
accuracy of the lexical analysis process.
Experiment 15
Aim/Objective:
To implement an operator precedence parser for a given grammar using JFLAP.
Tool:
JFLAP.
Theory:
An operator precedence parser is a bottom-up parsing technique for operator grammars, using
precedence relations (<, =, >) between operators (e.g., +, ∗ , (, )). It parses expressions
like E → E + E | E ∗ E | (E) | id using a stack-based approach.
Simulation:
1. Open JFLAP and select “Grammar”.
2. Define a grammar for arithmetic expressions:
• Variables: E
• Terminals: +, ∗ , (, ), id
• Productions: E → E + E | E ∗ E | (E) | id
3. Define precedence: ∗ > +, parentheses have highest precedence.
4. Select “Operator Precedence Parse” and build the precedence table.
5. Test strings such as “id + id * id” and “(id + id) * id”.
6. Observe shift/reduce actions.
Observation:
• The precedence table enforces correct operator precedence.
• Test Results:
– “id + id * id”: Accepted (∗ evaluated before +).
– “(id + id) * id”: Accepted (parentheses override precedence).
– “id + + id”: Rejected (invalid syntax).
• JFLAP’s visualization shows clear shift/reduce operations.
Conclusion:
The operator precedence parser was successfully implemented in JFLAP, correctly parsing
arithmetic expressions while respecting operator precedence. JFLAP’s visualization of 14 the
precedence table and parsing actions demonstrated the effectiveness of bottom-up parsing.