Professional Documents
Culture Documents
Even though a complier may not actually construct a parse tree a par
ser must be capable of constructing the tree.
The top-down construction of a parse tree is done starting with the
root, labeled with the starting non-terminal, and repeatedly performing the foll
owing two steps….
1. At node n, labeled with the non-terminal A, select one of the production
s for A and construct children at n, for the symbols on the right side of the pr
oduction.
2. Find the next node at which a sub tree is to be constructed.
a) <type>
b) <type>
For some grammar, the above steps can be implemented during a single lef
t-to-right scan of the input string.
The current token being scanned in the input is frequently referred as t
he look ahead symbol.
Initially, the look ahead symbol is the first, i.e., left most token of
the input string.
In general, the selection of a production for a non-terminal may be in t
he involvement of trail-and-error, i.e., we may have to try a production and bac
ktrack to try another production if the first is found to be unsuitable.
A production is unsuitable; we cannot complete the tree to match the inp
ut string.
There is an important special case; however called predictive parsing, i
n which back tracking does not occur.
PREDECTIVE PARSING:-
Recursive –descent parsing is a top-down method of syntax analysis in wh
ich we execute a set of recursive productions to process the input.
A procedure is associated with each non-terminal of the grammar.
The predictive parser consists of a procedures for the non-terminal <typ
e> and <sample> of grammar and an additional procedure, match.
MATCH:-
Match is used to simplify the code for <type> and <simple> it advance to the nex
t input token if it’s the argument t matchs the look ahead symbol
procedure match(t:token)
begin
if look ahead=t then
lookahead=next token
else
error
end
procedure type
begin
if lookahead is in{integer,char,num}them
simple
else
if lookahead=’^’then begin
match(^);match(id);
end
else
if lookahead=array then begin
match(array);match(‘[‘);
simple;
match(‘]);match(of);
type
end
else
error
end
procedure simple
begin
if lookahead=integer then
match(integer)
else
if lookahead=name then begin
match(name);match(dot dot);match(num);
end
else
error
end
tokens,pattern,lexems:-
->in general there is a set of strings in the input for which The same token is
procedured as output
->this set of string is described by a rule called a pattern Or (R.E)associated
with the token
->a lexeme is sequence of character in the source program that is matched by the
pattern for a token
->for eq:
Cost pi=3.1416
The substring pi is lexeme for the token identifier
->tokenscan be treated as terminal strings in the grammar For the source languag
e
->the lexeme mtched by the pattern for the token represent String of charcters i
n the source program that can be Treated as a lexical unit. In most programing l
anguages the following costructs are treated as tokens
Keywords, operators identifers, constants, literal strings, functions sy
mbols such as parantheses, commas and semicolons
In the aboveeq:when the character sequence pi appears in The source program a
token representing an identifier Is returned to the parser
->the returning of token is often implemented by passing an integer correspondin
g to the token
->A pattern is a rule describing the set of lexemes that can Repersent a particu
lar token in source program.
Parsing:
………
àparsering is the process of determing if a string of tokens can be generated by
a grammar
àA parser can be constructed for any grammar
àFor any CFG there is a parser that takes at most6 times to parse a string of n
tokens
àBut it is too expensive
àgiven a programming language generaly a grammar can be constructedthatcan be p
arsed quickly
àlinear algorithms suffice to parse essentially all languages that arise in prac
tice
àprogramming language parsers almost always make a signle left to right scan ove
r the input looking aheadb on looking ahead one token at atime
àmost parsing methods fall into one of two classes called the top down and botto
m up methods
-->top down methods starts at the root and proceeds towards the leaves while the
bottom up construction starts at the leaves and proceeds towards the root
Top-bottom parsing:-
àfor eq:the foloowing generats of the subsets of the types of pascal and assume
that dot dot is the token to emphasize
That the character sequence is treated as limit
Typeà<simple>/^id/array[<simple>]of <type> <simple>àinteger/char/num dot dot num
NON RECRUSSIVE PREDICATE PARSING:
A nonrecursive predicate parsing can be built by maintaining a stack explicitly,
rather than implivity via recursive calls.
The key problems during presdicate parsing is that of determining the production
to be apllied for a nontrminal.
The nonrecursive parse model is illustrated as
input
a + b $
X
Y
Z
$
Predicative parsing programme
Parsing table
M
It looks the produvtion to be apllied in a parsing table
A table_driven predicate praser has an input buffer,a stack,a parsing table,and
an output stream.
The input buffer contains the sting to be parsed,followed by $ ,a symbol used as
a right end marker to indicate the end of the input string.
The stack contains a sequence of grammar symbol with $ on the bottom,indicating
the bottom of the stack.
Initially,the stack contains the start symbol of the grammar on top of $.
The parsing table is a two-dimensional array M[A,a] ,where A is non-terminal and
a terminal or the symbol$.
E→
E+T/E-T/T
T→T*F/T/F/F
F→(E)/id
E→TE’
E’→+TE’/-TE’/e
T→FT’
T’→
*FT’/1FT’/e
The parser is controlled by a program that behaves as follows:
The program determines the action based on the symbols X on top stack, and ‘a’ t
he current input symbol.
The action as follows:
1. If X=a=$, then parser halts and announces successful completion of parsi
ng.
2. If X=a≠$, then the parser pops X off the stack and advances the input po
inter to the next input symbol.
3. If X is a non-terminal, the program consults entry M[X, a] of the parsin
g table M.
This entry may be X-production of grammar of an error. If the entry M[X, a] cont
ains X→UVW, the parser replaces X on the top of the stack by WVU (with U on top)
. IF M[X, a]=error, the parser calls an Error recovery Routine.
Algorithm is as follows: (Non recursive predicting parser)
Input: A string W and a parsing table M for grammar G
Output: IF W is in α (G), leftmost deriv tion of W, otherwise n error
indic tion.
Method: Initi lly the p rser is in $S on the st ck, where S is st rt symbol nd
W$ in the input buffer.
1. Set ‘ip’ to point to the first symbol of W$.
2. Repe t
Let X be the top of st ck symbol nd the symbol pointed to by ‘ip’
If X is termin l or $ then,
If X= then
POP X from st ck nd dv nce ‘ip’
else
error ()
else
if M[X, ]=X Y1Y2……..Yk then
{
POP X from the st ck
PUSH Yk,Yk-1,……Y1 on to st ck, with Y1 on top.
Output the production X Y1Y2…..Yk
}
else
error()
until X=$
The beh vior of the p rser c n be described in terms of its configur tion.
For ex mple: The p rse t ble is s follows
NON
TERMINAL INPUT SYMBOL
Id + * ( ) $
E E TE E TE
E E +TE E € E €
T T FT T FT
T T € T *FT T € T €
F F id F (E)
So, a cde isa right sentential from whose handle is A-> position
2.
Likewise, aA cde is right-sentential
from whose handle is A->A c at position 2.
Note that we say that, the su string β is a handle of αβW.
Handle programming
It is nothing ut reducing the handle y the non terminal, which is towa
rds the left of the production.
A right most derivation in reverse can e o tained y handle programming
.
Two points are to e concerned when we passing y handle running. They a
re
-To locate the su string to e reduced in a right sentential form, and
-Too determine
what production to choose if there is more than one production wi
th the su string on the right side
Shift Reduce Parsing:
->Shift-reduce parser as a ottom-up parse, which attempts to construct a parse
tree for an input string eginning at the leaves and working up towards the root
.
->At each set up of reduction,
a particular
su string matching the right side of
a production is replaced y the sym ol on the left of that production.
->The operations or actions
of shift-reduce parser are :
Shift: The next imp sym ol is shifted on to the top
of the stack.
Reduce : The handle on the top of the stack will e reduced y the non terminal
which is towards the left side of the production, which is selected for reducing
the handler.
Accept: The parser announces successful completion of parsing
Error: The parser discover that a syntax error has occurred and calls an error r
ecovery routine.
IMPLEMENTATION:
• it uses the data structure,stack
which can hold grammer sym ols,and
input uffer to hold the string to e parsed.
• The $ sm ol is used to mark the ottom of the stack and also the
right end of the input.
• Intially,the stack is empty and the string W is on the input as shown e
low:
STACK INPUT
$ W$
• The parser operates y shifting zero or more input sym ols onto the
stack until a handle S3 ia on top of the stack
• Once the handle appear on the top of the stack,the parser reduces S3
to the left side of the production.
• The parser repeats the a ove steps
until it has detected an error or
until the stack contains the start sym ol and the input is empty.
• After the parser enters this state it halts and announces successful
completion of parsing.
• The stack implementation for the input string id1+id2*id3 is as shown e
low:
STACK INPUT ACTION
$ id1+id2*id3$ SHIFT
$ id1 id +id2*id3$ REDUCE BY E
$ E +id2*id3$ SHIFT
$E+ id2*id3$ SHIFT
$E+id2 *id3$id REDUCE BY E
$E+E *id3$
SHIFT
$E+E* id3$
SHIFT
$E+E*id3 $
id REDUCE BY E
$E+E*E $
E*E REDUCE E
$E+E $
E+E REDUCE BY
$E
$ ACCEPT