Lecture 5-6
Lecture 5-6
1. The parser obtains a string of tokens from the lexical analyzer and
verifies that the string of token names can be generated by the
grammar for the source language.
Context-free grammars
Example:
|
Context Free Language (CFL)
The language generated by a grammar
∗
If is a context free grammar, then we call
a context free language.
where
Example:
is a CFG.
Thus, is a context free language.
Derivation (Parse) Tree
Consider the following CFG for balanced parentheses
Let
Derivation 1:
Derivation 2:
Pictorial representation of
both derivations for :
Derivation (Parse) Tree (formal definition)
Parse tree for each .
If is a rule, then
Derivation (Parse) Tree (formal definition)
Example: Consider a CFG with productions
.
String
Left most and Right most derivations.
If at each step in a derivation a production is applied to the
leftmost variable, then the derivation is said to be leftmost.
Example: ,
String:
Leftmost derivation:
rightmost derivation
Ambiguity
A CFG such that some word has two distinct parse trees (two
distinct leftmost derivations / two distinct rightmost derivations)
is said to be ambiguous.
String:
Ambiguity
Example: Consider a CFG with productions
String:
Ambiguous grammars
In general, find the longest prefix common to two or more of its alternatives.
2)
A α
β A α
Since the variable
A α A calls A without doing
β anything this might lead to
an infinite loop
Language generated by this grammar is βα* A α
with
A 1A’ | 2A’ | ... | nA’
A’ 1A’ | 2A’ | ... | m A’ |
Top-Down parsing
Construction of the parse tree starts at the root (from the start
symbol) and proceeds towards leaves (token or terminals)
Can be viewed as finding a leftmost derivation for an input string.
Bottom-up parsing
Construction of the parse tree starts from the leaf nodes and
proceeds towards root (start symbol).
Order is that of the reverse of a rightmost derivation
Bottom-up parsing:
Order is that of the reverse of a rightmost derivation.
Main Task is to make a decision of whether to shift or reduce.
Parsers
Operator LR
Backtracking Predictive
Precedence
Recursive
LL(1) LR(0) SLR(1) CLR(1) LALR(1)
Descent