Some example derivations

10.1.2 Some example derivations

  This section gives three examples in which the stack machine for parsing is applied. It turns out that, for all three examples, the nondeterministic stack machine can

  10.1 LL Parsing: Background

  act in a deterministic way by looking ahead one (or two) symbols of the sequence of input symbols. Each of the examples exemplifies why different kinds of grammar analyses are useful in parsing.

  The first example Our first example is gramm1. The set of terminal symbols of gramm1 is {a, b, c}, the

  set of nonterminal symbols is {S, A, B, C}, the start symbol is S; and the productions are

  C → aS | ba We want to know whether or not the string ccccba is a sentence of the language of

  this grammar. The stack machine produces, amongst others, the following sequence, corresponding with a leftmost derivation of ccccba.

  stack

  input S ccccba cA ccccba

  A cccba

  cBC

  cccba

  BC ccba

  Starting with S the machine chooses between two productions:

  S → cA | b but, since the first symbol of the string ccccba to be recognised is c, the only

  applicable production is the first one. After expanding S a match-action removes the leading c from ccccba and cA. So now we have to derive the string cccba from

  A. The machine chooses between three productions:

  A → cBC | bSA | a and again, since the next symbol of the remaining string cccba to be recognised is

  c, the only applicable production is the first one. After expanding A a match-action removes the leading c from cccba and cBC. So now we have to derive the string ccba from BC. The top stack symbol of BC is B. The machine chooses between two productions:

  B → cc | Cb

  LL Parsing

  The next symbol of the remaining string ccba to be recognised is, once again, c. The first production is applicable, but the second production may be applicable as well. To decide whether it also applies we have to determine the symbols that can appear as the first element of a string derived from B starting with the second production. The first symbol of the alternative Cb is the nonterminal C. From the productions

  C → aS | ba it is immediately clear that a string derived from C starts with either an a or a b.

  The set {a, b} is called the first set of the nonterminal C. Since the next symbol in the remaining string to be recognised is a c, the second production cannot be applied. After expanding B and performing two match-actions it remains to derive the string ba from C. The machine chooses between two productions C → aS and

  C → ba. Clearly, only the second one applies, and, after two match-actions, leads to success.

  From the above derivation we conclude the following.

  Deriving the sentence ccccba using gramm1 is a deterministic computa- tion: at each step of the derivation there is only one applicable alternative for the nonterminal on top of the stack.

  Determinicity is obtained by looking at the first set of the nonterminals. The second example

  A second example is the grammar gramm2 whose productions are

  S → abA | aa

  A → bb | bS Now we want to know whether or not the string abbb is a sentence of the language of

  this grammar. The stack machine produces, amongst others, the following sequence

  stack input S abbb abA abbb bA bbb

  A bb

  b

  Starting with S the machine chooses between two productions:

  S → abA | aa since both alternatives start with an a, it is not sufficient to look at the first symbol a

  of the string to be recognised. The problem is that the lookahead sets (the lookahead set of a production N → α is the set of terminal symbols that can appear as the first symbol of a string that can be derived from N starting with the production N → α, the definition is given in the following subsection) of the two productions for S both

  10.1 LL Parsing: Background

  contain a. However, if we look at the first two symbols ab, then we find that the only applicable production is the first one. After expanding and matching it remains to derive the string bb from A. Again, looking ahead one symbol in the input string does not give sufficient information for choosing one of the two productions

  A → bb | bS for A. If we look at the first two symbols bb of the input string, then we find that

  the first production applies (and, after matching, leads to success). Each string derived from A starting with the second production starts with a b and, since it is not possible to derive a string starting with another b from S, the second production does not apply.

  From the above derivation we conclude the following.

  Deriving the string abbb using gramm2 is a deterministic computation: at each step of the derivation there is only one applicable alternative for the nonterminal on the top of the stack.

  Again, determinicity is obtained by analysing the first set (of strings of length 2) of the nonterminals. Alternatively, we can left-factor the grammar to obtain a grammar in which all productions for a nonterminal start with a different terminal symbol.

  The third example

  A third example is grammar gramm3 with the following productions:

  S → AaS | B

  A → cS |

  B →b Now we want to know whether or not the string acbab is an element of the language

  of this grammar. The stack machine produces the following sequence

  stack

  input S acbab AaS acbab aS acbab

  Starting with S the machine chooses between two productions:

  S → AaS | B since each nonempty string derived from A starts with a c, and each nonempty string

  derived from B starts with a b, there does not seem to be a candidate production to start a leftmost derivation of acabb with. However, since A can also derive the empty string, we can apply the first production, and then apply the empty string for A, producing aS which, as required, starts with an a. We do not explain the rest of the leftmost derivation since it does not use any empty strings any more.

  Nonterminal symbols that can derive the empty sequence will play a central role in the grammar analysis problems which we will consider in section 10.2.

  From the above derivation we conclude the following.

  LL Parsing

  Deriving the string acbab using gramm3 is a deterministic computation: at each step of the derivation there is only one applicable alternative for the nonterminal on the top of the stack.

  Determinicity is obtained by analysing whether or not nonterminals can derive the empty string, and which terminal symbols can follow upon a nonterminal in a deriva- tion.

Dokumen yang terkait

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 819 7

ANTARA IDEALISME DAN KENYATAAN: KEBIJAKAN PENDIDIKAN TIONGHOA PERANAKAN DI SURABAYA PADA MASA PENDUDUKAN JEPANG TAHUN 1942-1945 Between Idealism and Reality: Education Policy of Chinese in Surabaya in the Japanese Era at 1942-1945)

1 29 9

Improving the Eighth Year Students' Tense Achievement and Active Participation by Giving Positive Reinforcement at SMPN 1 Silo in the 2013/2014 Academic Year

7 202 3

Improving the VIII-B Students' listening comprehension ability through note taking and partial dictation techniques at SMPN 3 Jember in the 2006/2007 Academic Year -

0 63 87

The Correlation between students vocabulary master and reading comprehension

16 145 49

The correlation intelligence quatient (IQ) and studenst achievement in learning english : a correlational study on tenth grade of man 19 jakarta

0 57 61

An analysis of moral values through the rewards and punishments on the script of The chronicles of Narnia : The Lion, the witch, and the wardrobe

1 59 47

Improping student's reading comprehension of descriptive text through textual teaching and learning (CTL)

8 140 133

The correlation between listening skill and pronunciation accuracy : a case study in the firt year of smk vocation higt school pupita bangsa ciputat school year 2005-2006

9 128 37

Transmission of Greek and Arabic Veteri

0 1 22