Atcd Unit 2
Atcd Unit 2
UNIT-II
Context free grammar
Context free grammar is a formal grammar which is used to
generate all possible strings in a given formal language.
Context free grammar G can be defined by four tuples as:
G= (V, T, P, S)
Where,
G describes the grammar
T describes a finite set of terminal symbols.
V describes a finite set of non-terminal symbols
P describes a set of production rules
S is the start symbol.
In CFG, the start symbol is used to derive the string. You can
derive the string by repeatedly replacing a non-terminal by the
right hand side of the production, until all non-terminal have
been replaced by terminal symbols.
Example
1.Construct the CFG for the language having any number of
a's over the set ∑= {a} Check String:aaaa
2. Construct a CFG for the regular expression (0+1)*. Check
String:1010
3. Construct a CFG for a language L = {wcwR | where w € (a,
b)*}. Check String: abbcbba
4. Construct a CFG for the language L = anb2n where n>=1.
Check String:aabbbb.
A grammar is said to be ambiguous if there exists more than one left most
derivation or more than one right most derivation or more than one parse
tree for a given input string.
A → aA’
A’ → AB / Bc / Ac
A → aA’
A’ → AD / Bc
D→B/c
This is a left factored grammar.
Left-Factoring
Problem-2: Do left factoring in the following grammar-
S → aSSbS / aSaSb / abb / b
S → aS’ / b
S’ → SSbS / SaSb / bb
S → aS’ / b
S’ → SA / bb
A → SbS / aSb
This is a left factored grammar.
LL(1) Parsing: Here the 1st L represents that the scanning of the
Input will be done from the Left to Right manner and the
second L shows that in this parsing technique, we are going to
use the Left most Derivation Tree. And finally, the 1 represents
the number of look-ahead, which means how many symbols are
you going to see when you want to make a decision.
Essential conditions to check first are as follows:
1.The grammar is free from left recursion.
2.The grammar should not be ambiguous.
3.The grammar has to be left factored in so that the grammar is
deterministic grammar.
These conditions are necessary but not sufficient for proving a
LL(1) parser.
Algorithm to construct LL(1) Parsing Table:
Step 1: First check all the essential conditions mentioned above and go
to step 2.
Step 2: Calculate First() and Follow() for all non-terminals.
1. First(): If there is a variable, and from that variable, if we try to drive
all the strings then the beginning Terminal Symbol is called the First.
2.Follow(): What is the Terminal Symbol which follows a variable in the
process of derivation.
Step 3: For each production A –> α. (A tends to alpha)
3.Find First(α) and for each terminal in First(α), make entry A –> α in
the table.
2.If First(α) contains ε (epsilon) as terminal, then find the Follow(A) and
for each terminal in Follow(A), make entry A –> ε in the table.
3.If the First(α) contains ε and Follow(A) contains $ as terminal, then
make entry A –> ε in the table for the $.
To construct the parsing table, we have two functions:
In the table, rows will contain the Non-Terminals and the column
will contain the Terminal Symbols.
All the Null Productions of the Grammars will go under the Follow
elements and the remaining productions will lie under the elements
:
of the First set.
construct the given grammar into LL(1) Parser
E → E+T/ T
T → T*F / F
F → (E) / id verify w=id+id
•Sift reduce parsing performs the two actions: shift and reduce.
That's why it is known as shift reduces parsing.
•At the shift action, the current symbol in the input string is pushed
to a stack.
•At each reduction, the symbols will replaced by the non-
terminals. The symbol is the right side of the production and non-
terminal is the left side of the production.
Grammar
LR parsers are also known as LR(k) parsers, where L stands for left-
to-right scanning of the input stream; R stands for the construction
of right-most derivation in reverse, and k denotes the number of
lookahead symbols to make decisions.
Steps for constructing the LR(0) parsing table :
STEP 1 –
STEP 2 –
STEP 3 – Find LR(0) collection of items
STEP 4- Draw the data flow diagram
STEP 5- Parse Table
1)Construct an LR parsing table for the given context-free grammar –
E->BB
B->cB/d verify the w=ccdd also construct the parse tree.
E->E+T/T
T->TF/F
F->F*/a/b
SLR Parser :
E->E+T/T
T->T*F/F
F->(E)/id verify the w=id*id+id also construct the parse tree.
E->T+E/T
T->id
verify the w=id+d+id also construct the parse tree.
Advantages of SLR -parsing draw backs of-SLR parsing