0% found this document useful (0 votes)
14 views3 pages

LL and LR Parsing

LL and LR Parsing

Uploaded by

Tanveer Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

LL and LR Parsing

LL and LR Parsing

Uploaded by

Tanveer Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

LL(K) Parsing

LL(k) parsing looks at k symbols in the string called lookaheads to help guide the parsing
process. The first L in LL(k) stands for processing the input from left to right, the second L
stands for a leftmost derivation, and the k indicates the minimum number of lookaheads
needed to parse the string deterministically. LL(k) parsing works on LL(k) grammars.

LL(1) PARSING ALGORITHM


We present the following algorithm for LL(1) parsing that uses the LL(1) Parse Table and a
stack to parse strings instead of using an npda. For this stack we do not use a stack start
symbol, but instead assume this stack can determine whether it is empty or not. We first
define several functions for the algorithm.
1. push(w) pushes w on the stack. If w = x1x2 … xn, where xi is a symbol either from V or T,
then xn is pushed on the stack first, and x1 is pushed on the stack last.
2. top() returns a copy of the top symbol on the stack.
3. pop() removes and returns the top symbol on the stack.
4. get() returns the next lookahead symbol in the input string. If there is no such symbol, it
returns the end of string marker $.

LR Parsing
LR parsing starts with the string, applying productions in reverse order to derive the start
symbol. LR parsing uses a stack and two main actions, shift and reduce. The shift action
pushes the next symbol from the string onto the stack. The reduce action occurs when the
right-hand side of a production is on top of the stack. The reduce action pops the right-hand
side of the production off the stack, and then pushes the production’s left-hand side on the
stack. LR parsing uses shift and reduce actions until the start symbol is the only symbol on
the stack.
There are several types of LR parsers. We examine the simple LR or SLR parser, which is
the simplest LR parsing method. Before presenting the LR(1) Parsing Algorithm, we explore
how the algorithm is developed. We start by converting a context-free grammar to an non-
deterministic pushdown automaton (npda) in a way that mimics the LR parsing process.
That process shifts symbols onto the stack or replaces a right-hand side on top of the stack
with the corresponding left-hand side variable. But there are two issues to address. The first
issue is that an npda is nondeterministic. We make the LR parsing algorithm deterministic by
combining the npda with a lookahead, looking at the next symbol in the string to determine
which shift or reduce action to apply. The second issue is that our algorithm must recognize
when a right-hand side of a production is on top of the stack. But a right-hand side may have
more than one symbol, and one cannot look at those additional symbols without looking
beyond the top of the stack. In order to recognize a right-hand side by only looking at the top
symbol on the stack, we build a deterministic finite acceptor (dfa) combined with marked
productions called items to model all the possible combinations of symbols that can be on the
stack. Each state in the dfa stores marked productions to keep track of whether part or all of a
right-hand side has been placed on the parsing stack. Once the dfa is built, we store the
information from the dfa in an LR(1) Parse Table. The LR(1) Parsing Algorithm then parses a
string by looking at the symbol on top of the stack and the current symbol in the string,
combined with information in the LR(1) Parse Table to determine which shift or reduce
operation to apply.
An LR(k) grammar is a restricted type of context-free grammar that can be parsed using the
LR(k) Parsing Algorithm. The L stands for processing the input symbols from left to right, the
R stands for creating a rightmost derivation, and the k stands for the number of lookaheads. A
context-free grammar is an LR(k) grammar if its right-hand sides are recognizable in a
bottom-up parser using up to k lookahead symbols. We only consider k = 1, which is
typically sufficient for a grammar representing a programming language. We will see that if
we can build an LR(1) Parse Table that has no conflicts, then our context-free grammar is an
LR(1) grammar. More context-free grammars are LR grammars than LL grammars.

LR(1) Parsing Actions


1. A shift action appears in the columns for terminals as sN for some number N and means
shift the terminal for that column on the stack, and move to row N.
2. A goto action appears in the columns with variables as gN for some number N and means
go to row N.
3. A reduce action appears in the columns for terminals and $ as rN for some number N and
means reduce by production number N.
4. An accept action appears in the column for $ as acc and means accept the string.
5. Any blank entry in the table means an error has occurred and the input string is not
accepted.

Mealy and Moore Machine


Mealy Machines
In a Mealy machine, the output produced by each transition depends on the internal state
prior to the transition and the input symbol used in the transition, so we can think of the
output produced during the transition.
A Mealy machine is defined by the six-tuple
M = (Q,Σ, Γ, δ, θ, q0),
where
Q is a finite set of internal states,
Σ is the input alphabet,
Γ is the output alphabet,
δ: Q × Σ → Q is the transition function,
θ: Q × Σ →Γ is the output function,
q0 ∈ Q is the initial state of M.
The machine starts in state q0 at which time all input is available for processing. If at time tn
the Mealy machine is in state qi, the current input symbol is a, and δ(qi, a) = qj, θ(qi, a) = b,
the machine will enter state qj and produce output b. It is assumed the entire process is
terminated when the end of the input is reached. Note that there are no final states associated
with a transducer.

Moore Machines
Moore machines differ from Mealy machines in the way output is produced. In a Moore
machine every state is associated with an element of the output alphabet. Whenever the state
is entered, this output symbol is printed. The output is produced only when a transition
occurs; thus, the symbol associated with the initial state is not printed at the start, but it may
be produced if this state is entered at a later stage.
A Moore machine is defined by the six-tuple
M = (Q,Σ, Γ, δ, θ, q0),
where
Q is a finite set of internal states,
Σ is the input alphabet,
Γ is the output alphabet,
δ: Q × Σ → Q is the transition function,
θ: Q →Γ is the output function,
q0 ∈ Q is the initial state.
The machine starts in state q0, at which time all input is available for processing. If at time tn
the Moore machine is in state qi, the current input symbol is a, and δ(qi, a) = qj, θ(qj) = b, the
machine will enter state qj and produce output b.

You might also like