Microprocessor Tutorial
Microprocessor Tutorial
ISBN 0-321-33025-0
Chapter 4 Topics
• Introduction
• Lexical Analysis
• The Parsing Problem
• Recursive-Descent Parsing
• Bottom-Up Parsing
• Top-down Parsers
– Given a sentential form, xAα , the parser must
choose the correct A-rule to get the next
sentential form in the leftmost derivation, using
only the first token produced by A
• The most common top-down parsing
algorithms:
– Recursive descent - a coded implementation
– LL parsers - table driven implementation
• Bottom-up parsers
– Given a right sentential form, α, determine what
substring of α is the right-hand side of the rule
in the grammar that must be reduced to
produce the previous sentential form in the
right derivation
– The most common bottom-up parsing
algorithms are in the LR family
/* Function expr
Parses strings in the language
generated by the rule:
<expr> → <term> {(+ | -) <term>}
*/
void expr() {
term();
…
/* Function factor
Parses strings in the language
generated by the rule:
<factor> -> id | (<expr>) */
void factor() {
lex();
• Shift-Reduce Algorithms
– Reduce is the action of replacing the handle on
the top of the parse stack with its corresponding
LHS
– Shift is the action of moving the next token to
the top of the parse stack
• Advantages of LR parsers:
– They will work for nearly all grammars that
describe programming languages.
– They work on a larger class of grammars than
other bottom-up algorithms, but are as efficient
as any other bottom-up parser.
– They can detect syntax errors as soon as it is
possible.
– The LR class of grammars is a superset of the
class parsable by LL parsers.
(S0X1S1X2S2…XmSm, aiai+1…an$)