Professional Documents
Culture Documents
412
FALL
2015
Parsing V
Comp
412
source
code
IR
Front
End
OpMmizer
target
IR
Back
End
code
Copyright
2015,
Keith
D.
Cooper
&
Linda
Torczon,
all
rights
reserved.
Students
enrolled
in
Comp
412
at
Rice
University
have
explicit
permission
to
make
copies
of
these
materials
for
their
personal
use.
Faculty
from
other
educaMonal
insMtuMons
may
use
these
materials
for
nonprot
educaMonal
purposes,
provided
this
copyright
noMce
is
preserved.
Chapter 3 in EaC2e
PredicMve Parsing
/*
nd
an
A
*/
if
(current_word
FIRST+(A1))
nd
a
1
and
return
true
else
if
(current_word
FIRST+(A2))
nd
a
2
and
return
true
else
if
(current_word
FIRST+(A3))
nd
a
3
and
return
true
else
report
an
error
and
return
false
Goal
Expr
Expr
Term Expr
Expr
+ Term Expr
| - Term Expr
Term
Factor Term
Term
* Factor Term
| / Factor Term
Factor ( Expr )
10
| number
11
| id
Factor(
)
if
(token
=
number
)
then
token
next_token(
);
return
true;
else
if
(token
=
idenMer
)
then
token
next_token(
);
return
true;
else
if
(token
=
lparen
)
token
next_token(
);
if
(Expr(
)
=
true
&
token
=
rparen
)
then
token
next_token(
);
return
true;
//
fall
out
of
if
statement
report
syntax
error;
return
false;
EPrime,
Term,
&
TPrime
follow
the
same
basic
lines
(Figure
3.10,
EaC2e)
4
Recursive
Descent
Page
111
in
EaC2e
sketches
a
recursive
descent
parser
for
the
right-recursive
version
of
the
classic
expression
grammar.
One
rouMne
per
NT
Check
each
RHS
by
checking
each
symbol
Includes
-producMons
Main( )
/* Goal Expr */
word NextWord( );
if (Expr( ))
then if (word = eof )
then report success;
else Fail( );
TPrime( )
Fail( )
report syntax error;
attempt error recovery or exit;
Expr( )
/* Expr Term Expr */
if ( Term( ) )
then return EPrime( );
else Fail();
EPrime( )
/* Expr + Term Expr */
/* Expr - Term Expr */
if (word = + or word = - )
then begin;
word NextWord( );
if ( Term() )
then return EPrime( );
else Fail();
end;
else if (word = ) or word = eof)
/* Expr */
then return true;
else Fail();
Term( )
/* Term Factor Term */
if ( Factor( ) )
then return TPrime( );
else Fail();
if (word = or word = )
then begin;
word NextWord( );
if ( Factor( ) )
then return TPrime( );
else Fail();
end;
else if (word = + or word = - or
word = ) or word = eof)
/* Term */
then return true;
else Fail();
Factor( )
/* Factor ( Expr ) */
if (word = ( ) then begin;
word NextWord( );
if (not Expr( ) )
then Fail();
if (word = ) )
then Fail();
word NextWord( );
return true;
end;
/* Factor num */
/* Factor name */
else if (word = num or
word = name )
then begin;
word NextWord( );
return true;
end;
else Fail();
FIGURE 3.12
*/
*/
Python?
Non-terminal
Symbols
Factor
Goal
Expr
Expr
Term Expr
Expr
+ Term Expr
- Term Expr
Term
Factor Term
Term
* Factor Term
/ Factor Term
( Expr )
10
number
11
idenMer
Terminal Symbols
Factor
Id.
Num.
EOF
11
10
9 *
id
num
EOF
Goal
Expr
Expr
Term
Term
11
10
Row
we
built
earlier
Factor
10
11
12
This
algorithm
is
the
LL(1)
table
construcMon
algorithm
In
Lab
2,
you
will
build
a
recursive
descent
parser
for
a
modied
form
of
BNF
and
build
LL(1)
tables
for
the
grammars
that
are
LL(1).
COMP
412,
Fall
2015
13
14
Parsing
Techniques
Top-down
parsers
(LL(1),
recursive
descent)
Start
at
the
root
of
the
parse
tree
and
grow
toward
leaves
Pick
a
producMon
&
try
to
match
the
input
G
Bad
pick
may
need
to
backtrack
E
Some
grammars
are
backtrack-free
E
<id,x>
<num,2>
F
<id,y>
15
16
boTom-up
we
call
it
a
reducBon.
Rightmost
derivaMon
in
reverse
processes
words
le^
to
right
17
Finding
ReducMons
Consider
the
grammar
0
Goal
2
3
a A B e
SentenIal
Form
Next
ReducIon
Prodn
Posn
A b c
abbcde
| b
a A bcde
a A de
a A B e
Goal
The
trick
is
scanning
the
input
and
nding
the
next
reducMon.
The
mechanism
for
doing
this
must
be
ecient.
The
reducMons
are
obvious
from
the
derivaMon.
Of
course,
building
the
derivaMon
is
not
a
pracMcal
way
to
nd
it.
COMP
412,
Fall
2015
18
Finding
ReducMons
Consider
the
grammar
0
Goal
2
3
a A B e
SentenIal
Form
Next
ReducIon
Prodn
Posn
A b c
abbcde
| b
a A bcde
a A de
a A B e
Goal
The
trick
is
scanning
the
input
and
nding
the
next
reducMon
The
mechanism
for
doing
this
must
be
ecient
PosiIon
species
where
the
right
end
of
occurs
in
the
current
sentenIal
form.
While
the
process
of
nding
the
next
reducMon
appears
to
be
almost
oracular,
it
can
be
automated
in
an
ecient
way
for
a
large
class
of
grammars.
COMP
412,
Fall
2015
19
20
21
Example
0
Goal
Expr
1 Expr
Expr + Term
| Expr - Term
| Term
4 Term
Term * Factor
| Term / Factor
| Factor
(
Expr
)
7
Factor
8
| number
| id
22
Example
0
Goal
Expr
1 Expr
Expr + Term
Goal
| Expr - Term
Expr
| Term
Expr - Term
4 Term
Term * Factor
| Term / Factor
| Factor
(
Expr
)
7
Factor
| number
| id
9
<id,x>
-
<num,2>
*
<id,y>
A
simple
leA-recursive
form
of
the
classic
expression
grammar
derivaMon
Rightmost
deriva@on
of
x
2
*
y
23
Example
0
Goal
Expr
1 Expr
Expr + Term
Goal
| Expr - Term
Expr
| Term
Expr - Term
4 Term
Term * Factor
| Term / Factor
| Factor
(
Expr
)
7
Factor
| number
| id
parse
24
Handles
At
this
point,
handles
appear
mysterious
Dont
Panic:
handles
are
mysterious
Next
lecture
will
focus
on
handles
If
it
were
easy,
it
would
not
have
taken
Knuth
to
invent
it!
Next
Class
Handles,
handles,
and
more
handles
COMP
412,
Fall
2015
25