Sie sind auf Seite 1von 13

10/26/12 Chapter 6 : LL(K) and LR (K) Grammars | FORMAL LANGUAGE & AUTOMATA THEORY

FORMAL LANGUAGE & AUTOMATA


THEORY
Recommend

Chapters
Home
Topics
Chapter 1 : Formal Languages and Thier
Classification
Chapter 2 : Regular Expressions and Finite
Automata (Part 1)

Home

Chapte
r6:
LL(K)
and LR
(K)
Gramm
ars

Chapter 2 : Part 2
Chapter 3 : Context Free Grammars and Their
Normal Forms (Part 1)
Chapter 3 : Part 2
Chapter 3 : Part 3
Chapter 4 : An Introduction to Push Down
Automata
Chapter 5 : Turing Machines (Part 1)
Chapter 5 : Part 2
Chapter 6 : LL(K) and LR (K) Grammars
Chapter 7 : Linear Grammars, Cellular Automata
and Rewriting Systems

Q.1
Write
the
defini
tions
of LL
(k)
Gram
mar.
Ans.
A
conte
xt
free
gram
mar
is
called
LL (k)
for
left to
right
scan,
produ
cing a
leftm
ost
deriva
tion
with k
symb
ol
look
ahead
if we
can
alway
s
make
a

2.6k

correct decision by
checking at most the first
k symbols of W.

Q.2 Write the definition


of LR (k) grammar.
Ans. LR (k) is a grammar
in which purser scans the
input from left to right and
generate the reverse
rightmost derivation, and it
can take a decision about
reduction by looking next k
symbol only. That is why;
it is called LR (k) grammar.

Q. 3. Explain the
Closure
properties of
Language
Classes.
Ans. Various properties of
languages. are:

Q .4 What are LR (k)


grammars Explain
with examples and
also state some
properties.
Ans LR (k) grammars are
used in syntax analysis
phase of a
compiler LR (k) grammars
reduce the given string w
to start symbol S by
reducing. It scans the

string from left to right and while


reduction it gives reverse rightmost
derivation of the string.

Properties of LR (k) grammars:


ptucse.l

1. Eve

ry
LR
(k)
gra
m
ma
r C
is

oremate.com/flat/node/9

unambiguous.
C is an LR (k)
grammar
there
exists
pushdown automata A
accepting L
(G). 1
3. If A is a deterministic
pushdown automata A
there exists an

2. If

1/7

10/26/12 Chapter 6 : LL(K) and LR (K) Grammars | FORMAL LANGUAGE & AUTOMATA THEORY

LR (1.) grammar G such that L (C) = N (A) V


4 The class of deterministic language is said to be proper
subclass of the class of context free languages. V
5. The class of deterministic languages can be denoted by
LdCnl is closed under complementation but not under union
and intersection
6. A context free language is generated by an LR (0) grammar
if and only if it is accepted by deterministic PDA and has prefix
property.
7 There is an algo to decide whether a given context free
grammar is LR (k) for a given natural number k

Q 5. Explain in brief the properties of LL (k) grammers


Ans.
1 A context free grammar is LL (k) grammar if by scanning n
input string left to right we can get a left most derivation by
looking k symbols ahead
2. LL (1) grammars are successfully being used in compilers.
3. LL (k) grammars are used in top down porsing.
4. LL (k) grammars are weak as compared to LR (k).
5. Every LL (k) grammar is non-ambiguous.

Q. 6. Explain in brief the properties of LR (k) grammars.


Ans. various properties of LR (k) grammars are as follows:
1. Every LR (k) grammar is unambiguous.
2. If C is an LR (k) grammar, there exist deterministic
pushdown automata A accepting L (C).
3. If A is a deterministic pushdown Automata A, there exists an
LR (1) grammar C such that L (G) = N (A).

Q. 7. Write closure properties of Languages.


Ans. various closure properties of languages are:

Where,

ptucse.loremate.com/flat/node/9

2/7

10/26/12 Chapter 6 : LL(K) and LR (K) Grammars | FORMAL LANGUAGE & AUTOMATA THEORY

Q. 8. Explain the properties of LL (k) and LR (k)


grammars.
Ans. A context free grammar is called LL (k), for left to right
scan, producing a leftmost derivation, with k symbol look
ahead, if we can always make a correct decision by checking
at most the first k symbols of w.
Formal properties of LL (k) grammar
For a grammar to be LL (1), the necessary and sufficient
condition is

W: k meaning the k letters long initial segment of W, that is

A context free grammar 18 LL (k) if and only if for every pair


