CSC 4181 Compiler Construction ContextFree Grammars Using grammars

  • Slides: 24
Download presentation
CSC 4181 Compiler Construction Context-Free Grammars Using grammars in parsers CFG 1

CSC 4181 Compiler Construction Context-Free Grammars Using grammars in parsers CFG 1

Parsing Process Call the scanner to get tokens n Build a parse tree from

Parsing Process Call the scanner to get tokens n Build a parse tree from the stream of tokens n n A parse tree shows the syntactic structure of the source program. Add information about identifiers in the symbol table n Report error, when found, and recover from the error n CFG 2

Context-Free Grammar n a tuple (V, T, P, S) where V is a finite

Context-Free Grammar n a tuple (V, T, P, S) where V is a finite set of nonterminals, containing S, n T is a finite set of terminals, n P is a set of production rules in the form of β where is in V and β is in (V UT )*, and n S is the start symbol. n n CFG Any string in (V U T)* is called a sentential form 3

Example Grammars E EOE E (E( E id O + O O * O

Example Grammars E EOE E (E( E id O + O O * O / S S SS (S)S () S SS | (S)S | () | E E O E | )E) | id O +|-|*|/ CFG 4

Backus-Naur Form (BNF( n n n Nonterminals are inside. < > Terminals are any

Backus-Naur Form (BNF( n n n Nonterminals are inside. < > Terminals are any other symbols. =: : means . |means or. Same Example Grammars: >E> : : = <E><O><E>| (<E>) | ID >O/ | * | - | + =: : < >S> : : = <S><S> | (<S>)<S> | () | CFG 5

Derivation A sequence of replacement of a substring in a sentential form. Definition n

Derivation A sequence of replacement of a substring in a sentential form. Definition n Let G = (V, T, P, S ) be a CFG, , , be strings in (V U T)* and A is in V. A G if A is in P. n CFG 6

Examples S SS | (S)S | () | S SS (S)SS (S)S(())S ((S)())S(())S ((())())

Examples S SS | (S)S | () | S SS (S)SS (S)S(())S ((S)())S(())S ((())()) (())S (())(()(())) CFG E E O E | (E) | id O /|*|-|+ E EOE (E) O E (E O E) O E ((E O E) O E ((id O E)) O E) O E ((id + id)) * id) + id 7

Leftmost Derivation n E CFG Each step of the derivation Rightmost Derivation n Each

Leftmost Derivation n E CFG Each step of the derivation Rightmost Derivation n Each step of the derivation is a replacement of the leftmost nonterminals in a rightmost nonterminals in a sentential form. EOE (E) O E (E O E) O E (id + id) * E (id + id) * id E EOE E O id E * id (E) * id (E O id) * id (E + id) * id (id + id) * id 8

Right/Left Recursive n n A grammar is a left recursive if its production rules

Right/Left Recursive n n A grammar is a left recursive if its production rules can generate a derivation of the form A * A X. Examples: n E E O id | (E) | id n E F + id | (E) | id F E * id | id n CFG E F + id E * id + id n n A grammar is a right recursive if its production rules can generate a derivation of the form A * X A. Examples: n E id O E | (E) | id n E id + F | (E) | id F id * E | id n E id + F id + id * E 9

Parse Tree n A labeled tree in which n n the interior nodes are

Parse Tree n A labeled tree in which n n 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 id F + id CFG * E id 10

Parse Trees and Derivations E E 2 + id 1 E E E 4

Parse Trees and Derivations E E 2 + id 1 E E E 4 3 E * 5 id id Preorder numbering E E id 5 1 E + E 4 * 2 E 3 id id Reverse or postorder numbering CFG E+E id + E * E id + id * id E E+E E + E * id E + id * id (1( (2( (3( (4( (5( 11

Grammar: Example <St> : : = | s; <St> | { <St> } <St>

Grammar: Example <St> : : = | s; <St> | { <St> } <St> { { <St> } <St> n s; { { s; <St> } <St> { { s; { <St> } <St>} <St> n More than one { { s; <St> } <St>} <St> statement: { { s; s; } <St>} <St> n s; s; s; { { s; s; } s; <St> } <St> n A statement can be a { { s; s; } s; { <St> } block of statements. <St>} <St> { { s; s; } s; { } <St>} n {s; s; s; } <St> Is the following correct? { { s; s; } s; { } } <St> { {s; s; } s; {}} s; } { { s; s; } s; { } } s; } <St> { { s; s; } s; { } } s; } List of statements: n No statement n One statement: CFG 12

Abstract Syntax Tree Representation of actual source tokens n Interior nodes represent operators. n

Abstract Syntax Tree Representation of actual source tokens n Interior nodes represent operators. n Leaf nodes represent operands. n CFG 13

Abstract Syntax Tree for Expression E E id 1 E + E id 2

Abstract Syntax Tree for Expression E E id 1 E + E id 2 CFG + * E * id 1 id 2 id 3 14

Abstract Syntax Tree for If Statement st if. Statement if ) exp true (

Abstract Syntax Tree for If Statement st if. Statement if ) exp true ( if st else. Part true else st return CFG 15

Ambiguous Grammar A grammar is ambiguous if it can generate two different parse trees

Ambiguous Grammar A grammar is ambiguous if it can generate two different parse trees for one string. n Ambiguous grammars can cause inconsistency in parsing. n CFG 16

Example: Ambiguous Grammar E E E E+E E-E E*E E/E id E E id

Example: Ambiguous Grammar E E E E+E E-E E*E E/E id E E id 1 E + E id 2 CFG E * E E E id 3 id 1 + * E E id 3 id 2 17

Ambiguity in Expressions n Which operation is to be done first? n solved by

Ambiguity in Expressions n Which operation is to be done first? n solved by precedence An operator with higher precedence is done before one with lower precedence. n An operator with higher precedence is placed in a rule (logically) further from the start symbol. n n 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. ( n Right-associated : W + (X + (Y + Z(( n Left-associated : ((W + X) + Y) + Z n CFG 18

Precedence E E+E E E-E E E*E E E/E E id E E+E E

Precedence E E+E E E-E E E*E E E/E E id E E+E E E-E E F F F*F F F/F F id CFG E E E id 1 + E E * E E E + id 3 id 1 id 2 * E E id 3 id 2 E E E + F F id 1 F id 2 * F id 3 19

Precedence (cont’d( E E+E|E-E|F F F*F|F/F|X X ( E ) | id E F

Precedence (cont’d( E E+E|E-E|F F F*F|F/F|X X ( E ) | id E F X ) E )id 1 + id 2) * id 3 * id 4 E PEMDAS parentheses exponentiation multiplication/division addition/subtraction CFG + F * F F ( E F F X X id 1 id 2 * F X X id 3 id 4 20

Associativity Left-associative operators E E+F|E-F|F F F*X|F/X|X X ( E ) | id E

Associativity Left-associative operators E E+F|E-F|F F F*X|F/X|X X ( E ) | id E n F / F F * X X ) E )id 1 + id 2) * id 3 / id 4 ))) =id 1 + id 2) * id 3) / id 4( E + X id 4 id 3 ( F F X X id 2 id 1 CFG 21

Ambiguity in Dangling Else St If. St. . . | If. St if (

Ambiguity in Dangling Else St If. St. . . | If. St if ( E ) St | if ( E ) St else St E 0 | 1… | if (0( if (1) St else St If. St if ) E ( 0 if ) If. St if ) St E ( 0 If. St E ( 1 CFG if (0( if (1) St else St if St else St If. St ) E ( St 1 22

Disambiguating Rules for Dangling Else St Most Closely Nested Rule Matched. St | Unmatched.

Disambiguating Rules for Dangling Else St Most Closely Nested Rule Matched. St | Unmatched. St if (E) St | St if (E) Matched. St else Unmatched. St Matched. St if (E) Matched. St else Matched. St | Unmatched. St. . . E if ) E ( St 1|0 Matched. St n if (0) if (1) St else St = if (0 ( if ) E ( Matched. St else Matched. St if (1) St else St CFG 23

Syntax Diagrams n Graphical representation of EBNF rules n n Examples n n n

Syntax Diagrams n Graphical representation of EBNF rules n n Examples n n n CFG nonterminals : If. St id terminals: sequences and choices: X : : = (E) | id ) id St Seq : : = {St ; } St E : : = F [+ E [ ( E ; F St + E 24