Chapter Three
Chapter Three
Principle and
Technology
Prof. Dongming LU
Mar. 5th, 2014
3. Context-Free
Grammars and
Parsing
(PART ONE)
Contents
PART ONE
3.1 The Parsing Process
3.2 Context-Free Grammars
3.3 Parse Trees and Abstract
PART TWO
3.4 Ambiguity
3.5 Extended Notations: EBNF and Syntax Diagrams
3.6 Formal Properties of Context-Free Languages
Introduction
Syntax
Syntax
analysis
Analysis
Tool
Context-free
Grammar
Top Down(LL)
Derivation
Parser
Bottom Up(LR)
Data
Structure
Function of a Parser
Parser
Sequence of tokens
Trees
initial + rate * 60
id + id * num
expression
expression
identifier
(initial)
+
expression
identifier
(rate)
Parse Tree
expression
expression
number
(60)
Multi-Pass compiler
The further passes will use the syntax tree as their
input
The structure of the syntax tree is heavily dependent
on the particular syntactic structure of the language
This tree is usually defined as a dynamic data
structure
Each node consists of a record whose fields include
the attributes needed for the remainder of the
compilation process (i.e., not just those computed by
the parser)
but
Back
Type 1
Type 2: CFG
Type 3:RE
We need a
more powerful
language!
3.2.1
Comparison to
Regular
Expression
Notation
Comparing an Example
A context-free grammar is a specification for the
syntactic structure of a programming language.
Similar to the specification of the lexical structure of
a language using regular expressions Except involving
recursive rules
Basic Rules
Regular Expression
1. Three operations:
Choice,
concatenation,
repetition
2. = represents the
definition of a name for
3. Name was written in
italics.
Grammar
characteristics
Regular Expression
Advantages:
The rules are simple
and easy for description
and understand
Disadvantages:
Its express
suitablepower is
The
limited
for Lexical
analysis
for Syntax
Disadvantages:
analysis
Grammar
rules
is
more
complicated
than
Regular
Expression; It cant express all kinds
of programming languages.
3.2.2
Specification of
Context-Free
Grammar Rules
example:
3.2.3
Derivations &
Language
Defined by a
Grammar
Derivations
Derivations
The example
exp exp op exp | (exp) | number
op + | | *
A derivation
(1) exp => exp op exp
[exp number]
[op * ]
[exp ( exp ) ]
[exp number]
[op - ]
[exp number]
Language Defined
by a Grammar
The set of all strings of token symbols obtained by
derivations from the exp symbol is the language defined by
the grammar of expressions.
L(G) = { s | exp =>* s }
program-block ..
Symbols in TINY
Represent
Instead
Examples
Example 3.1:
The grammar G with the single grammar rule
E (E) | a
This grammar generates the language
L(G) = { a,(a),((a)),(((a))),}
= { (na)n | n an integer >= 0 }
Eg : Derivation for ((a))
E => (E) => ((E)) => ((a))
Examples
Example 3.2:
The grammar G with the single grammar rule
E (E)
This grammar generates no strings at all, there is no
way we can derive a string consisting only of
terminals.
Examples
Example 3.3:
Derivation:
E => E+a => E+a+a => E+a+a+a =>
Example
Examples of strings in
Example 3.4
this language are
Consider the following
extremely simplified
grammar of statements: other
Statement if-stmt |
other
if-stmt if ( exp )
statement
| if ( exp ) statement else
statement
exp 0 | 1
if (0) other
if (1) other
if (0) other else other
if (1) other else other
if (0) if (0) other
if (0) if (1) other else other
if (1) other else if (0) other else
other
Recursion
Recursion
i) Left recursive:
The non-terminal A appears as the first symbol on the
right-hand side of the rule defining A:
A Aa|a
ii) Right recursive:
The non-terminal A appears as the last symbol on the
right-hand side of the rule defining A:
A aA|a
Examples of Recursion
, , , , ...
(All strings beginning with a , followed by 0 or more 's).
, , , , ....
Examples of Recursion
Examples
Example 3.5:
A (A) A |
Generating the strings of all "balanced parentheses."
Examples
Example 3.6:
The statement grammar of Example 3.4 can be written in
the following alternative way using an -production:
statement if-stmt | other
if-stmt if ( exp ) statement else-part
else-part else statement |
exp 0 | 1
Examples
Examples
3.3.1 Parse
trees
[exp
[op * ]
[exp ( exp ) ]
[exp number]
[op - ]
[exp number]
number]
[exp ( exp )]
[exp number]
[op - ]
[exp number]
[op *]
[exp number]
Parsing Tree
Parsing Tree
The example:
exp => exp op exp => number op exp => number + exp
=> number + number
Corresponding to the parse tree:
exp
exp
number
op
exp
number
Parsing Tree
Parsing Tree
Parsing Tree
A
leftmost derivation:
rightmost derivation:
Parsing Tree
4 exp
number
4 exp
(
3 op
5 exp )
8 exp
number
7 op 6 exp
number
2 exp
number
3.3.2 Abstract
syntax trees
exp
exp
number
(3)
op
*
exp
number
(4)
42
34
3
The parentheses tokens have actually disappeared
still represents precisely the semantic content of
subtracting 3 from 34, and then multiplying by 42.
Examples
Example
3.8:
Examples
The
exp
0
) statement
other
else statement
other
Examples
Using
Examples
This same string has the following parse tree:
statement
if-stmt
if
exp ) statement
0
other
other
else
else-part
statement
Examples
if
0
other
other
Examples
StmtKind skind;
struct streenode
*test,*thenpart,*elsepart;
} STreeNode;
typedef STreeNode * SyntaxTree;
Examples
Example
3.9:
Examples
stmt-sequence
stmt
stmt-sequence
stmt
s
The solution: use the standard leftmost-child rightsibling representation for a tree (presented in most
data structures texts) to deal with arbitrary number
of children
Back
End of Part
One
THANKS