Sei sulla pagina 1di 20

Context-Free Grammars

Lecture -6

1
Topics

• Introduction to CFG
• Introduction to CFL
• Parse Trees and Derivation

Module Code and Module Title Title of Slides


Context Free Grammars

• Way of understanding the relationship of terms


such as noun,verb,preposition and the phrases.
• CFG helps to organize and understand the
relationship
• Designers of Programming language often start
by obtaining grammar for the language
• Most compilers contains parser to extract the
meaning of the program prior to compilation.

Module Code and Module Title Title of Slides


Informal Comments

• A context-free grammar is a notation for


describing languages.

• It is more powerful than finite automata or RE’s,


but still cannot define all possible languages.

• Useful for nested structures, e.g., parentheses in


programming languages.

4
Module Code and Module Title Title of Slides
What is Context Free?

Module Code and Module Title Title of Slides


Context Free Grammars
Formal Definition
• A context-free grammar G is a quadruple (V,∑,R,S)
where
• V is a finite set of symbols (terminals and non-terminals)
• ∑ is the set of terminal symbols (the alphabet of the
language)
• S is a distinguished element of (V - ∑) called the start
symbol, and
• R is a set of rules

• Members of (V - ∑) are called non-terminals


– sometimes called variables
• The set of rules, R, is a finite subset of (V - ∑)  V *
(mappings of non-terminals to a string of non-terminals and
terminals)
Module Code and Module Title Title of Slides
6
CFG Components
• A set of non-terminals (V).
Non-terminals are syntactic variables that denote sets of strings.
The non-terminals define sets of strings that help define the
language generated by the grammar.
• A set of tokens, known as terminal symbols (Σ).
Terminals are the basic symbols from which strings are formed.
• A set of productions (P).
The productions of a grammar specify the manner in which the
terminals and non-terminals can be combined to form strings. Each
production consists of a non-terminal called the left side of the
production, an arrow, and a sequence of tokens and/or on-
terminals, called the right side of the production.
One of the non-terminals is designated as the start symbol (S); from
where the production begins.

Module Code and Module Title Title of Slides


Example: Formal CFG

• Here is a formal CFG for { 0n1n | n > 1}.


• Terminals = {0, 1}.
• Variables = {S}.
• Start symbol = S.
• Productions =
S -> 01
S -> 0S1

8
Module Code and Module Title Title of Slides
Context Free Grammars-
Example-1

• Grammar G1 = (V, ∑,R,S), where V = {S,a,b}, ∑ = {a,b} and R


consists of rules {S → aSb and S → ∈ }.

• Using these rules we can get:


S  aSb  aaSbb  aabb
S  aSb  aaSbb  aaaSbbb  aaabbb

The language of G, denoted L(G), is the set {w  S }.

• L(G1) is clearly {anbn : n ≥ 0}.

• Recall that a regular expression can’t specify this language


Module Code and Module Title Title of Slides
9
Context Free Grammars-
Example-2
a) S → aS | bB | ∈
B → aB | bS

S  aS  abB  abbS  abbbB  abbbaB 


abbbabS  abbbab

b) S → aB | bS | cS | ∈
B → aB | bC | cS | ∈
C → aB | bS | ∈
S  aB  abC  abaB  abacS  abac

Module Code and Module Title Title of Slides


10
Context-Free Languages

• A language that is defined by some


CFG is called a context-free language.
• There are CFL’s that are not regular
languages, such as the example just
given.
• But not all languages are CFL’s.
• Intuitively: CFL’s can count two things,
not three.

11
Module Code and Module Title Title of Slides
Derivations – Intuition

• We derive strings in the language of a CFG by


starting with the start symbol, and repeatedly
replacing some variable A by the right side of
one of its productions.

– That is, the “productions for A” are those that


have A on the left side of the ->.

12
Module Code and Module Title Title of Slides
Derivations – Formalism

• We say A =>  if A ->  is a production.

– Example: S -> 01; S -> 0S1.

• S => 0S1 => 00S11 => 000111.

13
Module Code and Module Title Title of Slides
Iterated Derivation

=>* means “zero or more derivation steps.”

• Basis:  =>*  for any string .


• Induction: if  =>*  and  => ,
then  =>* .

14
Module Code and Module Title Title of Slides
Example: Iterated Derivation

1.S -> 01; S -> 0S1.


2.S => 0S1 => 00S11 => 000111.
3.So S =>* S; S =>* 0S1; S =>* 00S11; S
=>* 000111.

15
Module Code and Module Title Title of Slides
Parsing

• Given a context-free grammar G and input w determine if


w  L(G).
• How do we determine this for all possible strings?
• Multiple derivations may exist
• Must also discover when no derivation exists

• A procedure to perform this function is called a parsing


algorithm or parser.

• Some grammars allow deterministic parsing, i.e. each


sentential form has at most one derivation

Module Code and Module Title Title of Slides


16
Parsing

Rule application
• Given the string uAv and the rule A → w, we
obtain the string uwv.
– We denote this as uAv  uwv.

• A string w is derivable from v if there is a finite


sequence of rule applications from v to w.
– v  w1  w2  ...  wn = w
– Usually written as v * w

Module Code and Module Title Title of Slides


17
Parsing

• S  AA  AbA  abA,
• so S * abA.

• The length of a derivation v * w is the number


of rule applications in the derivation

Module Code and Module Title Title of Slides


18
Parsing
Derivations:  aaab is derivable from S:
• S → aMb (1)
• M→A (2)
 S  aMb  aAb  aaAb
• M→B (3)
 aaaAb  aaab
• A → aA (4)
• A→ ∈ (5)
• B → bB (6)
 S * aaab.
• B→∈ (7)
 The length of this
derivation is 5

Module Code and Module Title Title of Slides


19
Ambiguous Grammars
• Recall the grammar G = V, S,R,S where
V = *, +, (, ),E
S = *, +, (, )
R= S→E
E → E + E | E * E | (E) | id

• The string id + id * id can be generated by this grammar according


to two different parse trees.

 Only one of these (a) corresponds to the “natural” semantics of id +


id * id, where * takes precedence over +.
Module Code and Module Title Title of Slides
20

Potrebbero piacerti anche