• No results found

LL(1)

N/A
N/A
Protected

Academic year: 2020

Share "LL(1)"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

LL(1) Grammar

• A context-free grammar G = (V

T

, V

N

, S, P) whose

parsing table has no multiple entries is said to

be LL(1). In the name LL(1),

– the first

L

stands for scanning the input from

l

eft to

right,

– the second

L

stands for producing a

l

eftmost

derivation,

(3)

LL(1) Grammar

• A language is said to be LL(1) if it can be generated by a LL(1) grammar. It can be shown that LL(1) grammars are not ambiguous and

(4)

LL(1) Grammar

• A grammar G is LL(1) if and only if whenever A α | β

are two distinct productions of G the following conditions hold

• For no terminal a do both α and β derive string beginning with a.

• At most one of α and β can derive empty string

• If β=>ε then α does not derive any string beginning with a terminal in FOLLOW(A).Like wise α=> εthen β does not derive any string beginning with a terminal in

(5)

Table-Driven LL Parsing

To build the parsing table, we need three concepts:Nullability

(6)

Nullability

Definition (Nullable)

A nonterminal A is nullable if

(7)

Nullability

Clearly, A is nullable if it has a production

A → ε.

But A is also nullable if there are, for example, productions

A. → BC

B. → A | aC | ε

C. → aB | Cb |

(8)

Nullability

In other words, A is nullable if there is a production

A → ε,

or there is a production

A B1B2 . . .

Bn,

(9)

Example

Example (Nullability) In the grammar

ET E

E+ T E | ε

TF T

T

F

* F T′ | ε

(E) | id |

num

Eand Tare nullable.

(10)

Example

Example (Nullability)

Nonterminal Nullable

E No

E′ Yes

T No

T′ Yes

(11)

FIRST and FOLLOW

Definition (FIRST)

FIRST(α) is the set of all terminals that may appear as the first symbol in a replacement string of α.

Definition (FOLLOW)

FOLLOW(α) is the set of all terminals that may follow α in a derivation.

Given a grammar G, we may define the functions FIRST and FOLLOW on the strings of symbols of

(12)

FIRST

For a grammar symbol X, FIRST(X) is computed as follows.

For every terminal X, FIRST(X) = {X}. For every nonterminal X, if

X Y1Y2 . . . Yn

is a production, then FIRST(Y1) ⊆ FIRST(X).

Furthermore, if Y1, Y2, . . . , Yk are nullable, then

(13)

FIRST

We are concerned with FIRST(X) only for the nonterminals of the grammar.

FIRST(X) for terminals is trivial.

(14)

Example

Example (FIRST) Let the grammar be

ET E

E → + T E | ε

TF T

T

* F T′ | ε

F → (E) | id |

num

(15)

Example

Example (FIRST) Find FIRST(E).

E occurs on the left in only one production

ET E

Therefore, FIRST(T) ⊆ FIRST(E).

Furthermore, T is not nullable. Therefore, FIRST(E) = FIRST(T).

(16)

Example

Example (FIRST) Find FIRST(T).

T occurs on the left in only one production

T

F T

Therefore,

FIRST(F) ⊆ FIRST(T). Furthermore, F is not nullable.

Therefore,

(17)

Example

Example (FIRST) Find FIRST(F).

FIRST(F) = {(, id,

num}.

Therefore,

FIRST(E) = {(, id,

num}.

FIRST(T) = {(, id,

(18)

Example

Example (FIRST) Find FIRST(E).

FIRST(E) =

{+}. Find FIRST(T).

FIRST(T) =

(19)

Example

Example (FIRST)

Nonterminal Nullable FIRST

E No {(, id, num}

E′ Yes {+}

T No {(, id, num}

T′ Yes {*}

(20)

FOLLOW

For a grammar symbol X, FOLLOW(X) is defined as

follows.

If S is the start symbol, then $ ∈

FOLLOW(S). If A → αBβ is a production, then

FIRST(β) ⊆ FOLLOW(B).

If A → αB is a production, or A → αBβ is a production and β is nullable, then

(21)

FOLLOW

We are concerned about FOLLOW(X) only for the nonterminals of the grammar.

(22)

Example

Example (FOLLOW) Let the grammar be

ET E

E → + T E | ε

TF T

T

* F T′ | ε

F → (E) | id |

num

(23)

Example

Example (FOLLOW) Find FOLLOW(E).

E is the start symbol, therefore

$ ∈ FOLLOW(E).

E occurs on the right in only one production.

F → (E)

Therefore

(24)

Example

Example (FOLLOW) Find FOLLOW(E).

Eoccurs on the right in two productions.

ET E

E+ T E

Therefore,

FOLLOW(E) = FOLLOW(E) =

(25)

Example

Example (FOLLOW) Find FOLLOW(T).

T occurs on the right in two productions.

ET E

E+ T E

Therefore, FOLLOW(T) ⊇ FIRST(E) = {+}.

However, Eis nullable, therefore it also contains

FOLLOW(E) = {$, )} and FOLLOW(E) =

{$, )}. Therefore,

(26)

Example

Example (FOLLOW) Find FOLLOW(T).

Toccurs on the right in two productions.

TF T

T

* F T′ Therefore,

(27)

Example

Example (FOLLOW) Find FOLLOW(F).

F occurs on the right in two productions.

TF T

T

* F T.

Therefore,

FOLLOW(F) ⊇ FIRST(T) = { *}.

However, Tis nullable, therefore it also contains

FOLLOW(T) = {+, $, )} and FOLLOW(T) = {$, ), +}.

Therefore,

(28)

Example

Example (FOLLOW)

Nonterminal Nullable FIRST FOLLOW

E No {(, id, num} {$, )}

E′ Yes {+} {$, )}

T No {(, id, num} {$, ), +}

T′ Yes {*} {$, ), +}

F No {(, id, num} {

*, $, ),

(29)

Parsing table

• For each production Aα of the grammar doo the following

• For each terminal a in FIRST(A),add A α to M[A, a]

• If ε is in FIRST (α) then for each terminal b in

(30)

Parsing table

Non Terminals

Terminals

id num + * ( ) $

E ETE’ ETE’ ETE’

E’ E’+TE’ E’ε E’ε

T TFT’ TFT’ TFT’

T’ T’ε T’FT’ T’ε T’ε

References

Related documents

No matter which symbols surround it, the single nonterminal on the left hand side can always be replaced by the right hand side. Types

2 Grammar G is non-terminally separable (Clark, 2006)... The E R -parsing- preserving property allows us to define a class of grammars that form a complete grammar lattice used

The attacks on the hash functions MD5, SHA-0 and SHA-1 may show that these hash functions no longer follow any of the list of the statements given in Section 1 as collision

CLINK automatically searches the library after all other CRL files given on the command line have been searched once; thus, any functions you explicitly define

functions combine to define a single mixture preserving utility function on. the union of the

Directions (11 to 15) : Study the given information carefully and answer the questions that follow :  Study the given information carefully and answer the questions that follow : 

Assuming the parameters m and tz to be independent and to follow specific distribution functions as well as the usual independence of frequency and severity given these

Actually, Onto2DSL generator will produce grammar symbols (and respective derivation rules) for all the concepts present in the given domain (because all of them are present in