Download presentation
Presentation is loading. Please wait.
Published byPosy Byrd Modified over 9 years ago
1
Parsing Context-Free Grammars, Parsing, Syntax Trees
2
Parsing Produce the parse tree for a given program (token stream): i := 1 ; sum := 0 ; while not i = 10 do { i = …
3
Parsing ; =; i1 = while sum0not ; == = = i10i+sum + i 1sumi
4
Recursive-Descent Parsing Using a stack, match grammar symbols (terminal and nonterminal) with the tokens in the stream: i = 1 ; sum = 0 ; while not … C_C_
5
Recursive-Descent Parsing Beginning with the grammar’s “start symbol”, pop the next stack symbol and try to match it with the next token in the stream i = 1 ; sum = 0 ; while not … C_C_
6
Recursive-Descent Parsing If they don’t match, expand the symbol by the appropriate grammar production and push those symbols onto the stack i = 1 ; sum = 0 ; while not … C ::= S ; C S;C_S;C_
7
Recursive-Descent Parsing Parse tree nodes corresponding to the new symbols will spawn as children of the popped symbol’s node … i = 1 ; sum = 0 ; while not … C ::= S ; C S;C_S;C_
8
Recursive-Descent Parsing C; S=C; S= C; i1 = while sum0not ; == = = i10i+sum + i 1sumi
9
Recursive-Descent Parsing Pop the next stack symbol and try to match it with the next token in the stream i = 1 ; sum = 0 ; while not … S;C_S;C_
10
Recursive-Descent Parsing If they don’t match, expand the symbol by the appropriate grammar production and push those symbols onto the stack i = 1 ; sum = 0 ; while not … S ::= id = num id = num ; C _
11
Recursive-Descent Parsing Parse tree nodes corresponding to the new symbols will spawn as children of the popped symbol’s node … i = 1 ; sum = 0 ; while not … S ::= id = num id = num ; C _
12
Recursive-Descent Parsing C; S=C; S= C; id inum 1 = while sum0not ; == = = i10i+sum + i 1sumi
13
Recursive-Descent Parsing Pop the next stack symbol and try to match it with the next token in the stream. If they match, eat the token… i = 1 ; sum = 0 ; while not … id = num ; C _
14
Recursive-Descent Parsing C; S=C; S= C; id inum 1 = while sum0not ; == = = i10i+sum + i 1sumi
15
Recursive-Descent Parsing Pop the next stack symbol and try to match it with the next token in the stream. If they match, eat the token = 1 ; sum = 0 ; while not … = num ; C _
16
Recursive-Descent Parsing C; S=C; S= C; id inum 1 = while sum0not ; == = = i10i+sum + i 1sumi
17
Recursive-Descent Parsing Pop the next stack symbol and try to match it with the next token in the stream. If they match, eat the token 1 ; sum = 0 ; while not i == … num ; C _
18
Recursive-Descent Parsing C; S=C; S= C; id inum 1 = while sum0not ; == = = i10i+sum + i 1sumi
19
Recursive-Descent Parsing Pop the next stack symbol and try to match it with the next token in the stream. If they match, eat the token ; sum = 0 ; while not i == … ;C_;C_
20
Recursive-Descent Parsing C; S=C; S= C; id inum 1 = while sum0not ; == = = i10i+sum + i 1sumi
21
Recursive-Descent Parsing Pop the next stack symbol and try to match it with the next token in the stream. sum = 0 ; while not i == 10 … C_C_
22
Recursive-Descent Parsing If they don’t match, expand the symbol by the appropriate grammar production and push those symbols onto the stack sum = 0 ; while not i == 10 … C := S ; C S;C_S;C_
23
Recursive-Descent Parsing Parse tree nodes corresponding to the new symbols will spawn as children of the popped symbol’s node … sum = 0 ; while not i == 10 … C := S ; C S;C_S;C_
24
Recursive-Descent Parsing C; =C; = C; i1 i1 S = C while sum0not ; == = = i10i+sum + i 1sumi
25
Recursive-Descent Parsing
26
Pop the next stack symbol and try to match it with the next token in the stream. If they match, eat the token + i } + id } _
27
Recusive-Descent Parsing ; =; i1 = while sum0not ; == = = i10i+sum + i 1sumi
28
Recursive-Descent Parsing Pop the next stack symbol and try to match it with the next token in the stream. If they match, eat the token i } id } _
29
Recusive-Descent Parsing ; =; i1 = while sum0not ; == = = i10i+sum + i 1sumi
30
Recursive-Descent Parsing If the symbol stack and token stream simultaneously become empty then the program successfully parsed. } }_}_
31
Which production? When we expand a nonterminal, how do we decide which production to use? i + 1 E ::= E + num|num E_E_
32
Which production? We must predict the structure of the program based on the next input token – predictive parsing. i + 1 E ::= E + num|num E_E_
33
Which production? If we have a choice of productions based on the next input token, the grammar belongs to a class that can’t be parsed by the predictive algorithm. i + 1 E ::= E + num|num E_E_
34
Unambiguous Arithmetic Grammar E::=E + T|T T::=T * F|F F ::=num|(E) We don’t know if an E will have 1 term or many terms. Should we expand E to E + T or T?
35
Unambiguous Arithmetic Grammar E::=E + T|T T::=T * F|F F ::=num|(E) E ::= T { + T }* T + T + T + … We do know that E must always start with T followed by 0 or more + T ‘s
36
Unambiguous Arithmetic Grammar E::=T E’| E + T|T E’ ::=+ T E’|ε T::=T * F|F F ::=num|(E) Solution: left-factor the T E’ is nullable and symbolizes an appended (+ T) Now, what about T and F ?
37
Unambiguous Arithmetic Grammar E::=T E’ E’ ::=+ T E’|ε T::=F T’|T * F|F T’ ::=* F T’| ε F ::=num|(E) The T/F interrelationship is isomorphic…
38
The “First” Set E::=T E’ E’ ::=+ T E’|ε T::=F T’ T’ ::=* F T’| ε F ::=num|(E) First (T E’ ) = { num, ( } First (+ T E’ ) = { + } First (F T’ ) = ?? …
39
Predictive Parsing Table +*num() ETE’TE’ E’+TE’ TFT’FT’ T’*FT’ Fnum(E) Next Token (terminal) in input Next symbol (non-terminal) on stack
40
Predictive Parsing Table Production under each terminal in its First set One entry per (stack symbol * input token), … otherwise?? Empty cells raise parsing error
41
But, not so fast… Since the null production will have an empty First set, it doesn’t appear in the table? (1 + 3) * 5 … E ::= T E’ E ::= T E’ E
42
But, not so fast… Since the null production will have an empty First set, it doesn’t appear in the table? 3) * 5 … num T’ E’ ) T’ E’
43
But, not so fast… Since the null production will have an empty First set, it doesn’t appear in the table? ) * 5 … Parse error? T’ E’ ) T’ E’
44
The “Follow” Set E::=T E’ E’ ::=+ T E’|ε T::=F T’ T’ ::=* F T’| ε F ::=num|(E) Follow ( E’ ) = { ) } Follow ( T’ ) = { ), + } Include a null production entry for each nullable nonterminal under all terminals in its Follow set …
45
Complete Predictive Parsing Table +*num() ETE’TE’ E’+TE’ε TFT’FT’ T’ε*FT’ε Fnum(E) Next Token (terminal) in input Next symbol (non-terminal) on stack
46
Recursive Descent Parsing C S ; C id := num ; C i := num ; C i := 1 ; :=; i1 := while sum0not ; =:=:= i10sum+i+ sumii1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.