AUTOMATA AND
COMPILER THEORIES
AND FORMAL
LANGUAGES
Prepared by: Maxil S. Urocay MSCS ongoing
Prepared by: Maxil S. Urocay MSCS ongoing
“Prayer to the Holy Spirit by St. Augustine”
Breathe in me, O Holy Spirit,
That my thoughts may all be holy.
Act in me, O Holy Spirit,
That my work, too may be holy.
Draw my heart, O Holy Spirit.
That I love but what is holy.
Strengthen me, O Holy Spirit,
To defend all that is holy.
CONTEXT-FREE GRAMMARS
Prepared by: Maxil S. Urocay MSCS ongoing
Prepared by: Maxil S. Urocay MSCS ongoing
A Context-Free
Grammar (CFG) is a type of
formal grammar used to
define the syntax of
programming languages
and natural languages.
WHAT IS A CONTEXT-FREE GRAMMAR
(CFG)?
Prepared by: Maxil S. Urocay MSCS ongoing
In CFGs, the rules
(called production rules)
are used to replace non-
terminal symbols with a
string of terminals and/or
non-terminals.
WHAT IS A CONTEXT-FREE GRAMMAR
(CFG)?
Prepared by: Maxil S. Urocay MSCS ongoing
This process helps to
generate strings that
belong to a certain
language. These strings
are structured and can be
parsed or understood in a
recursive manner.
WHAT IS A CONTEXT-FREE GRAMMAR
(CFG)?
Prepared by: Maxil S. Urocay MSCS ongoing
Non-Terminals (Variables)
V:
These are symbols
that can be replaced by
other symbols. They
represent concepts or
structures in the
language.
COMPONENT OF CFGs
Prepared by: Maxil S. Urocay MSCS ongoing
Terminals Σ:
These are the actual
content symbols of the
language, like words in
natural language or
characters in
programming.
COMPONENT OF CFGs
Prepared by: Maxil S. Urocay MSCS ongoing
Production Rules R:
These define how
non-terminals can be
replaced by a combination
of terminals and non-
terminals.
COMPONENT OF CFGs
Prepared by: Maxil S. Urocay MSCS ongoing
Start Symbol S:
This is the initial non-
terminal from which all
derivations begin. It
represents the entire
structure of the language.
COMPONENT OF CFGs
Prepared by: Maxil S. Urocay MSCS ongoing
Example Grammar (G):
S aSb | bSa | SS |
→ ε
S is the start symbol.
a and b are terminals.
S is also a non-terminal.
CONTEXT-FREE GRAMMAR
Prepared by: Maxil S. Urocay MSCS ongoing
Example Grammar (G):
S aSb | bSa | SS |
→ ε
Step 1:
Start with the start
symbol, S.
CONTEXT-FREE GRAMMAR
Prepared by: Maxil S. Urocay MSCS ongoing
Example Grammar (G):
S aSb | bSa | SS |
→ ε
Step 2:
Use production rules to
replace S with one of the
options. For example, S →
aSb. Now we have "aSb".
CONTEXT-FREE GRAMMAR
Prepared by: Maxil S. Urocay MSCS ongoing
Example Grammar (G):
S aSb | bSa | SS |
→ ε
Step 3:
Repeat the process for
the non-terminal S inside the
string. This will give us
something like "aabSb", and
so on.
CONTEXT-FREE GRAMMAR
Prepared by: Maxil S. Urocay MSCS ongoing
The production rules
define how to form larger
strings from smaller building
blocks (the terminals).
CONTEXT-FREE GRAMMAR
Prepared by: Maxil S. Urocay MSCS ongoing
In Compiler Design:
CFGs are used to
describe the syntax of
programming languages.
When writing a program, the
compiler uses a CFG to check
if the syntax is correct.
IMPORTANCE OF CFGS IN LANGUAGE
DESIGN
Prepared by: Maxil S. Urocay MSCS ongoing
In Compiler Design:
Example: A simple
programming language with
if-else statements can be
represented using CFGs.
IMPORTANCE OF CFGS IN LANGUAGE
DESIGN
Prepared by: Maxil S. Urocay MSCS ongoing
In Natural Language
Processing (NLP):
CFGs are used to model
the structure of sentences in
languages like English,
French, etc.
IMPORTANCE OF CFGS IN LANGUAGE
DESIGN
Prepared by: Maxil S. Urocay MSCS ongoing
In Natural Language
Processing (NLP):
Example: The sentence
"The cat sleeps" can be
broken down into a structure
where "The cat" is a noun
phrase (NP), and "sleeps" is a
verb phrase (VP).
IMPORTANCE OF CFGS IN LANGUAGE
DESIGN
Prepared by: Maxil S. Urocay MSCS ongoing
A Context-Free Grammar
(CFG) can be categorized into
two types based on whether it
produces a unique or multiple
ways to derive a string.
Unambiguous CFG and
Ambiguous CFG.
CONTEXT-FREE GRAMMARS
Prepared by: Maxil S. Urocay MSCS ongoing
Grammar for Arithmetic
Expressions:
E E + T | T
→
T T * F | F
→
F id | (E)
→
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Grammar for Arithmetic
Expressions:
E E + T | T
→
T T * F | F
→
F id | (E)
→
E (Expression) can be another
expression plus a term (E E +
→
T) or just a term (E T).
→
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Grammar for Arithmetic
Expressions:
E E + T | T
→
T T * F | F
→
F id | (E)
→
T (Term) can be another term
multiplied by a factor (T T * F)
→
or just a factor (T F).
→
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Grammar for Arithmetic Expressions:
E E + T | T
→
T T * F | F
→
F id | (E)
→
F (Factor) can be an identifier (a
number or variable, represented as id)
or a complete expression enclosed in
parentheses (F (E)).
→
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
What Does This Grammar Represent?
E E + T:
→
This rule says that an expression
can be the sum of another expression
and a term (like a + b).
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
What Does This Grammar Represent?
T T * F:
→
This rule says that a term can be
the product of another term and a
factor (like b * c).
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
What Does This Grammar Represent?
F id:
→
A factor can simply be an
identifier (like a variable or number,
such as a, b, 5).
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
What Does This Grammar Represent?
F (E):
→
A factor can also be an entire
expression wrapped in parentheses,
which means the operations inside the
parentheses are evaluated first.
CFG FOR ARITHMETIC EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Operators in Arithmetic:
Addition (+) and Multiplication (*)
are arithmetic operators.
ARITHMETIC OPERATORS AND
PRECEDENCE
Prepared by: Maxil S. Urocay MSCS ongoing
Operator Precedence:
In mathematics, multiplication has
higher precedence than addition. This
means that in expressions like a + b *
c, we first calculate b * c and then add
a.
ARITHMETIC OPERATORS AND
PRECEDENCE
Prepared by: Maxil S. Urocay MSCS ongoing
Operator Precedence:
The CFG captures this precedence
naturally, as multiplication (*) is
handled in the T T * F rule, whereas
→
addition (+) is handled in the E E + T
→
rule.
ARITHMETIC OPERATORS AND
PRECEDENCE
Prepared by: Maxil S. Urocay MSCS ongoing
Operator Precedence:
In the CFG for a + b * c, the
multiplication is handled before the
addition, just as in standard arithmetic.
Multiplication First: We expand T * F
first (b * c) before adding a to the
result.
ARITHMETIC OPERATORS AND
PRECEDENCE
Prepared by: Maxil S. Urocay MSCS ongoing
Left-to-Right Associativity:
Most arithmetic operators
(including + and *) are left-associative,
which means expressions are
evaluated from left to right.
For example, a + b + c is evaluated as (a
+ b) + c.
ASSOCIATIVITY IN ARITHMETIC
EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
How CFGs Work with Associativity:
Addition:
The CFG will apply the addition
rules left-to-right.
Multiplication:
Similarly, multiplication is
handled before addition because of
the order of rules.
ASSOCIATIVITY IN ARITHMETIC
EXPRESSIONS
Prepared by: Maxil S. Urocay MSCS ongoing
E E + T | T
→
T T * F | F
→
F id | (E)
→ String: a+b*c
Start with E:
Apply E E + T: The expression is
→
now E + T.
This is an addition expression, so we
now need to expand both parts of it.
CFG ARITHMETIC EXPRESSIONS
EXAMPLE
Prepared by: Maxil S. Urocay MSCS ongoing
E E + T | T
→
T T * F | F
→
F id | (E)
→ String: a+b*c
Expand E (the left side of the addition):
Apply E T: The left side is now a term.
→
Apply T F: The term is a factor.
→
Apply F id (left side terminal, a): We
→
get a.
CFG ARITHMETIC EXPRESSIONS
EXAMPLE
Prepared by: Maxil S. Urocay MSCS ongoing
E E + T | T
→
T T * F | F
→
F id | (E)
→ String: a+b*c
Expand T (the right side of the addition):
Apply T T * F: The right side is now a
→
term multiplied by a factor.
Apply T F: The term is a factor.
→
CFG ARITHMETIC EXPRESSIONS
EXAMPLE
Prepared by: Maxil S. Urocay MSCS ongoing
E E + T | T
→
T T * F | F
→
F id | (E)
→ String: a+b*c
Apply F id (right side terminal, b): We
→
get b.
Apply F id (for the second factor, c):
→
We get c.
CFG ARITHMETIC EXPRESSIONS
EXAMPLE
Prepared by: Maxil S. Urocay MSCS ongoing
An unambiguous CFG is
a grammar in which every
string derived from the start
symbol has only one valid
parse tree.
WHAT IS AN UNAMBIGUOUS CFG?
Prepared by: Maxil S. Urocay MSCS ongoing
This means that there is
no ambiguity in how a string
is derived, and it has a single
interpretation.
WHAT IS AN UNAMBIGUOUS CFG?
Prepared by: Maxil S. Urocay MSCS ongoing
An ambiguous CFG is a
grammar where some strings
have multiple valid parse
trees.
WHAT IS AN AMBIGUOUS CFG?
Prepared by: Maxil S. Urocay MSCS ongoing
This means the
grammar does not specify a
unique way to derive a string.
WHAT IS AN AMBIGUOUS CFG?
Prepared by: Maxil S. Urocay MSCS ongoing
This means the
grammar does not specify a
unique way to derive a string.
WHAT IS AN AMBIGUOUS CFG?
Prepared by: Maxil S. Urocay MSCS ongoing
Ambiguity creates
uncertainty about the
structure of a string, which
can lead to confusion in
parsing and interpretation.
WHY AMBIGUITY IS A PROBLEM
Prepared by: Maxil S. Urocay MSCS ongoing
In programming languages,
this could result in syntax errors or
unpredictable behavior.
In the expression a + b * c, if the
grammar is ambiguous, it’s unclear
whether the multiplication or
addition should be performed first.
WHY AMBIGUITY IS A PROBLEM
Prepared by: Maxil S. Urocay MSCS ongoing
Problem in Programming:
int result = a + b * c;
If the grammar is ambiguous,
the program could be interpreted
as:
a + (b * c) (multiplication first,
correct behavior in most
languages).
WHY AMBIGUITY IS A PROBLEM
Prepared by: Maxil S. Urocay MSCS ongoing
Problem in Programming:
int result = a + b * c;
If the grammar is ambiguous,
the program could be interpreted
as:
(a + b) * c (addition first, which
changes the meaning completely).
WHY AMBIGUITY IS A PROBLEM
Prepared by: Maxil S. Urocay MSCS ongoing
Ambiguity in CFGs must be
resolved in programming
languages to ensure that
operations are performed in the
correct order.
WHY AMBIGUITY IS A PROBLEM
Prepared by: Maxil S. Urocay MSCS ongoing
Disambiguating CFGs:
One solution to resolve
ambiguity is to introduce
precedence rules or associativity.
HOW TO SOLVE AMBIGUITY?
Prepared by: Maxil S. Urocay MSCS ongoing
Disambiguating CFGs:
Precedence: Operators like *
have higher precedence than +.
Associativity: Some operators (like
+ and *) are left-associative,
meaning operations are performed
from left to right.
HOW TO SOLVE AMBIGUITY?
Prepared by: Maxil S. Urocay MSCS ongoing
A derivation is the process of
applying production rules step-by-
step to derive a string from the
start symbol.
WHAT IS A DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
There are two methods for
choosing which non-terminal to
replace first during the derivation
process:
Leftmost and Rightmost
Derivations
WHAT IS A DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
In leftmost derivation, at each
step, you always replace the
leftmost non-terminal first.
This method expands the grammar
from left to right.
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
E E + T | T
→
T T * F | F
→
F id | (E)
→
String: a + b * c
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Start with the start symbol: E
Initial string: E
Apply the production rule E E + T:
→
We replace E with E + T.
Now, we have: E + T
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule E T
→
(since E is now the leftmost non-
terminal):
We replace the first E with T.
Now, we have: T + T
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule T F for
→
the first T (the leftmost non-
terminal):
We replace the first T with F.
Now, we have: F + T
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule F id for
→
the first F (the leftmost non-
terminal):
We replace F with id (which
represents a in this case).
Now, we have: id + T
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule T T * F
→
for the second T (the leftmost non-
terminal):
We replace T with T * F.
Now, we have: id + T * F
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule T F for
→
the T (the leftmost non-terminal):
We replace T with F.
Now, we have: id + F * F
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule F id for
→
both F (representing b and c):
We replace both F symbols with id
(representing b and c).
Now, we have: id + id * id
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
In leftmost derivation, we
always replace the leftmost non-
terminal first until the entire string
is derived.
WHAT IS LEFTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
In rightmost derivation, at
each step, you always replace the
rightmost non-terminal first.
This method expands the grammar
from right to left.
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
E E + T | T
→
T T * F | F
→
F id | (E)
→
String: a + b * c
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Start with the start symbol: E
Initial string: E
Apply the production rule E E + T:
→
We replace E with E + T.
Now, we have: E + T
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule T F for
→
the T (the rightmost non-terminal):
We replace T with F.
Now, we have: E + F
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule F T*F
→
for the F (the rightmost non-
terminal):
We replace F with T * F.
Now, we have: E + T * F
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule F id for
→
the F (the rightmost non-terminal):
We replace F with id (representing c
in this case).
Now, we have: E + T * id
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule T F for
→
the left T:
We replace T with F.
Now, we have: E + F * id
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule F id for
→
the left F:
We replace F with id (representing
b in this case).
Now, we have: E + id * id
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule E T for
→
the left E:
We replace E with T.
Now, we have: T + id * id
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule T F for
→
the left T:
We replace T with F.
Now, we have: F + id * id
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
Apply the production rule F id for
→
the left F:
We replace F with id (representing
a in this case).
Now, we have: id + id * id
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
In rightmost derivation, we
always replace the rightmost non-
terminal first until the entire string
is derived.
WHAT IS RIGHTMOST DERIVATION?
Prepared by: Maxil S. Urocay MSCS ongoing
CFGs are not just theoretical
concepts; they are widely used in
computational linguistics,
programming languages, and
parsing techniques.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Understanding the
applications of CFGs helps us see
how these grammars are used to
define the syntax of languages and
guide language processing.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Compilers:
CFGs are used in compilers to
define the syntax of programming
languages.
A compiler translates source code
written in a high-level language (like C,
Java, Python) into machine code.
During this process, CFGs are used to
ensure the correct syntax of the code.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Compilers:
Example: A simple arithmetic
expression in code, like a + b * c, needs
to be parsed according to operator
precedence rules (multiplication first,
then addition). The CFG will ensure
that the expression is correctly
understood.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Parsing Sentences:
CFGs are used in Natural
Language Processing (NLP) to model
sentence structures in human
languages.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Parsing Sentences:
Example: In NLP, CFGs are used to
parse sentences into noun phrases
(NP) and verb phrases (VP), which
helps in understanding the meaning of
the sentence.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Mathematical Expression Parsers:
CFGs are used to define the
syntax of mathematical expressions
and ensure that operators like +, -, *,
and / are applied in the correct order.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Mathematical Expression Parsers:
Example: For the expression a + b
* c, the CFG ensures that multiplication
happens before addition (due to
precedence rules).
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Configuration Files:
CFGs are used to define the syntax of
configuration files in software
applications, ensuring that the files are
structured correctly.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
Database Query Parsing:
In databases, SQL queries are parsed
using CFGs to ensure they are
syntactically correct before execution.
APPLICATIONS OF CONTEXT-FREE
GRAMMARS (CFGS)
Prepared by: Maxil S. Urocay MSCS ongoing
A parse tree is a tree
structure that visually
represents how a string can
be derived from a Context-
Free Grammar (CFG).
WHAT IS A PARSE TREE?
Prepared by: Maxil S. Urocay MSCS ongoing
The root of the tree is the
start symbol, and the leaves
are the terminals (actual
symbols in the string).
WHAT IS A PARSE TREE?
Prepared by: Maxil S. Urocay MSCS ongoing
The internal nodes
represent non-terminals that
are further replaced by other
non-terminals or terminals.
WHAT IS A PARSE TREE?
Prepared by: Maxil S. Urocay MSCS ongoing
Why is it important?
A parse tree helps us
understand the structure of
the string and how it is
derived.
WHAT IS A PARSE TREE?
Prepared by: Maxil S. Urocay MSCS ongoing
Why is it important?
It shows exactly how
production rules are applied
step-by-step.
WHAT IS A PARSE TREE?
Prepared by: Maxil S. Urocay MSCS ongoing
Start with the start symbol.
Begin with the root node
labeled with the start symbol.
Example:
If our CFG is S aSb |
→
bSa | SS | ε, the start symbol
is S.
HOW TO CREATE A PARSE TREE - THE
BASIC STEPS
Prepared by: Maxil S. Urocay MSCS ongoing
Use the production rules
To expand the start
symbol and non-terminal
symbols.
Production rules are used to
replace non-terminals with
combinations of non-
terminals and terminals.
HOW TO CREATE A PARSE TREE - THE
BASIC STEPS
Prepared by: Maxil S. Urocay MSCS ongoing
Expand Non-Terminals
Continue expanding the
non-terminals until only
terminal symbols are left.
Terminal symbols are the
symbols that appear in the
string and cannot be replaced
further.
HOW TO CREATE A PARSE TREE - THE
BASIC STEPS
Prepared by: Maxil S. Urocay MSCS ongoing
Terminate with Terminals:
Stop expanding once all
symbols in the tree are
terminals.
The leaves of the tree are the
actual symbols from the
string.
HOW TO CREATE A PARSE TREE - THE
BASIC STEPS
Prepared by: Maxil S. Urocay MSCS ongoing
Example:
Let’s take the string abab
and the following CFG:
S aSb | bSa | SS |
→ ε
PARSE TREE EXAMPLE
Prepared by: Maxil S. Urocay MSCS ongoing
A parse tree shows the
derivation order, which non-
terminal is replaced first, and
how the structure develops
step by step.
DERIVATION ORDER IN PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
Leftmost Derivation (LMD):
Always replace the leftmost
non-terminal first.
Example: For S aSb |
→ ε:
S aSb ab
→ →
DERIVATION ORDER IN PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
Rightmost Derivation (RMD):
Always replace the rightmost
non-terminal first.
Example: For S aSb |
→ ε:
S aSb ab
→ →
DERIVATION ORDER IN PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
Syntax Checking:
Parse trees are essential
in compilers for checking
whether a string (like a
program) follows the correct
syntax.
IMPORTANCE OF PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
Language Understanding:
In natural language
processing (NLP), parse trees
help break down sentences
into their grammatical
components (e.g., noun
phrase, verb phrase).
IMPORTANCE OF PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
Visualizing the Structure:
Parse trees allow us to
visually understand how a
complex string is constructed
from smaller building blocks.
IMPORTANCE OF PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
What is Ambiguity?:
A grammar is ambiguous
if there are multiple parse
trees for the same string.
AMBIGUITY IN PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
Why is Ambiguity a Problem?:
Ambiguity can cause
confusion in parsing and
interpreting strings. In
programming, this could lead
to syntax errors or incorrect
behavior in compilers.
AMBIGUITY IN PARSE TREES
Prepared by: Maxil S. Urocay MSCS ongoing
Syntax Trees for Compilers:
Parse Trees (also called
syntax trees) are used by
compilers to check the
structure of the program as it
is parsed.
HOW PARSE TREES ARE USED IN
APPLICATIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Syntax Trees for Compilers:
A syntax tree allows the
compiler to understand how
the language rules are
applied to the source code,
ensuring correctness.
HOW PARSE TREES ARE USED IN
APPLICATIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Visualizing Expressions in
Compilers:
Parse trees are also used
in intermediate
representations of code,
which are then used for
optimization or machine code
generation.
HOW PARSE TREES ARE USED IN
APPLICATIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Language Models in NLP:
In NLP, parse trees help
computers understand
human language by breaking
sentences down into
constituent parts (subject,
verb, object).
HOW PARSE TREES ARE USED IN
APPLICATIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Language Models in NLP:
Example: In speech
recognition systems, parse
trees are used to break down
spoken sentences into a form
that the computer can
process.
HOW PARSE TREES ARE USED IN
APPLICATIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Machine Translation:
Parse trees are essential
in machine translation (e.g.,
translating from English to
French), as they break down
the structure of a sentence in
one language, then help
rebuild it in the target
language.
HOW PARSE TREES ARE USED IN
APPLICATIONS
Prepared by: Maxil S. Urocay MSCS ongoing
Top-Down Parsing:
Parse trees are used in
top-down parsing (a type of
predictive parsing) to check if
the syntax of a string matches
the grammar.
APPLICATIONS OF PARSE TREES IN
PARSING ALGORITHMS
Prepared by: Maxil S. Urocay MSCS ongoing
Top-Down Parsing:
The parser begins with
the start symbol and applies
rules recursively to break
down the string.
APPLICATIONS OF PARSE TREES IN
PARSING ALGORITHMS
Prepared by: Maxil S. Urocay MSCS ongoing
Bottom-Up Parsing:
Parse trees are also used in
bottom-up parsing to construct
the syntax tree from the leaves
up to the root. This type of
parsing builds the tree by
combining smaller parts of the
input into larger units, following
grammar rules.
APPLICATIONS OF PARSE TREES IN
PARSING ALGORITHMS
Prepared by: Maxil S. Urocay MSCS ongoing
Syntax Checking Example
Example: In a C program,
the statement int result = a + b *
c; is checked for correct syntax.
The CFG for arithmetic
expressions is used to check
whether the expression adheres
to the rules of precedence and
associativity.
APPLICATIONS OF PARSE TREES IN
PARSING ALGORITHMS
A well-formed language begins with a
solid grammar, and its structure is
revealed through parse trees, guiding
us toward clarity in both computation
and communication.
Prepared by: Maxil S. Urocay MSCS ongoing
THANK
YOU
Prepared by: Maxil S. Urocay MSCS ongoing
SHORT QUIZ
Prepared by: Maxil S. Urocay MSCS ongoing