of left most derivation of the form

Q. 10. Write short notes on any two:

(1) Derivation languages.


(2) Cellular Automata.
(3) Syntax Analysis.
ptucse.loremate.com/flat/node/9

3/7

10/26/12 Chapter 6 : LL(K) and LR (K) Grammars | FORMAL LANGUAGE & AUTOMATA THEORY

Loremate.com

on

Facebook
Like
2,681 people like Loremate.com.

Chebii

Gunjan

Er

Facebook social plugin

(2) Cellular Automata : Cellular automata are simple

mathematical idealization of natural system. They consist of a


lattice of discrete identical sites, each site taking on a finite set
of any integer values. The values of the sites evolve in discrete
time steps according to deterministic roles that specify the
value of each site in terms up the value of neighbouring sites.
Cellular Automata are by definition dynamical systems which
are discrete in space and time, operate on a uniform regular,
lattice and are characterized by local interaction.
Cellular automata may be considered as discrete idealization
of the partial differential equations after used to describe
natural system.
Their discrete nature also allows an important analogy with
digital computer: cellular automata may be viewed as parallel
processing computers of simple construction.
(3) Syntax Analysis: Syntax analysis is performed by a special
program, called syntax analyzer or parser. A parser obtaining a
string of to hens from the lexical analyzer string of tokens and
checks whether whether the string is valid construct of the
same language parser performs this task by verifying whether
the string of to hens obtained from the lexical analyzer, can be
generated by the grammar for the source language and for this
a parser either attempts to derive a string w from start symbol
S or the reduce string w to S.
Parsing algorithms are of two types

ptucse.loremate.com/flat/node/9

4/7

10/26/12 Chapter 6 : LL(K) and LR (K) Grammars | FORMAL LANGUAGE & AUTOMATA THEORY

2. Bottom op parsing: We generate start symbol S from string


w by performing reduction.
Consider previous grammar.

Bottom op parsing takes place from leaf of parser tree into its
root.

Q. 11. Write the closure properties of Languages.


Ans.

Q. 12. Write short notes on the following:


(1) Closure properties of languages.
(2) Syntax Analysis.
(3) Kusoda Normal Form.
(4) Pushdown Automata.
Ans. (a) Closure properties of languages:

(b) Syntax analysis: Syntax analysis is performed by a special


program known as syntax analyzer or passes. It obtains a
string of to hens from lexical analyzer and check whether the
string is valid for the language or not. The parsing algorithms
are divided into 2 categories (a) top down parsing (b) bottom
up parsing. Here top-down and bottom up denotes ways of
constructing the derivation tree which represents either
leftmost derivation in case of top-down construction whereas it
produces a rightmost derivation in reverse order for bottom-up
construction. Various types of grammars used by syntax
analysis are LL (k) and LR (k). LL (k) denotes left to right
scan of string product, left most derivation by looking ahead k
symbols of string for making the decision. LR (k) denotes
left to right scan of string producing reverse rightmost
derivation by looking ahead k symbols of string for making

decisions. (c) Kuroda normal form A length increasing grimmer


is said to be in kuroda normal from if each of its rules has any
of the following four forms:
ptucse.loremate.com/flat/node/9

5/7

10/26/12 Chapter 6 : LL(K) and LR (K) Grammars | FORMAL LANGUAGE & AUTOMATA THEORY

(d) Pushdown Automata: A pushdown automata is a


computational model used for recognizing the strings of next
free languages. A pushdown automata uses the input tape of
fixed length for input string and stack (linear data structure)
for memory.
A push down automata is a system which is mathematically
defined as follows:

form

Formal properties of LR (k) grammar as follows:


1. Every LR (k) grammar C is unambiguous.
2. If G is an LR (k) grimmer there exists deterministic
pushdown automata A accepting Z (C).
3. If A is a deterministic pushdown automata A, there exists an

LR (1) grammar C such that L (G) = N (A).


ptucse.loremate.com/flat/node/9

6/7

10/26/12 Chapter 6 : LL(K) and LR (K) Grammars | FORMAL LANGUAGE & AUTOMATA THEORY

4. The class of deterministic languages is a proper subclass of


the class of context free languages.
5.
(Deterministic context free languages) is closed under
complementation but not under union and intersection.
6. A CFL is generated by LR (0) grammar iff it is accepted by
deterministic PDA and has prefix property.
7. There is an algorithm to decide whether a given CFG is LR
(k) for a given Natural number k.

ptucse.loremate.com/flat/node/9

7/7