CSC262 Ā· 2081 Ā· 5 marks
Define the language of a grammar. For the grammar , show the leftmost derivation for the string 00100 with its parse tree. Sā0S0ā£1ā£ĪµS \rightarrow 0S0 \mid 1 \mid \varepsilonSā0S0ā£1ā£Īµ
Define the language of a grammar. For the grammar , show the leftmost derivation for the string 00100 with its parse tree. Sā0S0ā£1ā£ĪµS \rightarrow 0S0 \mid 1 \mid \varepsilonSā0S0ā£1ā£Īµ[5]
Language of a Grammar and Leftmost Derivation of 00100
Given
- Grammar $G$ with productions: $$S \rightarrow 0S0 \mid 1 \mid \varepsilon$$
- Start symbol: $S$
- Terminals: ${0, 1}$
- Non-terminal: ${S}$
- Target string: $00100$
Solution
Definition: Language of a Grammar
For a grammar $G = (V, T, P, S)$ where $V$ is the set of variables (non-terminals), $T$ the set of terminals, $P$ the set of productions, and $S$ the start symbol, the language of the grammar, written $L(G)$, is the set of all terminal strings derivable from $S$:
$$L(G) = {, w \in T^* \mid S \Rightarrow^* w ,}$$
where $\Rightarrow^*$ denotes derivation in zero or more steps. Only strings consisting entirely of terminals belong to $L(G)$.
Leftmost Derivation of 00100
In a leftmost derivation, the leftmost non-terminal is expanded at each step. Since there is only one non-terminal $S$, this is straightforward.
$$S \Rightarrow 0S0 \quad (S \rightarrow 0S0)$$ $$\Rightarrow 00S00 \quad (S \rightarrow 0S0)$$ $$\Rightarrow 00100 \quad (S \rightarrow 1)$$
Complete derivation:
$$S \Rightarrow 0S0 \Rightarrow 00S00 \Rightarrow 00100$$
Therefore $00100 \in L(G)$.
Verification of leaf string: outer pair $0\ldots0$, inner pair $0\ldots0$, centre $1$ gives $0,0,1,0,0 = 00100$ ā
Parse Tree for 00100
S
/ | \
0 S 0
/ | \
0 S 0
|
1
Reading the leaves left to right: $0,\ 0,\ 1,\ 0,\ 0 = 00100$ ā
Summary Table
| Step | Sentential Form | Production |
|---|---|---|
| 1 | $S$ | start |
| 2 | $0S0$ | $S \rightarrow 0S0$ |
| 3 | $00S00$ | $S \rightarrow 0S0$ |
| 4 | $00100$ | $S \rightarrow 1$ |
The string $00100$ is derived in 3 steps, confirming $00100 \in L(G)$.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.