Download presentation
Presentation is loading. Please wait.
Published byAlexander Collins Modified over 8 years ago
1
Chapter 4. Syntax Analysis (1)
2
2 Application of a production A in a derivation step i i+1
3
3 Formal grammars (1/3) Example : Let G 1 have N = {A, B, C}, T = {a, b, c} and the set of productions ACB BC A aABCbB bb A abCbC bc cC cc The reader should convince himself that the word a k b k c k is in L(G 1 ) for all k 1 and that only these words are in L(G 1 ). That is, L(G 1 ) = { a k b k c k | k 1}.
4
4 Formal grammars (2/3) Example : Grammar G 2 is a modification of G 1 : G 2 : ACB BC A aABCbB bb A abCbC b The reader may verify that L(G 2 ) = { a k b k | k 1}. Note that the last rule, bC b, erases all the C's from the derivation, and that only this production removes the nonterminal C from sentential forms.
5
5 Formal grammars (3/3) Example : A simpler grammar that generates { a k b k | k 1} is the grammar G 3 : G 3 : S S aSb S ab A derivation of a 3 b 3 is S aSb aaSbb aaabbb The reader may verify that L(G 3 ) = { a k b k | k 1}.
6
6 TypeFormat of ProductionsRemarks 0φAψ→ φω ψUnrestricted Substitution Rules 1φAψ→ φω ψ, ω≠λ ∑→λ Context Sensitive Context Free Right Linear Left Linear 2A →ω, ω≠λ ∑→λ 3A→aB A→a ∑→λ A→Ba A →a ∑→λ Regular Noncon- tracting Contracting The four types of formal grammars
7
7 Context-Sensitive Grammars(Type1 ) Definition : A context-sensitive grammar G = (N,T,P, ) is a formal grammar in which all productions are of the form φAψ→φωψ, ω≠ The grammar may also contain the production →, if G is a context-sensitive (type1) grammar, then L(G) is a context-sensitive (type1) language. Unrestricted Grammars(Type0 )
8
8 Context-Free Grammars (Type2) Definition : A context-free grammar G=(N,T,P, ) is a formal grammar in which all productions are of the form A→ω The grammar may also contain the production →λ. If G is a context-free (type2) grammar, then L(G) is a context-free (type2) language. A ∈ N ∪ { } ω ∈ (N ∪ T)* - {λ}
9
9 Regular Grammars (Type3) (1/2) Definition : A production of the form A→aB or A→a is called a right linear production. A production of the form A→Ba or A→a is a left linear production. A formal grammar is right linear if it contains only right linear productions, and is left linear if it contains only left linear production →λ. Left and right linear grammars are also known as regular grammars. If G is a regular (type3) grammar, then L(G) is a regular (type3) language. A ∈ N ∪ {∑} B ∈ N a ∈ T A ∈ N ∪ {∑} B ∈ N a ∈ T
10
10 Regular Grammars (Type3) (2/2) Example: A left linear grammar G 1 and a right linear grammar G 2 have productions as follows: G 1 : G 2 : The reader may verify that L(G 1 ) = (10)*1=1(01)*=L(G 2 ) ∑ → 1B ∑ → 1 A → 1B B → 0A A → 1 ∑ → B1 ∑ → 1 A → B1 B → A0 A → 1
11
11 Fig. 4.23. Operator-precedence relations. id+*$ ·>·> ·>·> ·>·> + <·<··>·><·<··>·> * <·<··>·> ·>·> ·>·> $ <·<· <·<· <·<·
12
12 Operator-Precedence Relations from Associativity and Precedence (1/2) 1. If operator θ 1 has higher precedence than operator θ 2, make θ 1 ·> θ 2 and θ 2 + and + <· *. These relations ensure that, in an expression of the form E+E*E+E, the central E*E is the handle that will be reduced first. 2. If θ 1 and θ 2 are operators of equal precedence (they may in fact be the same operator), then make θ 1 ·> θ 2 and θ 2 ·> θ 1 if the operators are left- associative, or make θ 1 +, + ·> -, - ·> - and - ·> +. If is right associative, then make <· . These relations ensure that E - E+E will have handle E - E selected and E E E will have the last E E selected.
13
13 Operator-Precedence Relations from Associativity and Precedence (2/2) 3.Make θ θ, θ θ, θ ·> ), θ ·> $, and $ <· θ for all operators θ. Also, let These rules ensure that both id and (E) will be reduced to E. Also, $ serves as both the left and right endmarker, causing handles to be found between $’s wherever possible. ( = )$ < · ( $ < · id ( < · ( id ·> $ ) ·> $ ( < · id id ·> ) ) ·> ) ·
14
14 + - */↑id()$ + ·>·> ·>·><·<· <·<· <·<· <·<· <·<··>·> ·>·> -·>·> ·>·><·<· <·<· <·<· <·<· <·<··>·> ·>·> * ·>·> ·>·> ·>·> ·>·><·<· <·<· <·<··>·> ·>·> / ·>·> ·>·> ·>·> ·>·><·<· <·<· <·<··>·> ·>·> ↑ ·>·> ·>·> ·>·> ·>·><·<· <·<· <·<··>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ( <·<· <·<· <·<· <·<· <·<· <·<· <·<· = ) ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> $ <·<· <·<· <·<· <·<· <·<· <·<· <·<· · Fig. 4.25. Operator-precedence relations.
15
15 Precedence Functions Example 4.29 The Precedence table of Fig. 4.25 has the following pair of precedence functions, For example, * g(id) suggests that id · > id; but, in fact, no precedence relation holds between id and id. Other error entries in Fig. 4.25 are similarly replaced by one or another precedence relation. + - */↑()id$ f224440660 g113355050
16
16 Fig. 4.26. Graph representing precedence functions.
17
17 id()$ e3 ·>·> ·>·> ( <·<· <·<· =e4 )e3 ·>·> ·>·> $ <·<· <·<· e2e1 · Fig. 4.28. Operator-precedence matrix with error entries
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.