Context-Free Grammars, and Parsing Introduction (2025 Edition) Automata

  • 1.
    AUTOMATA AND COMPILER THEORIES ANDFORMAL LANGUAGES Prepared by: Maxil S. Urocay MSCS ongoing
  • 2.
    Prepared by: MaxilS. Urocay MSCS ongoing “Prayer to the Holy Spirit by St. Augustine” Breathe in me, O Holy Spirit, That my thoughts may all be holy. Act in me, O Holy Spirit, That my work, too may be holy. Draw my heart, O Holy Spirit. That I love but what is holy. Strengthen me, O Holy Spirit, To defend all that is holy.
  • 3.
    CONTEXT-FREE GRAMMARS Prepared by:Maxil S. Urocay MSCS ongoing
  • 4.
    Prepared by: MaxilS. Urocay MSCS ongoing A Context-Free Grammar (CFG) is a type of formal grammar used to define the syntax of programming languages and natural languages. WHAT IS A CONTEXT-FREE GRAMMAR (CFG)?
  • 5.
    Prepared by: MaxilS. Urocay MSCS ongoing In CFGs, the rules (called production rules) are used to replace non- terminal symbols with a string of terminals and/or non-terminals. WHAT IS A CONTEXT-FREE GRAMMAR (CFG)?
  • 6.
    Prepared by: MaxilS. Urocay MSCS ongoing This process helps to generate strings that belong to a certain language. These strings are structured and can be parsed or understood in a recursive manner. WHAT IS A CONTEXT-FREE GRAMMAR (CFG)?
  • 7.
    Prepared by: MaxilS. Urocay MSCS ongoing Non-Terminals (Variables) V: These are symbols that can be replaced by other symbols. They represent concepts or structures in the language. COMPONENT OF CFGs
  • 8.
    Prepared by: MaxilS. Urocay MSCS ongoing Terminals Σ: These are the actual content symbols of the language, like words in natural language or characters in programming. COMPONENT OF CFGs
  • 9.
    Prepared by: MaxilS. Urocay MSCS ongoing Production Rules R: These define how non-terminals can be replaced by a combination of terminals and non- terminals. COMPONENT OF CFGs
  • 10.
    Prepared by: MaxilS. Urocay MSCS ongoing Start Symbol S: This is the initial non- terminal from which all derivations begin. It represents the entire structure of the language. COMPONENT OF CFGs
  • 11.
    Prepared by: MaxilS. Urocay MSCS ongoing Example Grammar (G): S aSb | bSa | SS | → ε S is the start symbol. a and b are terminals. S is also a non-terminal. CONTEXT-FREE GRAMMAR
  • 12.
    Prepared by: MaxilS. Urocay MSCS ongoing Example Grammar (G): S aSb | bSa | SS | → ε Step 1: Start with the start symbol, S. CONTEXT-FREE GRAMMAR
  • 13.
    Prepared by: MaxilS. Urocay MSCS ongoing Example Grammar (G): S aSb | bSa | SS | → ε Step 2: Use production rules to replace S with one of the options. For example, S → aSb. Now we have "aSb". CONTEXT-FREE GRAMMAR
  • 14.
    Prepared by: MaxilS. Urocay MSCS ongoing Example Grammar (G): S aSb | bSa | SS | → ε Step 3: Repeat the process for the non-terminal S inside the string. This will give us something like "aabSb", and so on. CONTEXT-FREE GRAMMAR
  • 15.
    Prepared by: MaxilS. Urocay MSCS ongoing The production rules define how to form larger strings from smaller building blocks (the terminals). CONTEXT-FREE GRAMMAR
  • 16.
    Prepared by: MaxilS. Urocay MSCS ongoing In Compiler Design: CFGs are used to describe the syntax of programming languages. When writing a program, the compiler uses a CFG to check if the syntax is correct. IMPORTANCE OF CFGS IN LANGUAGE DESIGN
  • 17.
    Prepared by: MaxilS. Urocay MSCS ongoing In Compiler Design: Example: A simple programming language with if-else statements can be represented using CFGs. IMPORTANCE OF CFGS IN LANGUAGE DESIGN
  • 18.
    Prepared by: MaxilS. Urocay MSCS ongoing In Natural Language Processing (NLP): CFGs are used to model the structure of sentences in languages like English, French, etc. IMPORTANCE OF CFGS IN LANGUAGE DESIGN
  • 19.
    Prepared by: MaxilS. Urocay MSCS ongoing In Natural Language Processing (NLP): Example: The sentence "The cat sleeps" can be broken down into a structure where "The cat" is a noun phrase (NP), and "sleeps" is a verb phrase (VP). IMPORTANCE OF CFGS IN LANGUAGE DESIGN
  • 20.
    Prepared by: MaxilS. Urocay MSCS ongoing A Context-Free Grammar (CFG) can be categorized into two types based on whether it produces a unique or multiple ways to derive a string. Unambiguous CFG and Ambiguous CFG. CONTEXT-FREE GRAMMARS
  • 21.
    Prepared by: MaxilS. Urocay MSCS ongoing Grammar for Arithmetic Expressions: E E + T | T → T T * F | F → F id | (E) → CFG FOR ARITHMETIC EXPRESSIONS
  • 22.
    Prepared by: MaxilS. Urocay MSCS ongoing Grammar for Arithmetic Expressions: E E + T | T → T T * F | F → F id | (E) → E (Expression) can be another expression plus a term (E E + → T) or just a term (E T). → CFG FOR ARITHMETIC EXPRESSIONS
  • 23.
    Prepared by: MaxilS. Urocay MSCS ongoing Grammar for Arithmetic Expressions: E E + T | T → T T * F | F → F id | (E) → T (Term) can be another term multiplied by a factor (T T * F) → or just a factor (T F). → CFG FOR ARITHMETIC EXPRESSIONS
  • 24.
    Prepared by: MaxilS. Urocay MSCS ongoing Grammar for Arithmetic Expressions: E E + T | T → T T * F | F → F id | (E) → F (Factor) can be an identifier (a number or variable, represented as id) or a complete expression enclosed in parentheses (F (E)). → CFG FOR ARITHMETIC EXPRESSIONS
  • 25.
    Prepared by: MaxilS. Urocay MSCS ongoing What Does This Grammar Represent? E E + T: → This rule says that an expression can be the sum of another expression and a term (like a + b). CFG FOR ARITHMETIC EXPRESSIONS
  • 26.
    Prepared by: MaxilS. Urocay MSCS ongoing What Does This Grammar Represent? T T * F: → This rule says that a term can be the product of another term and a factor (like b * c). CFG FOR ARITHMETIC EXPRESSIONS
  • 27.
    Prepared by: MaxilS. Urocay MSCS ongoing What Does This Grammar Represent? F id: → A factor can simply be an identifier (like a variable or number, such as a, b, 5). CFG FOR ARITHMETIC EXPRESSIONS
  • 28.
    Prepared by: MaxilS. Urocay MSCS ongoing What Does This Grammar Represent? F (E): → A factor can also be an entire expression wrapped in parentheses, which means the operations inside the parentheses are evaluated first. CFG FOR ARITHMETIC EXPRESSIONS
  • 29.
    Prepared by: MaxilS. Urocay MSCS ongoing Operators in Arithmetic: Addition (+) and Multiplication (*) are arithmetic operators. ARITHMETIC OPERATORS AND PRECEDENCE
  • 30.
    Prepared by: MaxilS. Urocay MSCS ongoing Operator Precedence: In mathematics, multiplication has higher precedence than addition. This means that in expressions like a + b * c, we first calculate b * c and then add a. ARITHMETIC OPERATORS AND PRECEDENCE
  • 31.
    Prepared by: MaxilS. Urocay MSCS ongoing Operator Precedence: The CFG captures this precedence naturally, as multiplication (*) is handled in the T T * F rule, whereas → addition (+) is handled in the E E + T → rule. ARITHMETIC OPERATORS AND PRECEDENCE
  • 32.
    Prepared by: MaxilS. Urocay MSCS ongoing Operator Precedence: In the CFG for a + b * c, the multiplication is handled before the addition, just as in standard arithmetic. Multiplication First: We expand T * F first (b * c) before adding a to the result. ARITHMETIC OPERATORS AND PRECEDENCE
  • 33.
    Prepared by: MaxilS. Urocay MSCS ongoing Left-to-Right Associativity: Most arithmetic operators (including + and *) are left-associative, which means expressions are evaluated from left to right. For example, a + b + c is evaluated as (a + b) + c. ASSOCIATIVITY IN ARITHMETIC EXPRESSIONS
  • 34.
    Prepared by: MaxilS. Urocay MSCS ongoing How CFGs Work with Associativity: Addition: The CFG will apply the addition rules left-to-right. Multiplication: Similarly, multiplication is handled before addition because of the order of rules. ASSOCIATIVITY IN ARITHMETIC EXPRESSIONS
  • 35.
    Prepared by: MaxilS. Urocay MSCS ongoing E E + T | T → T T * F | F → F id | (E) → String: a+b*c Start with E: Apply E E + T: The expression is → now E + T. This is an addition expression, so we now need to expand both parts of it. CFG ARITHMETIC EXPRESSIONS EXAMPLE
  • 36.
    Prepared by: MaxilS. Urocay MSCS ongoing E E + T | T → T T * F | F → F id | (E) → String: a+b*c Expand E (the left side of the addition): Apply E T: The left side is now a term. → Apply T F: The term is a factor. → Apply F id (left side terminal, a): We → get a. CFG ARITHMETIC EXPRESSIONS EXAMPLE
  • 37.
    Prepared by: MaxilS. Urocay MSCS ongoing E E + T | T → T T * F | F → F id | (E) → String: a+b*c Expand T (the right side of the addition): Apply T T * F: The right side is now a → term multiplied by a factor. Apply T F: The term is a factor. → CFG ARITHMETIC EXPRESSIONS EXAMPLE
  • 38.
    Prepared by: MaxilS. Urocay MSCS ongoing E E + T | T → T T * F | F → F id | (E) → String: a+b*c Apply F id (right side terminal, b): We → get b. Apply F id (for the second factor, c): → We get c. CFG ARITHMETIC EXPRESSIONS EXAMPLE
  • 39.
    Prepared by: MaxilS. Urocay MSCS ongoing An unambiguous CFG is a grammar in which every string derived from the start symbol has only one valid parse tree. WHAT IS AN UNAMBIGUOUS CFG?
  • 40.
    Prepared by: MaxilS. Urocay MSCS ongoing This means that there is no ambiguity in how a string is derived, and it has a single interpretation. WHAT IS AN UNAMBIGUOUS CFG?
  • 41.
    Prepared by: MaxilS. Urocay MSCS ongoing An ambiguous CFG is a grammar where some strings have multiple valid parse trees. WHAT IS AN AMBIGUOUS CFG?
  • 42.
    Prepared by: MaxilS. Urocay MSCS ongoing This means the grammar does not specify a unique way to derive a string. WHAT IS AN AMBIGUOUS CFG?
  • 43.
    Prepared by: MaxilS. Urocay MSCS ongoing This means the grammar does not specify a unique way to derive a string. WHAT IS AN AMBIGUOUS CFG?
  • 44.
    Prepared by: MaxilS. Urocay MSCS ongoing Ambiguity creates uncertainty about the structure of a string, which can lead to confusion in parsing and interpretation. WHY AMBIGUITY IS A PROBLEM
  • 45.
    Prepared by: MaxilS. Urocay MSCS ongoing In programming languages, this could result in syntax errors or unpredictable behavior. In the expression a + b * c, if the grammar is ambiguous, it’s unclear whether the multiplication or addition should be performed first. WHY AMBIGUITY IS A PROBLEM
  • 46.
    Prepared by: MaxilS. Urocay MSCS ongoing Problem in Programming: int result = a + b * c; If the grammar is ambiguous, the program could be interpreted as: a + (b * c) (multiplication first, correct behavior in most languages). WHY AMBIGUITY IS A PROBLEM
  • 47.
    Prepared by: MaxilS. Urocay MSCS ongoing Problem in Programming: int result = a + b * c; If the grammar is ambiguous, the program could be interpreted as: (a + b) * c (addition first, which changes the meaning completely). WHY AMBIGUITY IS A PROBLEM
  • 48.
    Prepared by: MaxilS. Urocay MSCS ongoing Ambiguity in CFGs must be resolved in programming languages to ensure that operations are performed in the correct order. WHY AMBIGUITY IS A PROBLEM
  • 49.
    Prepared by: MaxilS. Urocay MSCS ongoing Disambiguating CFGs: One solution to resolve ambiguity is to introduce precedence rules or associativity. HOW TO SOLVE AMBIGUITY?
  • 50.
    Prepared by: MaxilS. Urocay MSCS ongoing Disambiguating CFGs: Precedence: Operators like * have higher precedence than +. Associativity: Some operators (like + and *) are left-associative, meaning operations are performed from left to right. HOW TO SOLVE AMBIGUITY?
  • 51.
    Prepared by: MaxilS. Urocay MSCS ongoing A derivation is the process of applying production rules step-by- step to derive a string from the start symbol. WHAT IS A DERIVATION?
  • 52.
    Prepared by: MaxilS. Urocay MSCS ongoing There are two methods for choosing which non-terminal to replace first during the derivation process: Leftmost and Rightmost Derivations WHAT IS A DERIVATION?
  • 53.
    Prepared by: MaxilS. Urocay MSCS ongoing In leftmost derivation, at each step, you always replace the leftmost non-terminal first. This method expands the grammar from left to right. WHAT IS LEFTMOST DERIVATION?
  • 54.
    Prepared by: MaxilS. Urocay MSCS ongoing E E + T | T → T T * F | F → F id | (E) → String: a + b * c WHAT IS LEFTMOST DERIVATION?
  • 55.
    Prepared by: MaxilS. Urocay MSCS ongoing Start with the start symbol: E Initial string: E Apply the production rule E E + T: → We replace E with E + T. Now, we have: E + T WHAT IS LEFTMOST DERIVATION?
  • 56.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule E T → (since E is now the leftmost non- terminal): We replace the first E with T. Now, we have: T + T WHAT IS LEFTMOST DERIVATION?
  • 57.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule T F for → the first T (the leftmost non- terminal): We replace the first T with F. Now, we have: F + T WHAT IS LEFTMOST DERIVATION?
  • 58.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule F id for → the first F (the leftmost non- terminal): We replace F with id (which represents a in this case). Now, we have: id + T WHAT IS LEFTMOST DERIVATION?
  • 59.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule T T * F → for the second T (the leftmost non- terminal): We replace T with T * F. Now, we have: id + T * F WHAT IS LEFTMOST DERIVATION?
  • 60.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule T F for → the T (the leftmost non-terminal): We replace T with F. Now, we have: id + F * F WHAT IS LEFTMOST DERIVATION?
  • 61.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule F id for → both F (representing b and c): We replace both F symbols with id (representing b and c). Now, we have: id + id * id WHAT IS LEFTMOST DERIVATION?
  • 62.
    Prepared by: MaxilS. Urocay MSCS ongoing In leftmost derivation, we always replace the leftmost non- terminal first until the entire string is derived. WHAT IS LEFTMOST DERIVATION?
  • 63.
    Prepared by: MaxilS. Urocay MSCS ongoing In rightmost derivation, at each step, you always replace the rightmost non-terminal first. This method expands the grammar from right to left. WHAT IS RIGHTMOST DERIVATION?
  • 64.
    Prepared by: MaxilS. Urocay MSCS ongoing E E + T | T → T T * F | F → F id | (E) → String: a + b * c WHAT IS RIGHTMOST DERIVATION?
  • 65.
    Prepared by: MaxilS. Urocay MSCS ongoing Start with the start symbol: E Initial string: E Apply the production rule E E + T: → We replace E with E + T. Now, we have: E + T WHAT IS RIGHTMOST DERIVATION?
  • 66.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule T F for → the T (the rightmost non-terminal): We replace T with F. Now, we have: E + F WHAT IS RIGHTMOST DERIVATION?
  • 67.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule F T*F → for the F (the rightmost non- terminal): We replace F with T * F. Now, we have: E + T * F WHAT IS RIGHTMOST DERIVATION?
  • 68.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule F id for → the F (the rightmost non-terminal): We replace F with id (representing c in this case). Now, we have: E + T * id WHAT IS RIGHTMOST DERIVATION?
  • 69.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule T F for → the left T: We replace T with F. Now, we have: E + F * id WHAT IS RIGHTMOST DERIVATION?
  • 70.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule F id for → the left F: We replace F with id (representing b in this case). Now, we have: E + id * id WHAT IS RIGHTMOST DERIVATION?
  • 71.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule E T for → the left E: We replace E with T. Now, we have: T + id * id WHAT IS RIGHTMOST DERIVATION?
  • 72.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule T F for → the left T: We replace T with F. Now, we have: F + id * id WHAT IS RIGHTMOST DERIVATION?
  • 73.
    Prepared by: MaxilS. Urocay MSCS ongoing Apply the production rule F id for → the left F: We replace F with id (representing a in this case). Now, we have: id + id * id WHAT IS RIGHTMOST DERIVATION?
  • 74.
    Prepared by: MaxilS. Urocay MSCS ongoing In rightmost derivation, we always replace the rightmost non- terminal first until the entire string is derived. WHAT IS RIGHTMOST DERIVATION?
  • 75.
    Prepared by: MaxilS. Urocay MSCS ongoing CFGs are not just theoretical concepts; they are widely used in computational linguistics, programming languages, and parsing techniques. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 76.
    Prepared by: MaxilS. Urocay MSCS ongoing Understanding the applications of CFGs helps us see how these grammars are used to define the syntax of languages and guide language processing. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 77.
    Prepared by: MaxilS. Urocay MSCS ongoing Compilers: CFGs are used in compilers to define the syntax of programming languages. A compiler translates source code written in a high-level language (like C, Java, Python) into machine code. During this process, CFGs are used to ensure the correct syntax of the code. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 78.
    Prepared by: MaxilS. Urocay MSCS ongoing Compilers: Example: A simple arithmetic expression in code, like a + b * c, needs to be parsed according to operator precedence rules (multiplication first, then addition). The CFG will ensure that the expression is correctly understood. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 79.
    Prepared by: MaxilS. Urocay MSCS ongoing Parsing Sentences: CFGs are used in Natural Language Processing (NLP) to model sentence structures in human languages. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 80.
    Prepared by: MaxilS. Urocay MSCS ongoing Parsing Sentences: Example: In NLP, CFGs are used to parse sentences into noun phrases (NP) and verb phrases (VP), which helps in understanding the meaning of the sentence. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 81.
    Prepared by: MaxilS. Urocay MSCS ongoing Mathematical Expression Parsers: CFGs are used to define the syntax of mathematical expressions and ensure that operators like +, -, *, and / are applied in the correct order. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 82.
    Prepared by: MaxilS. Urocay MSCS ongoing Mathematical Expression Parsers: Example: For the expression a + b * c, the CFG ensures that multiplication happens before addition (due to precedence rules). APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 83.
    Prepared by: MaxilS. Urocay MSCS ongoing Configuration Files: CFGs are used to define the syntax of configuration files in software applications, ensuring that the files are structured correctly. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 84.
    Prepared by: MaxilS. Urocay MSCS ongoing Database Query Parsing: In databases, SQL queries are parsed using CFGs to ensure they are syntactically correct before execution. APPLICATIONS OF CONTEXT-FREE GRAMMARS (CFGS)
  • 85.
    Prepared by: MaxilS. Urocay MSCS ongoing A parse tree is a tree structure that visually represents how a string can be derived from a Context- Free Grammar (CFG). WHAT IS A PARSE TREE?
  • 86.
    Prepared by: MaxilS. Urocay MSCS ongoing The root of the tree is the start symbol, and the leaves are the terminals (actual symbols in the string). WHAT IS A PARSE TREE?
  • 87.
    Prepared by: MaxilS. Urocay MSCS ongoing The internal nodes represent non-terminals that are further replaced by other non-terminals or terminals. WHAT IS A PARSE TREE?
  • 88.
    Prepared by: MaxilS. Urocay MSCS ongoing Why is it important? A parse tree helps us understand the structure of the string and how it is derived. WHAT IS A PARSE TREE?
  • 89.
    Prepared by: MaxilS. Urocay MSCS ongoing Why is it important? It shows exactly how production rules are applied step-by-step. WHAT IS A PARSE TREE?
  • 90.
    Prepared by: MaxilS. Urocay MSCS ongoing Start with the start symbol. Begin with the root node labeled with the start symbol. Example: If our CFG is S aSb | → bSa | SS | ε, the start symbol is S. HOW TO CREATE A PARSE TREE - THE BASIC STEPS
  • 91.
    Prepared by: MaxilS. Urocay MSCS ongoing Use the production rules To expand the start symbol and non-terminal symbols. Production rules are used to replace non-terminals with combinations of non- terminals and terminals. HOW TO CREATE A PARSE TREE - THE BASIC STEPS
  • 92.
    Prepared by: MaxilS. Urocay MSCS ongoing Expand Non-Terminals Continue expanding the non-terminals until only terminal symbols are left. Terminal symbols are the symbols that appear in the string and cannot be replaced further. HOW TO CREATE A PARSE TREE - THE BASIC STEPS
  • 93.
    Prepared by: MaxilS. Urocay MSCS ongoing Terminate with Terminals: Stop expanding once all symbols in the tree are terminals. The leaves of the tree are the actual symbols from the string. HOW TO CREATE A PARSE TREE - THE BASIC STEPS
  • 94.
    Prepared by: MaxilS. Urocay MSCS ongoing Example: Let’s take the string abab and the following CFG: S aSb | bSa | SS | → ε PARSE TREE EXAMPLE
  • 95.
    Prepared by: MaxilS. Urocay MSCS ongoing A parse tree shows the derivation order, which non- terminal is replaced first, and how the structure develops step by step. DERIVATION ORDER IN PARSE TREES
  • 96.
    Prepared by: MaxilS. Urocay MSCS ongoing Leftmost Derivation (LMD): Always replace the leftmost non-terminal first. Example: For S aSb | → ε: S aSb ab → → DERIVATION ORDER IN PARSE TREES
  • 97.
    Prepared by: MaxilS. Urocay MSCS ongoing Rightmost Derivation (RMD): Always replace the rightmost non-terminal first. Example: For S aSb | → ε: S aSb ab → → DERIVATION ORDER IN PARSE TREES
  • 98.
    Prepared by: MaxilS. Urocay MSCS ongoing Syntax Checking: Parse trees are essential in compilers for checking whether a string (like a program) follows the correct syntax. IMPORTANCE OF PARSE TREES
  • 99.
    Prepared by: MaxilS. Urocay MSCS ongoing Language Understanding: In natural language processing (NLP), parse trees help break down sentences into their grammatical components (e.g., noun phrase, verb phrase). IMPORTANCE OF PARSE TREES
  • 100.
    Prepared by: MaxilS. Urocay MSCS ongoing Visualizing the Structure: Parse trees allow us to visually understand how a complex string is constructed from smaller building blocks. IMPORTANCE OF PARSE TREES
  • 101.
    Prepared by: MaxilS. Urocay MSCS ongoing What is Ambiguity?: A grammar is ambiguous if there are multiple parse trees for the same string. AMBIGUITY IN PARSE TREES
  • 102.
    Prepared by: MaxilS. Urocay MSCS ongoing Why is Ambiguity a Problem?: Ambiguity can cause confusion in parsing and interpreting strings. In programming, this could lead to syntax errors or incorrect behavior in compilers. AMBIGUITY IN PARSE TREES
  • 103.
    Prepared by: MaxilS. Urocay MSCS ongoing Syntax Trees for Compilers: Parse Trees (also called syntax trees) are used by compilers to check the structure of the program as it is parsed. HOW PARSE TREES ARE USED IN APPLICATIONS
  • 104.
    Prepared by: MaxilS. Urocay MSCS ongoing Syntax Trees for Compilers: A syntax tree allows the compiler to understand how the language rules are applied to the source code, ensuring correctness. HOW PARSE TREES ARE USED IN APPLICATIONS
  • 105.
    Prepared by: MaxilS. Urocay MSCS ongoing Visualizing Expressions in Compilers: Parse trees are also used in intermediate representations of code, which are then used for optimization or machine code generation. HOW PARSE TREES ARE USED IN APPLICATIONS
  • 106.
    Prepared by: MaxilS. Urocay MSCS ongoing Language Models in NLP: In NLP, parse trees help computers understand human language by breaking sentences down into constituent parts (subject, verb, object). HOW PARSE TREES ARE USED IN APPLICATIONS
  • 107.
    Prepared by: MaxilS. Urocay MSCS ongoing Language Models in NLP: Example: In speech recognition systems, parse trees are used to break down spoken sentences into a form that the computer can process. HOW PARSE TREES ARE USED IN APPLICATIONS
  • 108.
    Prepared by: MaxilS. Urocay MSCS ongoing Machine Translation: Parse trees are essential in machine translation (e.g., translating from English to French), as they break down the structure of a sentence in one language, then help rebuild it in the target language. HOW PARSE TREES ARE USED IN APPLICATIONS
  • 109.
    Prepared by: MaxilS. Urocay MSCS ongoing Top-Down Parsing: Parse trees are used in top-down parsing (a type of predictive parsing) to check if the syntax of a string matches the grammar. APPLICATIONS OF PARSE TREES IN PARSING ALGORITHMS
  • 110.
    Prepared by: MaxilS. Urocay MSCS ongoing Top-Down Parsing: The parser begins with the start symbol and applies rules recursively to break down the string. APPLICATIONS OF PARSE TREES IN PARSING ALGORITHMS
  • 111.
    Prepared by: MaxilS. Urocay MSCS ongoing Bottom-Up Parsing: Parse trees are also used in bottom-up parsing to construct the syntax tree from the leaves up to the root. This type of parsing builds the tree by combining smaller parts of the input into larger units, following grammar rules. APPLICATIONS OF PARSE TREES IN PARSING ALGORITHMS
  • 112.
    Prepared by: MaxilS. Urocay MSCS ongoing Syntax Checking Example Example: In a C program, the statement int result = a + b * c; is checked for correct syntax. The CFG for arithmetic expressions is used to check whether the expression adheres to the rules of precedence and associativity. APPLICATIONS OF PARSE TREES IN PARSING ALGORITHMS
  • 113.
    A well-formed languagebegins with a solid grammar, and its structure is revealed through parse trees, guiding us toward clarity in both computation and communication. Prepared by: Maxil S. Urocay MSCS ongoing
  • 114.
    THANK YOU Prepared by: MaxilS. Urocay MSCS ongoing
  • 115.
    SHORT QUIZ Prepared by:Maxil S. Urocay MSCS ongoing

