Tutorial 5

CourseCompilers & Interpreters
SemesterS1 2022

Tutorial 5

  • [ ] Tutorial 4 - Questions

For a production of the formAα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.

lexpatom | listatomNUM  IDlist’(’ lexpSeq ’)’lexpseqlexpSeq 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: AAαβ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\rightarrow \beta A’\ A’\rightarrow \epsilon | \alpha A’

- The left recursion in this grammar occurs in the production $\text{lexpseq}\rightarrow\text{lexpSeq lexp | lexp}$ - We resolve this by introducing a new non-terminal symbol, say $\text{lexpNext}$, and a new production - Using the formula defined above, A = lexpseq, alpha=lexp, beta=lexp

\text{lexpseq}\rightarrow\text{lexp lexpNext}\ \text{lexpNext}\rightarrow\epsilon|\alpha \text{ lexpNext’}

Finally,theproductionsare: - Finally, the productions are:

\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 $\text{First}$ and $\text{Follow}$ sets for the non-terminals of the resulting grammar for the question above - The non-terminal symbols in the productions above are: $\{\text{lexp, atom, list,lexpSeq, lexpNext}\}$

\def\first{\text{First}} \first(\bold{NUM})=

3. d $$\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}

  • FIrst column - add all of the terminal symbols in the production
  • lexp can start with an atom, so we add the first set of an atom to lexp
lexpNUM, IDNUM, IDNUM, ID
atom{NUM, ID}{NUM, ID}{NUM, ID}{NUM, ID}
list{’(’}{’(’}{’(’}{’(’}
lexpSeqNUM, IDNUM, ID, “(”
lexpNextϵ\epsilonϵ\epsilonϵ\epsilonϵ\epsilon

d.

The following productions are given from before

lexpseqlexp 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}