Motivation For Formal Grammars
Motivation For Formal Grammars
Motivation For Formal Grammars
Where
V is a finite set of variables or nonterminals or syntactic
categories
| NUM
| E*E
| E/E
| E+E
| E-E
| (E)
ID → a | b |…|z
NUM → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Grammars Produce Languages
Does the above grammar produce the following
sentence (a*b)+c Start with start symbol of the CFG
E → E+E
Left most derivation
→ ( E)+E
→ ( E*E)+E
Notations
→ (ID*E)+E
G
→ ( a * E)+E E ==> E +E (single step left most derivation)
lm
→ ( a * ID)+E
*
E ==> E+E (zero or more step derivation)
→ ( a * b)+ID lm
*
Example: E ==> (a*b)+c
→ ( a * b) + c G
Context – free languages (CFL ’s)
The languages described by context –free grammars are
known as CFL ’s
formal notation:
The language generated by G [denoted L(G)] is {w | w is
== w }
*
in T* and S ==>
G
That is , a string is in L(G) if:
1) the string consists solely of terminals
2) the string can be derived from S
A string of terminals and variables is called
a sentential form if S ==>
*
==
CFG Example 2
S → if E then S else S
| begin S L
| print E
L → end
|;SL
E → NUM = NUM
NUM → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Parse Tree
Represents the derivation steps from start
symbol to the string
3 digit digit
4 5
What is Parsing?
expr + expr
number expr * expr
4 5