Академический Документы
Профессиональный Документы
Культура Документы
• Contents
– Introduction Context free grammar
– Parsing
– 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?
– Parsing
4
Introduction on CFG
5
Definition
• There are tools called “parser generators” that turn CFG’s into parsers. One
of them is the famous YACC (“Yet Another Compiler Compiler”).
7
parse tree
• Graphical means to illustrate a derivation of a
string from a grammar
– Root of the tree = start variable
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:
13
E
Sample Parse Tree
E * E
• Using a leftmost derivation
generates the parse tree for
a*(a+b1) I ( E )
Leftmost derivation:
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:
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.
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.
21
Ambiguity and Parse Trees
The ambiguity of 01+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?
– SAS | ε
– AA1 | 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:
– EE+E | EE | id becomes:
EE+T | T
TTF | F
F id
32
example
• Consider the grammar show that this grammar
is ambiguous.
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?
36