Вы находитесь на странице: 1из 36

Chapter four: Context free languages(CFG)

• Contents
– Introduction Context free grammar

– Parsing

– Leftmost and right most derivation

– Derivation tree or Parse tree

– Definition, Relationship between parse trees and derivations.

– Ambiguity grammar

1
Introduction on CFG
• It turns out that many important languages,
– e.g. programming languages.
• Context Free Languages(CFL) is the next class of
languages outside of Regular Languages:
– Means for defining: Context Free Grammar
– Machine for accepting: Pushdown Automata
• To generate strings by beginning with a start
symbol S and then apply rules that describe
how certain symbols may be replaced with
other strings.

2
3
Context Free Languages (CFL)
• Described by Context-Free Grammars (CFG)
– Why named context-free?

– Property that we can substitute strings for variables regardless of


context (implies context sensitive languages exist)

• CFG’s are useful in many applications


– Describing syntax of programming languages

– Parsing

– Structure of documents, e.g.XML

4
Introduction on CFG

5
Definition

Remove all restrictions on the form of the right hand


sides.S → abDeFGab
Keep requirement for single non-terminal on left hand
side.S →
but not ASB → or aSb → or ab →
6
Parsing
• Finding a parse tree for a string is called parsing. Software tools that do
this are called parsers.

• A compiler must parse every program before producing the executable


code.

• A parse tree is a top-down representation of a derivation


– Good way to visualize the derivation process

• There are tools called “parser generators” that turn CFG’s into parsers. One
of them is the famous YACC (“Yet Another Compiler Compiler”).

• Parsing is a science unto itself, and described in detail in courses about


compilers.

7
parse tree
• Graphical means to illustrate a derivation of a
string from a grammar
– Root of the tree = start variable

– Interior nodes = other variables


– Children of nodes = application of a production rule

– Leaf nodes = Terminal symbols

8
Leaves are all labeled with terminals or ε. Other nodes are labeled with
nonterminals.A path is a sequence of nodes, starting at the root, ending at a leaf,
and following branches in the tree.

9
Example

10
An inorder (left to right) traversal of the tree will
give the the string derived.

11
Sample Parse Tree
• Sample parse tree for the palindrome CFG for
1110111:
P   | 0 | 1 | 0P0 | 1P1
P

1 P 1

1 P 1

1 P 1

0 12
Derivation
• Similar to top-down instead of bottom-up
– Expand start symbol first and work way down in such a way that it
matches the input string
• For example, given a + a * a we can derive this by:

• Note that at each step of the productions we could have chosen


any one of the variables to replace with a more specific rule.

13
E
Sample Parse Tree
E * E
• Using a leftmost derivation
generates the parse tree for
a*(a+b1) I ( E )

• Does using a rightmost


derivation produce a different L E + E
tree?
• The yield of the parse tree is the a I I
string that results when we
concatenate the leaves from left
to right (e.g., doing a leftmost L I D
depth first search).
– The yield is always a string that is
a L 1
derived from the root and is
guaranteed to be a string in the
language L.
b
14
Leftmost Derivation
• A leftmost derivation is simply one in which we
replace the leftmost variable in a production body by
one of its production bodies first, and then work our
way from left to right.

Leftmost derivation:

S  aAB  abBbB  abAbB


 abbBbbB  abbbbB  abbbb

15
Rightmost Derivation
• A rightmost derivation is one in which we
replace the rightmost variable by one of its
production bodies first, and then work our way
from right to left.

Rightmost derivation:

S  aAB  aA  abBb  abAb


 abbBbb  abbbb

16
Example Derivations

17
18
Left or Right?
• Does it matter which method you use?
– Answer: No
• Any derivation has an equivalent leftmost
and rightmost derivation.

19
Ambiguity
• A context-free grammar with more than one parse tree for some
expression is called ambiguous.
• A grammar G for a language L is ambiguous if there exist strings in
L for which G can generate more than one parse tree (note that we
don't care about the number of derivations).
– This is often undesirable, for example, in a programming language we would not like the computer to
interpret a line of code in a way different than what the programmer intends.

• To address this problem, parser generators (like YACC) allow the


language designer to specify the operator precedence.

20
Ambiguity
• A grammar is ambiguous if some strings
are derived ambiguously.
A string is derived ambiguously if it has more
than one leftmost derivations.

Typical example: rule S  0 | 1 | S+S | SS

S  S+S  SS+S  0S+S  01+S  01+1


versus
S  SS  0S  0S+S  01+S  01+1

21
Ambiguity and Parse Trees
The ambiguity of 01+1 is shown by the two
different parse trees:

S + S S  S

0
1 S + S
S  S
1 1
0
1

22
Ambiguity in the Grammar

23
Another Example

24
Ambiguous Grammar
• Is the following grammar ambiguous?
– SAS | ε
– AA1 | 0A1 | 01

25
Famous programming language ambiguity

26
27
28
Removing Ambiguity
• There is no algorithm for resolving ambiguity (in the
sense of automatically deriving an unambiguous
grammar from a given grammar).
• Unfortunately, there is no algorithm that can tells us
whether a grammar is ambiguous or not.
• Solutions
– Apply precedence
– e.g. Instead of: E E+E | E*E
– Use: E T | E + T, T F | T * F
• This rule say we multiply first before adding

29
Previous example

30
31
• In general, we try to eliminate ambiguity by
rewriting the grammar.
• Example:
– EE+E | EE | id becomes:

EE+T | T
TTF | F
F id

32
example
• Consider the grammar show that this grammar
is ambiguous.

• Consider the grammar show that this grammar


is unambiguous

33
34
Applications of Context Free Grammars
• Example 1: Parsing Programming Languages
– Consider an arbitrary expression
• Arbitrary nesting of operators
• Parenthesis balancing
• Requires CFG
– YACC – Yet Another Compiler Compiler
– Yacc (Yet Another Compiler Compiler) is a tool for translating a
context free grammar into a bottom-up LALR parser
• Unix program often used to generate a parser for a compiler
• Output is code that implements an automaton capable of parsing the
defined grammar

35
Example 2: XML - What is it?

• XML = eXtensible Markup Language


• Technology for web applications - 1997
• World Wide Web Consortium (W3C)
standard that lets you create your own tags.
– Implications for business-to-business
transactions on the web.

36

Вам также может понравиться