Schoolof ElectricalSciences ComputerScience and...

2

Click here to load reader

Transcript of Schoolof ElectricalSciences ComputerScience and...

Page 1: Schoolof ElectricalSciences ComputerScience and ...cse.iitkgp.ac.in/~goutam/bbsCompiler/lab/assignment4.pdf · Schoolof ElectricalSciences ComputerScience and Engineering ... Following

School of Electrical SciencesComputer Science and Engineering

I. I. T. Bhubaneswar

Compilers Laboratory (Autumn: 2016-2017)4th Year CSE: 7th Semester

Assignment - 4 Marks: 25Assignment Out: 26th August, 2016 Report on or before: 8th September, 2016

1. In this experiment you construct a non-deterministic finite automaton(NFA) equivalent to a given regular expression over the alphabet Σ. Thismay be viewed as a translation.

2. Use Flex and Bison to construct the NFA. Used the method using first-pos(), lastpos(), nullable() and followpos(), given in section 3.9 of Compil-ers Principles, Techniques, & Tools by Alfred V Aho, Monica S Lam, Ravi

Sethi and Jeffrey D. Ullman (2nd ed.).

3. Use the following ambiguous context-free grammar G of regular expres-sions over Σ.

S → E ·#E → E + E | E ·E | E ∗ | (E) | σ | e

where σ ∈ Σ and e 6∈ Σ. We use ‘+’ for union, ‘·’ for concatenation, e forε (null string) and # 6∈ Σ is the special end-of-expression marker.

We assume that the order of precedence of the operators are ∗ > · > +.Both the binary operators are left associative.

The alphabet set Σ should contain {0, 1, 2, 3} as symbols. You may in-crease the number of symbols in Σ if you wish.

The terminals of the context-free grammar G are {+ · ∗ ( ) e #} ∪ Σ.The non-terminals are {S,E}, where S is the start symbol.

4. Use Flex to identify the tokens of the grammar G. Use Bison to parsethe augmented regular expression. Construct the NFA while parsing theinput string (augmented regular expression) - syntax-directed translation.

5. Following global data structures and attributes of non-terminals may beused.

(a) A global array pos[]: where pos[i] stores the element of σ ∈ Σ∪{#}in position i. A global variable pc may be used as a position counter.

(b) A global array followPos[]: where followPos[i] stores the list ofpositions that may follow the symbol of position i.

(c) A global state transition table, where the states are the differentpositions of σ ∈ Σ ∪ {#} in the regular expression. The next-stateon an input σ ∈ Σ ∪ {#} is a list of positions.

(d) The attributes of non-terminals are fp (firstpos), lp (lastpos), nl (nul-lable).

1

Page 2: Schoolof ElectricalSciences ComputerScience and ...cse.iitkgp.ac.in/~goutam/bbsCompiler/lab/assignment4.pdf · Schoolof ElectricalSciences ComputerScience and Engineering ... Following

6. A few examples of syntax-directed translations are as follows:

(a) Production rule: E → σ

Semantic Actions:pos[pc] = σ.val; // code for σ

E.fp = E.lp = makeSet(pc);pc++;

E.nl = false;

(b) Production rule: E → E1 ·E2

Semantic Actions:if (E1.nl) E.fp = E1.fp ∪ E2.fp; else E.fp = E1.fp;

if (E2.nl) E.lp = E1.lp ∪ E2.lp; else E.lp = E2.lp;

E.nl = E1.nl ∧ E2.nl

∀i ∈ E1.lp, followPos[i] = followPos[i] ∪ E2.fp;

Complete these definitions and encode them as actions in the bison filenfa.y++.

7. Write function to construct the NFA state transition table using followPos[],pos[] etc.

8. Write functions to print the set of states, transition table, start states andthe final state in human understandable form.

9. Prepare a Makefile for the compilation of the whole process. Finally pre-pare a .tar file and return the it.

10. You are free to change the data structures, but it is essential to implementthe mentioned algorithm.

2