

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CMSC 330 2
CMSC 330 3
CMSC 330 4
CMSC 330 5
CMSC 330 6
CMSC 330 7
First(V) = { 0…9 } First(U) = First( (S) ) First(V) = { ( } { (, 0…9 } = { (, 0…9 } First(M) = First( *T ) First( ) = { * } { } = { *, } First(T) = First( UM ) = First( U ) = { (, 0…9 } First(L) = First( +S ) First( ) = { + } { } = { +, } First(S) = First( TL ) = First( T ) = { (, 0…9 }
CMSC 330 8
parse_S( ) { // S TL parse_T(); parse_L(); }
parse_L( ) { // L + S if (lookahead == “+”) { match(“+”); parse_S( ); }
else ; }
parse_T( ) { // T UM parse_U(); parse_M(); }
CMSC 330 9
Grammar
Recursive descent parser parse_M( ) {
if (lookahead == “”) { match(“”); parse_T( ); }
parse_U( ) {
match(“(”); parse_S( ); match(“)”); }
parse_V( ) {
match(“0”);
match(“1”); } else …
match(“9”); } else error( ); } (^) CMSC 330 10
Grammar
Lookahead in red Parse “1+2” Remaining parse_S( ) “ 1 +2” parse_T( ) “ 1 +2” parse_U( ) “ 1 +2” parse_V( ) “ 1 +2” match(“1”) “ 1 +2” parse_M( ) “+2” parse_L( ) “+2” match(“+”) “+2” parse_S( ) “ 2 ” parse_T( ) “ 2 ” parse_U( ) “ 2 ” parse_V( ) “ 2 ” match(“2”) “ 2 ” “”
CMSC 330 11
S ABd | aBc
B b | c
First(b) = {b} First(c) = {c} First(B) = First(b) First(c) = {b,c}
First( ) = { } First(A) = First( ) = { }
First(ABd) = {First(A) – } First(Bd) = { – } First(Bd) = First(Bd) = First(B) = {b, c} First(aBc) = { a }
First(S) = First(ABd) First(aBc) = {b, c} {a} = {a,b,c}
CMSC 330 12
S ABd | aBc
B b | c
First(b) = {b} First(c) = {c}
First(ABd) = {b, c} First(aBc) = { a }
And not left recursive…