Editor's Notes

  • #4 "A Context-Free Grammar (CFG) is a type of formal grammar used to define the syntax of programming languages and natural languages."
  • #22 E → E + T: This rule says that an expression can be the sum of another expression and a term (like a + b).
  • #37 Apply F → id (right side terminal, b): We get b. Apply F → id (for the second factor, c): We get c.
  • #40 This grammar is unambiguous because there is only one valid parse tree for any expression derived from it.
  • #41 This grammar is unambiguous because there is only one valid parse tree for any expression derived from it.
  • #42 This grammar is unambiguous because there is only one valid parse tree for any expression derived from it.
  • #43 E → E + E | E * E | id This CFG allows addition and multiplication of expressions, but it does not specify which operation should be done first when both + and * appear. For the string a + b * c, the grammar can generate two different parse trees.
  • #45 Ambiguity makes it difficult to determine the correct meaning or structure of a string, which is problematic in compiler design and natural language processing.
  • #46  (a + b) * c (addition first, which changes the meaning completely).
  • #77 Applications in Programming Languages
  • #78 Applications in Programming Languages
  • #79 Applications in Natural Language Processing (NLP)
  • #80 Applications in Natural Language Processing (NLP)
  • #81 Applications in Mathematical Expression Parsing
  • #82 Applications in Mathematical Expression Parsing
  • #83 Applications in Mathematical Expression Parsing
  • #84 Applications in Mathematical Expression Parsing
  • #90 The start symbol is the root of the parse tree.
  • #91 Use the production rules of the CFG to expand the start symbol.
  • #92 Keep expanding the non-terminal symbols into other non-terminals or terminals until you reach terminal symbols.
  • #101 This is a case of ambiguity in the grammar. If a string can be derived in more than one way, it means the grammar is ambiguous, which can cause confusion in understanding the language.
  • #103 Role of Parse Trees in Applications
  • #104 Role of Parse Trees in Applications
  • #105 Role of Parse Trees in Applications
  • #106 Role in Natural Language Understanding
  • #107 Role in Natural Language Understanding
  • #108 Role in Natural Language Understanding
  • #109 Role in Natural Language Understanding
  • #110 Role in Natural Language Understanding
  • #111 Role in Natural Language Understanding
  • #112 Real-World Example: Syntax Checking in Programming
  • #113 This quote emphasizes that a solid grammar is the foundation of any well-formed language, whether in programming or natural communication. Context-Free Grammars (CFGs) provide the rules that structure the language, while parse trees visually represent how strings are derived from these rules. By breaking down a sentence or expression, parse trees offer clarity in understanding how the components fit together. In both computation and communication, this structured approach ensures accuracy and understanding.