Create a Context free Grammar that generates all strings in Σ*

1.4k Views Asked by At

Consider the alphabet Σ = {∅, ε, a, b,(,), +, *}. Provide a context-free grammar that generates all strings in Σ? that are valid regular expressions. For example, it should be able to generate aaa? and a(b + ε) but not () or ))bc+.

The parts of a context free grammar are the following:

• a finite set of rules. each rule maps a variable to some string of variables and alphabet symbols: P ⊆ V × (V ∪ Σ)*

• an alphabet of symbols Σ. these are also called terminals.

• a set of variables V .

• S ∈ V , the starting variable.

A context free grammar is a 4-tuple G = (V, Σ, P, S)

I have tried this question but confused on the steps on creating this Grammar

1

There are 1 best solutions below

0
On

Think about how regular expressions over an alphabet $\Sigma$ are defined. It is probably something along the lines of this:

  • The string $\varnothing$ is a regular expression (for the empty language),
  • The string $\varepsilon$ is a regular expression (for the language with only the empty string),
  • For each symbol $x\in\Sigma$ the string $x$ is a regular expression,
  • If $A$ and $B$ are regular expressions, then $AB$ is a regular expression,
  • If $A$ and $B$ are regular expressions, then $A+B$ is a regular expression,
  • If $A$ is a regular expressions, then $A^*$ is a regular expression,
  • If $A$ is a regular expression, then $(A)$ is a regular expression,
  • Nothing else is a regular expression.

So, if we want a CFG that recognises all strings that are valid expression over some alphabet $\Sigma$, we need to incorporate each of these rules as an appropriate rule in the grammar.

For example, if $S$ is the start variable of the CFG, then the first of the above points is simply the rule $S\to\varnothing$, the fourth would be $S\to SS$ and the fifth would be $S\to S+S$, etc.