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

sliek môn máy tự động chương 5 context-free grammar

36 150 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

Định dạng
Số trang 36
Dung lượng 100,67 KB

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

Nội dung

Chapter 5: Context-Free GrammarNovember 9, 2009 Chapter 5: Context-Free Grammar... Leftmost and Rightmost DerivationsLeftmost: in each step the leftmost variable in the sententialform is

Trang 1

Chapter 5: Context-Free Grammar

November 9, 2009

Chapter 5: Context-Free Grammar

Trang 2

Non-regular Languages

Not all languages are regular

L1 = {anbn: n ≥ 0} is not regular

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

Some properties of programming languages are not regular

Chapter 5: Context-Free Grammar

Trang 4

CFG and Regular Language

A regular language is also a context-free language

Why the name?

Chapter 5: Context-Free Grammar

Trang 9

G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B →

Bb, B → λ})

Chapter 5: Context-Free Grammar

Trang 10

Leftmost and Rightmost Derivations

Leftmost: in each step the leftmost variable in the sententialform is replaced

Rightmost: in each step the rightmost variable in the

sentential form is replaced

Chapter 5: Context-Free Grammar

Trang 11

Example 5.5

G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B →

Bb, B → λ})

Chapter 5: Context-Free Grammar

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 aderivation treeiff:

1 The root is labeled S

2 Every leaf has a label from T ∪ {λ}

3 Every interior vertex has a label from V

4 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.

5 A leaf labeled λ has no siblings.

Chapter 5: Context-Free Grammar

Trang 14

Partial Derivation Tree

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

An ordered tree is apartial derivation tree iff:

1 The root is labeled S (not need).

2 Every leaf has a label from V ∪ T ∪ {λ}.

3 Every interior vertex has a label from V

4 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.

5 A leaf labeled λ has no siblings.

Chapter 5: Context-Free Grammar

Trang 15

Yield of a Tree

The string of symbols obtained by reading the leaves of a tree fromleft to right (omitting any λ’s encountered) is called theyieldofthe tree

Chapter 5: Context-Free Grammar

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 t G is a partial derivation tree of G whose root label is S, then the yield of tG is a sentential form of G.

Chapter 5: Context-Free Grammar

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

Chapter 5: Context-Free Grammar

Trang 21

Exhaustive Search Parsing

Top-down parsing

S → SS | aSb | bSa | λw = aabb

Chapter 5: Context-Free Grammar

Trang 22

Exhaustive Search Parsing Disadvantages

Trang 23

Restricted Grammar for Exhaustive Search Parsing

Suppose G = (V, T, S, P) is a context-free grammar which

L(G) or not

Chapter 5: Context-Free Grammar

Trang 24

Theorem 5.1

For every context-free grammar G, there exists an algorithmthat parses any w ∈ L(G) in a number of steps proportional to

|w |3

Not satisfactory as linear timeparsing algorithm (proportional

to the length of a string)

Chapter 5: Context-Free Grammar

Trang 25

Simple Grammars (S-Grammars)

Trang 26

Parsing in Simple Grammars (S-Grammars)

w = a1a2 an

S ⇒ a1A1A2 Am ⇒a1a2B1B2 BkA1A2 Am ⇒

Chapter 5: Context-Free Grammar

Trang 27

S-Grammars and Contemporary Programming Languages

Many features of Pascal-like programming languages can be

expressed with s-grammars

Chapter 5: Context-Free Grammar

Trang 28

Definition

Chapter 5: Context-Free Grammar

Trang 31

Unambiguous Grammar

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

unambiguous grammar, then L is said to be unambiguous

Chapter 5: Context-Free Grammar

Trang 32

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 iscalledinherently ambiguous

Chapter 5: Context-Free Grammar

Trang 34

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 morecomplicated aspects

Chapter 5: Context-Free Grammar

Trang 35

Example 5.14

S-grammars:

<if-statement> ::= if <expression> <then clause>

<else clause>

<then clause> ::= then <statement>

<else clause> ::= else <statement>

Chapter 5: Context-Free Grammar

Trang 36

Exercises: 2, 3, 4, 6, 7, 9, 15, 17, 22 of Section 5.1 - Linzsbook

Exercises: 1, 2, 5, 6, 8, 10, 11, 12, 13, 15 of Section 5.2 Linzs book

-Exercises: 1, 2, 3 of Section 5.3 - Linzs book

Chapter 5: Context-Free Grammar

Ngày đăng: 23/10/2014, 17:47

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w