1. Trang chủ
  2. » Công Nghệ Thông Tin

Chapter 5: Context - Free Grammar

37 388 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Context-Free Grammar
Tác giả Quan Thanh Tho
Trường học Hochiminh City University of Technology
Chuyên ngành Computer Science
Thể loại Bài báo
Thành phố Ho Chi Minh City
Định dạng
Số trang 37
Dung lượng 92 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Leftmost and Rightmost Derivations• Leftmost: in each step the leftmost variable in the sentential form is replaced.. • Rightmost: in each step the rightmost variable in the sentential

Trang 1

Chapter 5: Context-Free Grammar

Quan Thanh Tho qttho@cse.hcmut.edu.vn

Trang 2

Non-regular Languages

• Not all languages are regular.

• L1 = {anbn | n  0} is not regular.} is not regular.

L2 = { () , (()) , ((())) , } is not regular.

some properties of programming languages

Trang 4

CFG and Regular Language

• A regular language is also a context-free language.

Trang 9

• G = ({S, A, B}, {a, b}, S, {S  AB, A  aaA, A  , B  Bb, B  })

S  AB  aaAB  aaB  aaBb  aab

S  AB  ABb  aaABb  aaAb  aab

Trang 10

Leftmost and Rightmost Derivations

• Leftmost: in each step the leftmost variable in the

sentential form is replaced.

• Rightmost: in each step the rightmost variable in the sentential form is replaced.

Trang 11

Example 5.5

• G = ({S, A, B}, {a, b}, S, {S  AB, A  aaA, A  , B  Bb, B  })

S  AB  aaAB  aaB  aaBb  aab leftmost

S  AB  ABb  aaABb  aaAb  aab

Trang 12

Ordered Tree for a Production

Trang 13

Derivation Tree

• Let G = (V, T, S, P) be a context-free grammar

An ordered tree is a derivation tree iff:

1 The root is labeled S

2 Every leaf has a label from T  {}

3 Every interior vertex has a label from V

4 A vertex has label AV and its children are labeled a 1 , a 2 , , a n iff

P contains the production A  a 1 a 2 a n

Trang 14

Partial Derivation Tree

• Let G = (V, T, S, P) be a context-free grammar

1 The root is labeled S

2 Every leaf has a label from T  {}

Every leaf has a label from V  T  {}

1 Every interior vertex has a label from V

2 A vertex has label AV and its children are labeled a1, a2, , an iff

P contains the production A  a1 a2 an.

Trang 15

Yield of a Tree

The string of symbols obtained by reading the leaves of a tree from left to right (omitting any 's encountered) is called the s encountered) is called the yield of the tree

Trang 18

Sentential Forms & Derivation Trees

• Let G = (V, T, S, P) be a context-free grammar.

 wL(G) iff there exists a derivation tree of G whose yield is w

 If tG is a partial derivation tree of G whose root label is S,

then the yield of tG is a sentential form of G

Trang 19

Membership and Parsing

• Membership algorithm: tells if wL(G).

• Parsing: finding a sequence of productions by which wL(G) is derived.

Trang 20

Example 5.8

Trang 21

Exhaustive Search Parsing

• Top-down parsing

• S  SS | aSb | bSa |  w = aabb

1 S  SS S  SS  SSS S  aSb  aSSb

2 S  aSb S  SS  aSbS S  aSb  aaSbb

3 S  bSa S  SS  bSaS S  aSb  abSab

4 S   S  SS  S S  aSb  ab

Trang 22

Exhaustive Search Parsing

Trang 23

Restricted Grammar for Exhaustive

Trang 24

Theorem 5.1

• For every context-free grammar G, there exists an

algorithm that parses any wL(G) in a number of steps

proportional to |w|3.

• Not satisfactory as linear time parsing algorithm (proportional

to the length of a string)

Trang 25

Simple Grammars (S-Grammars)

G = (V, T, S, P)

Productions are of the form:

A  ax

A  V, a  T, and x  V*, and any pair (A,a) can occur

in at most one rule

Trang 26

Parsing in Simple Grammars

(S-Grammars)

Any wL(G) can be parsed in at most |w| steps.

w = a1a2 an

S  a1A1A2 Am  a1a2B1B2 BkA1A2 Am

Trang 27

S-Grammars and Contemporary

Programming Languages

Many features of Pascal-like programming languages can

be expressed with s-grammars.

Trang 28

A context-free grammar G is said to be ambiguous if there exits some wL(G) that has at least two distinct derivation trees

Trang 30

Example 5.10} is not regular.

• G = ({E, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c

Trang 32

Unambiguous Grammar

If L is a context-free language for which there exists an

unambiguous grammar , then L is said to be unambiguous

Trang 33

Inherently Ambiguous Grammar

If L is a context-free language for which there exists an

unambiguous grammar , then L is said to be unambiguous

If every grammar that generates L is ambiguous, then L is called inherently ambiguous

Trang 35

CFGs and Programming Languages

• Important uses of formal languages:

 to define precisely a programming language

 to construct an efficient translator for it

• RLs are used for simple patterns, while CFGs for more

complicated aspects

Trang 36

Example 5.14

• S-grammars:

<if-statement> ::= if <expression> <then_clause> <else_clause>

<then_clause> ::= then <statement>

<else_clause> ::= else <statement>

Trang 37

• Exercises: 2, 3, 4, 6, 7, 9, 15, 17, 22 of Section 5.1 - Linz’s book.

• Exercises: 1, 2, 5, 6, 8, 10} is not regular., 11, 12, 13, 15 of Section 5.2 - Linz’s book.

• Exercises: 1, 2, 3 of Section 5.3 - Linz’s book.

Ngày đăng: 13/05/2014, 10:21

TỪ KHÓA LIÊN QUAN