Chapter 4. Syntax Analysis (1) - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 4. Syntax Analysis (1)

Description:

Chapter 4. Syntax Analysis (1) – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 18
Provided by: 4518
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4. Syntax Analysis (1)


1
Chapter 4.Syntax Analysis (1)
2
Application of a production ?A????? in a
derivation step ?i ? ?i1
3
Formal grammars (1/3)
  • Example Let G1 have N A, B, C, T a, b,
    c and the set of productions
  • ? ? A CB ? BC
  • A ? aABC bB ? bb
  • A ? abC bC ? bc
  • cC ? cc
  • The reader should convince himself that the word
    akbkck is in L(G1) for all k ? 1 and that only
    these words are in L(G1). That is,
  • L(G1) akbkck k ? 1.

4
Formal grammars (2/3)
  • Example Grammar G2 is a modification of G1
  • G2 ? ? A CB ? BC
  • A ? aABC bB ? bb
  • A ? abC bC ? b
  • The reader may verify that L(G2) akbk 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
Formal grammars (3/3)
  • Example A simpler grammar that generates akbk
    k ? 1 is the grammar G3
  • G3 ? ? S
  • S ? aSb
  • S ? ab
  • A derivation of a3b3 is
  • ? ? S ? aSb ? aaSbb ? aaabbb
  • The reader may verify that L(G3) akbk k ?
    1.

6
Type Format of Productions Remarks
0 fA?? f? ? Unrestricted Substitution Rules
1 fA?? f? ?, ??? ??? Context Sensitive Context Free Right Linear Left Linear
2 A ??, ??? ??? Context Sensitive Context Free Right Linear Left Linear
3 A?aB A?a ??? A?Ba A ?a ??? Context Sensitive Context Free Right Linear Left Linear
Contracting
Noncon- tracting
Regular
The four types of formal grammars
7
Context-Sensitive Grammars(Type1)
Unrestricted Grammars(Type0)
  • Definition A context-sensitive grammar G
    (N,T,P,?) is a formal grammar in which all
    productions are of the form
  • fA??f??, ?? ?
  • The grammar may also contain the production ?
    ??, if G is a context-sensitive (type1) grammar,
    then L(G) is a context-sensitive (type1) language.

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
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
Regular Grammars (Type3) (2/2)
  • Example A left linear grammar G1 and a right
    linear grammar G2 have productions as follows
  • G1 G2
  • The reader may verify that
  • L(G1) (10)11(01)L(G2)

? ? 1B ? ? 1 A ? 1B B ? 0A A ? 1
? ? B1 ? ? 1 A ? B1 B ? A0 A ? 1
11
Fig. 4.23. Operator-precedence relations.
id
id gt gt gt
lt gt lt gt
lt gt gt gt
lt lt lt
12
Operator-Precedence Relations from Associativity
and Precedence (1/2)
  • 1. If operator ?1 has higher precedence than
    operator ?2, make ?1 gt ?2 and ?2 lt ?1 . For
    example, if has higher precedence than , make
    gt and lt . These relations ensure that,
    in an expression of the form EEEE, the central
    EE 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 gt ?2 and ?2 gt ?1 if the
    operators are left-associative, or make ?1 lt ?2
    and ?2 lt ?1 if they are right-associative. For
    example, if and are left-associative, then
    make gt , gt -, - gt - and - gt . If ? is
    right associative, then make ? lt ?. These
    relations ensure that E-EE will have handle E-E
    selected and E?E?E will have the last E?E
    selected.

13
Operator-Precedence Relations from Associativity
and Precedence (2/2)
  • 3. Make ? lt id, id gt ?, ? lt (, ( lt ? , ) gt
    ?, ? gt ), ? gt , and lt ? 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.


( ) lt ( lt id
( lt ( id gt ) gt
( lt id id gt ) ) gt )
14
- / ? id ( )
gt gt lt lt lt lt lt gt gt
- gt gt lt lt lt lt lt gt gt
gt gt gt gt lt lt lt gt gt
/ gt gt gt gt lt lt lt gt gt
? gt gt gt gt lt lt lt gt gt
id gt gt gt gt gt gt gt
( lt lt lt lt lt lt lt
) gt gt gt gt gt gt gt
lt lt lt lt lt lt lt

Fig. 4.25. Operator-precedence relations.
15
Precedence Functions
  • Example 4.29 The Precedence table of Fig. 4.25
    has the following pair of precedence functions,
  • For example, lt id and, f() lt g(id). Note
    that f(id) gt g(id) suggests that id gt 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
f 2 2 4 4 4 0 6 6 0
g 1 1 3 3 5 5 0 5 0
16
Fig. 4.26. Graph representing precedence
functions.
17
Fig. 4.28. Operator-precedence matrix with error
entries
id ( )
id e3 e3 gt gt
( lt lt e4
) e3 e3 gt gt
lt lt e2 e1
Write a Comment
User Comments (0)
About PowerShow.com