CT - Lecture 4
CT - Lecture 4
TYPES OF
PARSERS IN
COMPILER
DESIGN
TYPES OF PARSERS IN COMPILER DESIGN
• The parser is that phase of the compiler that takes a token string
• The parser is mainly classified into two categories, Top-down Parser, and Bottom-up Parser. These are explained
below:
• Top-Down Parser:
• The top-down parser is the parser that generates parse for the given input string with the help of grammar
productions by expanding the non-terminals. it starts from the start symbol and ends on the terminals. It uses
leftmost derivation. Where parse constructed from top and input is read from left to right. The input is recursively
• Further Top-down parser is classified into 2 types: A recursive descent parser, and a Non-recursive descent parser.
TOP-DOWN PARSER:
• Bottom-up Parser is the parser that generates the parse tree for the given
terminals. it starts from terminals and ends on the start symbol. It uses the
• Further, we classify the Bottom-up parser into two types: LR parser and
• LR parser is the bottom-up parser that generates the parse tree for the given string by using
• LR(0)
• SLR(1)
• LALR(1)
• CLR(1)
BOTTOM-UP PARSER- OPERATOR PRECEDENCE PARSER :
operators.
example, in the expression 3 + 5 * 2, multiplication (*) has higher precedence than addition (+), so the parser interprets it as 3
+ (5 * 2).
• Associativity Rules: Besides operator precedence, the parser defines the associativity of operators. In most arithmetic
operations, binary operators like + and - are left-associative (evaluated from left to right), while certain operations, such as
• Precedence Table: The Operator Precedence Parser relies on an Operator Precedence Table that contains the precedence order
• No Overlapping Operators: In grammars used with this parser, there must not be overlapping between operators, which
• This parser functions in a bottom-up parsing manner, where it starts by analyzing smaller parts of an
expression, like operators and numbers, and then gradually builds up the structure of the entire
• Using the precedence table to determine which operator to evaluate first based on its precedence
• 3+5*2
• Step 2: We see the * operator and know it has higher precedence than +.
• It cannot handle all complex grammars, as it’s generally limited to parsing expressions
• Backtracking: The parse tree is started from the root node and the input
string is matched against the production rules for replacing them. It means,
that if one derivation of a production fails, the syntax analyzer restarts the
process using different rules of the same production. This technique may
process the input string more than once to determine the right production.
BACKTRACKING
A→ab|d
Input → W=cdd c A d c A d
W = cabd W = cdd
BACKTRACKING
• Input Buffer − The input buffer includes the string to be parsed followed by
an end marker $ to denote the end of the string.
17
Thank you for listening