Multimedia Application L4
Multimedia Application L4
Application
By
Production rules:
S → aSa
S → bSb
S→c
check that abbcbba string can be derived from the given CFG.
S ⇒ aSa
S ⇒ abSba
S ⇒ abbSbba
S ⇒ abbcbba
Context Free Grammar (CFG)
Categories of CFG
Recursive CFG
S -> Aa output: { ca, cba, cbba, cbbba, …. } S->
Aa
->Aba
-> cba
A -> Ab|c
Context Free Grammar (CFG)
Example of a CFG
S -> Aa S-> Aa
A -> Ab| c -> Aba
-> Abba
-> cbba
Input: {c, b, b, a}
Parsing using
CFG
Parse tree
Parse tree
In the left most derivation, the input is scanned and replaced with the
production rule from left to right. So in left most derivatives we read
the input string from left to right.
Production
S=S+S
S=S-S
S=a|b|
c
Input:
a-b+c
Left-most Derivation
S=S+S
S=S-S+S
S=a-S+S
S=a-b+S
S=a-b+c
Right-most Derivation
In the right most derivation, the input is scanned and replaced with
the production rule from right to left. So in right most derivatives we
read the input string from right to left.
S=S+S
S=S-S
S = a | b |c
Input: a - b + c
The right-most derivation is:
S=S-S
S=S-S+S
S=S-S+c
S=S-b+c
S=a-b+c
Ambiguity
Example grammar:
S -> S + S Input String: 1 + 2 *
3
S -> S * S
S -> NUMBER
+ *
/\ /\
1 * + 3
/\ /\
2 3 1 2
Parser
Equivalent token
C program
Advantages of Lexical analysis
• Simplifies Parsing: Breaking down the source code into tokens makes it
easier for computers to understand and work with the code. This helps
programs like compilers or interpreters to figure out what the code is
supposed to do. It’s like breaking down a big puzzle into smaller pieces,
which makes it easier to put together and solve.
Grammar
S → S+S
S → S-S
S → (S)
S→a
w = id + id * id
Equivalent parse
tree
Operator precedence parsing
ROOT: "sat" is the root of the sentence—the main verb. All other words depend on it in
some way.
nsubj (nominal subject): "cat" is the nominal subject of the verb "sat." It's the one doing
the sitting.
det (determiner): "The" (both instances) are determiners, modifying the nouns "cat" and
"mat."
prep (preposition): "on" is a preposition.
pobj (object of preposition): "mat" is the object of the preposition "on." It's what the cat
is sitting on.
Reference
Chapter 2 Chapter 5
Question
Thank you