0% found this document useful (0 votes)
34 views

Handout 11 CFG

- Context free grammars (CFGs) are used to define the syntax of languages by specifying rules for rewriting nonterminal symbols. - A CFG consists of nonterminal symbols, terminal symbols from an alphabet, and productions that replace nonterminals with terminals and/or nonterminals. - Strings generated by applying productions are called derivations; their parse trees represent the production steps. - The language defined by a CFG is the set of all strings that can be derived from the start symbol. Ambiguous CFGs allow multiple parse trees for some strings.

Uploaded by

TALHA SALEEM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Handout 11 CFG

- Context free grammars (CFGs) are used to define the syntax of languages by specifying rules for rewriting nonterminal symbols. - A CFG consists of nonterminal symbols, terminal symbols from an alphabet, and productions that replace nonterminals with terminals and/or nonterminals. - Strings generated by applying productions are called derivations; their parse trees represent the production steps. - The language defined by a CFG is the set of all strings that can be derived from the start symbol. Ambiguous CFGs allow multiple parse trees for some strings.

Uploaded by

TALHA SALEEM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Context Free Grammars

Context Free Grammars


• By definition a context-free grammar is a finite set of
variables (also called non-terminals) each of which
represents a language.
• The languages represented by the variables are
described recursively in terms of each other and
primitive symbols called terminals.
• The rules relating the variables are called productions.
Context Free Grammars
• Example
– S → aS
– S→Λ
– Continuous strings of as
• Strings with at least one double letter
– S → ADA
– A → aA
– A → bA
– A→Λ
– D → aa
– D → bb
Context Free Grammars
• A context-free grammar, is a collection of three things
– An alphabet  of letters called terminals from which strings of
language are generated
– A set of symbols called nonterminals, one of which is a symbol S,
termed as the start symbol
– A finite set of productions (production rules) of the form
• One nonterminal Finite string of Terminals and / or Nonterminals

• The strings of terminals and nonterminals can consist of only


terminals or of only nonterminals, or of any mixture of terminals
and nonterminals or even the empty string
• A CFG must has at least one production that has the nonterminal
S at its left side
Context Free Grammars
• Nonterminal / Variables / Syntactic category
– A symbol that can be substituted by some other
symbol(s)
– Variable because the same non-terminal can have
multiple substitutions
• Terminal
– A symbol that cannot be substituted further
– Letters from the alphabet set
Context Free Grammars
• Conventions for CFG
– Nonterminals are written in upper case letters

– Terminals Symbols are written in lower case

• Terminal symbols are also called atomic


symbols
Context Free Grammars
• Terminologies
– Generation or Derivation
• The sequence of applications of the rules that
produces the finished string of terminals from
the starting symbol is called a generation or a
derivation of the word
– Production
• The grammatical rules are called productions
Context Free Languages
• The language generated by a CFG is the set of all
strings of terminals that can be produced from the
start symbol S using the productions as
substitutions.

• A language generated by a CFG is called a Context


Free Language (CFL)
Context Free Grammars
• Non terminals vs. terminals
–S→X
–S→Y
–X→Λ
– Y → aY
– Y → bY
–Y→a
–Y→b
Context Free Grammars
• S → XaaX
• X → aX
• X → bX
• X→Λ
• (a+b)* aa (a+b)*
CFG
• Examples
– All strings that contain the substring
“bbb”
– All strings that start and end with
different letters
– All strings that don’t end at ba
CFG
• Which languages do these CFGs
define
– S → abS
– S → ab

– S → aS
– S → bb
Context Free Grammars
• CFG For L = {anbn n 0 1 2 3 4 …}
– S → aSb
– S→Λ
– S → ab
• CFG For EQUAL
– S → aB
– S → bA
– A→a
– A → aS
– A → bAA
– B→b
– B → bS
– B → aBB
Context Free Grammars
• CFG For EQUAL
– S → aB
– S → bA
– A→a
– A → aS
– A → bAA
– B→b
– B → bS
– B → aBB
• Can be compactly written as
S → aB | bA <S> ::= a<B> | b<A>
A → a | aS | bAA <A >::= a | a<S> | b<A><A>
B → b | bS | Abb <B> ::= b | b<S> | <A>bb
Context Free Grammars
• CFG For identifier
– IDENTIFIER → ALPHA /ALPHANUMERIC

– ALPHA → A|B|….|Z|a|b|c….|z

– ALPHANUMERIC → ALPHA ALPHANUMERIC |

NUMERIC ALPHANUMERIC | Λ

– NUMERIC → 0|1|2…|9
Context Free Grammars
• CFG For arithmetic expressions
– <expression>  <expression> + <expression>
– <expression>  <expression> * <expression>
– <expression>  <expression> - <expression>
– <expression>  <expression> + <expression>
– <expression>  (<expression>)
– <expression>  <number>
Context Free Grammars
• Derivation or Generation
• S → abS | Λ
• S  abS
  ababS
  abababS
  ababab
  abab
Parse Trees
• A tree format used for the derivation of a string from
the CFG
• Parse tree, Syntax tree, Generation tree, Production
tree, Derivation tree
• Start symbol of the CFG at root
• Non terminals are represented as nodes
• Terminals as leaves
• Every next level of tree is a derivation from a
production of CFG
• The yield of a parse tree is a terminal string held at all
the leaves
Parse Trees
• Examples
– S → abS | Λ
– Derivation of abababab

a b S

a b S

a b S

a b Λ
Derivation
• Left Most Derivation
– If a word w is generated by a CFG by a certain derivation and
at each step in the derivation a rule of production is applied to
the leftmost nonterminal in the working string then this
derivation is called a leftmost derivation
• Right Most Derivation
– If a word w is generated by a CFG by a certain derivation and
at each step in the derivation a rule of production is applied to
the rightmost nonterminal in the working string then this
derivation is called a rightmost derivation
Ambiguity
• A CFG is called ambiguous if for at least one
word in the language that it generated, there
are two possible derivations of the word that
corresponds to different syntax trees.

• A CFG which is not ambiguous is called


unambiguous CFG
Ambiguous Grammars
• S → aS |Sa |a
• Derivation of aaa

S S S S

a a
S S S a S a

a S S a a S S a

a a a a

• S → aS | a

You might also like