Tutorial 5

For a production of the form A→αNβA\rightarrow\alpha N\beta Add First(β)−{ϵ}∈Follow(N)\text{First}(\beta)-\{\epsilon\}\in\text{Follow}(N)


Question 1

Consider the following grammar for possibly nested lists of numbers and identifiers enclosed in parentheses. The symbols NUM and ID are terminal symbols (lexical tokens) that matches numbers and identifiers, respectively.

lexp→atom | listatom→NUM ∣ IDlist→’(’ lexpSeq ’)’lexpseq→lexpSeq lexp | lexp \begin{aligned} \text{lexp}&\rightarrow\text{atom | list}\\ \text{atom}&\rightarrow\bold{NUM\ |\ ID}\\ \text{list}&\rightarrow\text{'(' lexpSeq ')'}\\ \text{lexpseq}&\rightarrow\text {lexpSeq lexp | lexp} \end{aligned}
  1. Remove any left recursion in the grammar

    To remove the left recursion from: A→Aα∣βA\rightarrow A\alpha|\beta Which matches a β\beta followed by one or more occurrences of α\alpha, one can re-write the production as follows:

    A→βA′A′→ϵ∣αA′A\rightarrow \beta A'\\A'\rightarrow \epsilon | \alpha A'

    • The left recursion in this grammar occurs in the production

      lexpseq→lexpSeq lexp | lexp\text{lexpseq}\rightarrow\text{lexpSeq lexp | lexp}

      • We resolve this by introducing a new non-terminal symbol, say lexpNext\text{lexpNext}, and a new production
      • Using the formula defined above, A = lexpseq, alpha=lexp, beta=lexp
      lexpseq→lexp lexpNextlexpNext→ϵ∣α lexpNext’\text{lexpseq}\rightarrow\text{lexp lexpNext}\\ \text{lexpNext}\rightarrow\epsilon|\alpha \text{ lexpNext'}
    • Finally, the productions are:

      lexp→atom | listatom→NUM ∣ IDlist→’(’ lexpSeq ’)’lexpseq→lexp lexpNextlexpNext→ϵ∣lexp lexpNext’\begin{aligned} \text{lexp}&\rightarrow\text{atom | list}\\ \text{atom}&\rightarrow\bold{NUM\ |\ ID}\\ \text{list}&\rightarrow\text{'(' lexpSeq ')'}\\ \text{lexpseq}&\rightarrow\text{lexp lexpNext}\\ \text{lexpNext}&\rightarrow\epsilon|\text{lexp lexpNext'} \end{aligned}
  2. Construct First\text{First} and Follow\text{Follow} sets for the non-terminals of the resulting grammar for the question above

    • The non-terminal symbols in the productions above are:
      {lexp, atom, list,lexpSeq, lexpNext}\{\text{lexp, atom, list,lexpSeq, lexpNext}\}
    First(NUM)=\def\first{\text{First}} \first(\bold{NUM})=
  3. d

lexp→atom | listatom→NUM ∣ IDlist→’(’ lexpSeq ’)’lexpseq→lexp lexpNextlexpNext→ϵ∣lexp lexpNext\begin{aligned} \text{lexp}&\rightarrow\text{atom | list}\\ \text{atom}&\rightarrow\bold{NUM\ |\ ID}\\ \text{list}&\rightarrow\text{'(' lexpSeq ')'}\\ \text{lexpseq}&\rightarrow\text{lexp lexpNext}\\ \text{lexpNext}&\rightarrow\epsilon|\text{lexp lexpNext} \end{aligned}
lexp NUM, ID NUM, ID NUM, ID
atom {NUM, ID} {NUM, ID} {NUM, ID} {NUM, ID}
list {’(’} {’(’} {’(’} {’(’}
lexpSeq NUM, ID NUM, ID, “(”
lexpNext ϵ\epsilon ϵ\epsilon ϵ\epsilon ϵ\epsilon

d.

The following productions are given from before

lexpseq→lexp lexpNextlexpNext→ϵ∣lexp lexpNext\text{lexpseq}\rightarrow\text{lexp lexpNext}\\ \text{lexpNext}\rightarrow\epsilon|\text{lexp lexpNext}

We can convert to extended bnf form or some shit

lexpSeq → lexp {lexp}

Q2

Type → INT | Type’

Type’ → \epsilon | LBRACKET RBRACKET Type’

Convert to EBNF

Type → INT {LB RB}