Syntax Analysis (Part-I)
Syntax Analysis (Part-I)
Then α α α α ϒ)
∗
If α α α α , then α α (Reflexive transitive closure)
Types of Parser
• There are three general types of parsers for grammars: universal, top-
down, and bottom-up.
• Universal parsing methods such as the Cocke-Younger-Kasami algorithm
and Earley's algorithm can parse any grammar. These general methods
are, however, too inefficient to use in production compilers.
• Top-down methods build parse trees from the top (root) to the bottom
(leaves).
• Bottom-up methods start from the leaves and work their way up to the
root.
• In either case, the input to the parser is scanned from left to right, one
symbol at a time.
Notational Conventions
Notational Conventions
Notational Conventions
Parse Tree
• A parse tree pictorially shows how the start symbol of a grammar derives a string in
the language.
• If nonterminal A has a production A XYZ, then a parse tree may have an interior
node labeled A with three children labeled X, Y , and Z, from left to right:
• Predictive parsers can be constructed for LL(1) grammars since the proper
production to apply for a nonterminal can be selected by looking only at
the current input symbol.
• Flow-of-control constructs, with their distinguishing key- words, generally
satisfy the LL(1) constraints. For instance, if we have the productions
LL(1) Grammars
Symbol FIRST FOLLOW
E {(, id} {),
E’ {+, } {),
T {(, id} {+, ),
T’ {*, {+, ),
F {(, id} {+, *, ),
Nonrecursive Predictive Parsing
• A nonrecursive predictive parser can be constructed by maintaining a
stack explicitly, rather than implicitly done recursive calls.
• The parser mimics a leftmost derivation.
• If w is the input that has been matched so far, then the stack holds a
sequence of grammar symbols α such that
• If parser looks up entry M[A, a] and finds it blank, then the input
symbol a is skipped.
• If the entry is synch, then the non-terminal on the top of stack is
popped in an attempt to resume parsing.
• If a token on the top of stack does not match the input symbol, then
we pop the token from the stack.