CSC 4181Compiler Construction Context-ree Grammars Using grammars in parsers CG 1 Parsing Process Call the scanner to get tokens Build a parse tree from the stream of tokens A parse tree shows the syntactic structure of the source program. Add information about entifiers in the symbol table Report error, when found, and recover from thee error CG 2 Context-ree Grammar a tuple (V, T, P, S) where V is a finite set of nonterminals, containing S, T is a finite set of terminals, P is a set of production rules in the form of aβ where ais in V and β is in (V UT ), and S is the start symbol. Any string in (V U T) is called a sentential form. CG 3 1
xamples O () O O - O O / S SS S (S)S S () S CG 4 Backus-Naur orm (BN) Nonterminals are in < >. Terminals are any other symbols. ::= means. means or. xamples: <> ::= <><O><> (<>) ID <O> ::= - / <S> ::= <S><S> (<S>)<S> () CG 5 Derivation A sequence of replacement of a substring in a sentential form. Definition Let G = (V, T, P, S ) be a CG, a,, be strings in (V U T) and A is in V. aa G a if A is in P. G denotes a derivation in zero step or more. CG 6 2
xamples S SS (S)S () S SS (S)SS (S)S(S)S (S)S(())S ((S)S)S(())S ((S)())S(())S ((())())S(())S ((())()) (())S ((())())(()) O () O - / O () O ( O ) O (( O ) O ) O (( O )) O ) O (( )) O ) O (( )) O ) O (( )) ) CG 7 Leftmost Derivation ach step of the derivation is a replacement of the leftmost nonterminals in a sentential form. Rightmost Derivation ach step of the derivation is a replacement of the rightmost nonterminals in a sentential form. O () O ( O ) O ( O ) O ( ) O ( ) O ( ) ( ) O O () ( O ) ( O ) ( ) ( ) CG 8 Right/Left Recursive A grammar is a left recursive if its production rules can generate a derivation of the form A A. xamples: O () () A grammar is a right recursive if its production rules can generate a derivation of the form A A. xamples: O () () CG 9 3
Parse Tree A labeled tree in which the interior nodes are labeled by nonterminals leaf nodes are labeled by terminals the children of an interior node represent a replacement of the associated nonterminal in a derivation corresponding to a derivation CG 10 Parse Trees and Derivations 1 3 2 4 5 Preorder numbering 1 2 5 4 3 Reverse or postorder numbering (1) (2) (3) (4) (5) (1) (2) (3) (4) (5) CG 11 List of statements: No statement One statement: s; More than one statement: s; s; s; A statement can be a block of statements. {s; s; s;} Is the following correct? { {s; {s; s;} s; {}} s; } Grammar: xample <St> ::= s; s; <St> { <St> } <St> <St> { <St> } <St> { { <St> } <St> } <St> { { s; <St> } <St>} <St> { { s; { <St> } <St> } <St>} <St> { { s; { s; <St> } <St> } <St>} <St> { { s; { s; s; } <St> } <St>} <St> { { s; { s; s; } s; <St> } <St>} <St> { { s; { s; s; } s; { <St> } <St> } <St>} <St> { { s; { s; s; } s; { } <St> } <St>} <St> { { s; { s; s; } s; { } } <St>} <St> { { s; { s; s;} s; { } } s; } <St> { { s; { s; s;} s; { } } s; } CG 12 4
Abstract Syntax Tree Representation of actual source tokens Interior nodes represent operators. Leaf nodes represent operands. CG 13 Abstract Syntax Tree for xpression CG 14 Abstract Syntax Tree for If Statement st ifstatement if if ( exp ) st elsepart true st return true else st return CG 15 5
Ambiguous Grammar A grammar is ambiguous if it can generate two different parse trees for one string. Ambiguous grammars can cause inconsistency in parsing. CG 16 xample: Ambiguous Grammar - / CG 17 Ambiguity in xpressions Which operation is to be done first? solved by precedence An operator with higher precedence is done before one with lower precedence. An operator with higher precedence is placed in a rule (logically) further from the start symbol. solved by associativity If an operator is right-associative (or left-associative), an operand in between 2 operators is associated to the operator to the right (left). Right-associated : W ( (Y Z)) Left-associated : ((W ) Y) Z CG 18 6
Precedence - / - / CG 19 Precedence (cont d) - / ( ) ( ) 4 ( ) 4 CG 20 Associativity Left-associative operators - / ( ) ( ) / 4 = ((( ) ) / 4) CG 21 ( ) / 4 7
Ambiguity in Dangling lse St IfSt... IfSt if ( ) St if ( ) St else St 0 1 IfSt if ( ) St { if (0) { if (1) St else St } } 0 IfSt if ( ) St else St 1 { if (0) { if (1) St } else St } IfSt if ( ) St else St IfSt if ( ) St CG 22 0 1 Disambiguating Rules for Dangling lse St MatchedSt UnmatchedSt UnmatchedSt if () St if () MatchedSt else UnmatchedSt St MatchedSt if () MatchedSt else MatchedSt UnmatchedSt... 0 1 if ( ) St MatchedSt if (0) if (1) St else St = if (0) if (1) St else St if ( ) MatchedSt else MatchedSt CG 23 xtended Backus-Naur orm (BN) Kleene s Star/ Kleene s Closure Seq ::= St {; St} Seq ::= {St ;} St Optional Part IfSt ::= if ( ) St [else St] ::= [ ] [- ] CG 24 8
Syntax Diagrams Graphical representation of BN rules IfSt nonterminals: terminals: sequences and choices: xamples ::= () Seq ::= {St ;} St ::= [ ] ( ) CG 25 ; St St 9