CSC 4181 Compiler Construction Context-Free Grammars Using grammars in parsers CFG 1 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 identifiers in the symbol table Report error, when found, and recover from thee error CFG 2 2 1
Context-Free 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 β where is in V and β is in ( V U T )* , and S is the start symbol. Any string in (V U T)* is called a sentential form � CFG 3 3 Examples E E O E S SS E � S (S)S (E) E id S () O � S � + O � - O � * � O � / CFG 4 4 2
Backus-Naur Form (BNF) Nonterminals are in < > . Terminals are any other symbols. ::= means � | means or. Examples: < E> ::= < E> < O> < E> | (< E> ) | ID < O> ::= + | - | * | / < S> ::= < S> < S> | (< S> )< S> | () | CFG 5 5 Derivation A sequence of replacement of a substring in a sentential form. Definition ( V , T , P , S ) be a CFG, , , be Let G � � � strings in ( V U T ) * and A is in V � A � G � if A � is in P � � G denotes a derivation in zero step or more. CFG 6 6 3
Examples S SS | (S)S | () | E E O E | (E) | id O + | - | * | / S SS E (S)SS E O E (S)S(S)S (E) O E (S)S(())S (E O E) O E ((S)S)S(())S ((E O E) O E) O E ((S)())S(())S ((id O E)) O E) O E ((())())S(())S ((id + E)) O E) O E ((())()) (())S ((id + id)) O E) O E ((())())(()) ((id + id)) * id) + id CFG 7 7 Leftmost Derivation Rightmost Derivation Each step of the derivation Each step of the derivation is a replacement of the is a replacement of the leftmost nonterminals in a rightmost nonterminals in sentential form. a sentential form. E E E O E E O E ( E ) O E E O id ( E O E) O E E * id (id O E) O E ( E ) * id (id + E ) O E (E O E ) * id (id + id) O E (E O id) * id (id + id) * E ( E + id) * id (id + id) * id (id + id) * id CFG 8 8 4
Right/Left Recursive A grammar is a left A grammar is a right recursive if its recursive if its production rules can production rules can generate a derivation generate a derivation of the form A * A X. of the form A * X A. Examples: Examples: E E O id | (E) | id E id O E | (E) | id E F + id | (E) | id E id + F | (E) | id F E * id | id F id * E | id E F + id E id + F E * id + id id + id * E CFG 9 9 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 E � F id id E � id CFG 10 10 5
Parse Trees and Derivations E � E E + E (1) E � id + E E � (2) � id + E * E E � E � (3) id � id + id * E (4) id id id + id * id Preorder numbering (5) E E + E E � (1) E + E * E E � (2) E � � E + E * id (3) E � E � id � E + id * id (4) id id id + id * id (5) Reverse or postorder numbering CFG 11 11 Grammar: Example <St> ::= | s; | s; <St> | { <St> } <St> List of statements: <St> No statement { <St> } <St> One statement: { { <St> } <St> } <St> { { s; <St> } <St> } <St> s; { { s; { <St> } <St> } <St> } <St> More than one { { s; { s; <St> } <St> } <St> } <St> statement: { { s; { s; s; } <St> } <St> } <St> s; s; s; { { s; { s; s; } s; <St> } <St> } <St> A statement can be a { { s; { s; s; } s; { <St> } <St> } <St> } <St> block of statements. { { s; { s; s; } s; { } <St> } <St> } <St> { { s; { s; s; } s; { } } <St>} <St> { s; s; s;} { { s; { s; s;} s; { } } s; } <St> Is the following correct? { { s; { s; s;} s; { } } s; } { { s; { s; s;} s; { } } s; } CFG 12 12 6
Abstract Syntax Tree Representation of actual source tokens Interior nodes represent operators. Leaf nodes represent operands. CFG 13 13 Abstract Syntax Tree for Expression E � � E E � id1 E E � id1 id2 id3 id2 id3 CFG 14 14 7
Abstract Syntax Tree for If Statement st ifStatement if elsePart st � � exp if true st return else st true return CFG 15 15 Ambiguous Grammar A grammar is ambiguous if it can generate two different parse trees for one string. Ambiguous grammars can cause inconsistency in parsing. CFG 16 16 8
Example: Ambiguous Grammar E � E + E E � E - E E � E * E E E / E E id E E + E * E E E E E E E id3 * id1 + id2 id3 id1 id2 CFG 17 17 Ambiguity in Expressions 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 + (X + (Y + Z)) Left-associated : ((W + X) + Y) + Z CFG 18 18 9
Precedence E � E E + E E E E - E + E * E E E E E * E E E E E id3 E E / E * + id1 E id id1 id2 id2 id3 E � E + E E E E - E E + E E F F F F � F * F F / F F * F id1 F � id F � id2 id3 CFG 19 19 Precedence (cont’d) E � E + E | E - E | F E F F * F | F / F | X F X � ( E ) | id F * F X F * F X X ( E ) (id1 + id2) * id3 * id4 id4 id3 E + E F F X X id1 id2 CFG 20 20 10
Associativity E Left-associative operators F E � E + F | E - F | F X F / F � F * X | F / X | X X � * X id4 ( E ) | id F X id3 ( E ) (id1 + id2) * id3 / id4 + E F = (((id1 + id2) * id3) / id4) F X X id2 id1 CFG 21 21 Ambiguity in Dangling Else St � IfSt | ... IfSt � if ( E ) St | if ( E ) St else St E � { if (0) 0 | 1 | … { if (1) St } else St } { if (0) { if (1) St else St } } IfSt IfSt � � if E St else St if � E � St 0 IfSt IfSt 0 � � if E St if � E � St else St 1 1 CFG 22 22 11
Disambiguating Rules for Dangling Else St MatchedSt | UnmatchedSt UnmatchedSt if (E) St | St if (E) MatchedSt else UnmatchedSt MatchedSt � UnmatchedSt if (E) MatchedSt else MatchedSt| ... E if � E � St 0 | 1 MatchedSt if (0) if (1) St else St = if (0) if � E � MatchedSt else MatchedSt if (1) St else St CFG 23 23 Extended Backus-Naur Form (EBNF) Kleene’s Star/ Kleene’s Closure Seq ::= St { ; St} Seq ::= { St ;} St Optional Part IfSt ::= if ( E ) St [else St] E ::= F [+ E ] | F [- E] CFG 24 24 12
Syntax Diagrams Graphical representation of EBNF rules IfSt nonterminals: id terminals: sequences and choices: Examples � � E X ::= (E) | id id St Seq ::= { St ;} St � St F � E ::= F [+ E ] E CFG 25 25 13
Recommend
More recommend