lec04-bottomupparser

# lec04-bottomupparser - Bottom-Up Parsing A bottom-up parser...

This preview shows pages 1–6. Sign up to view the full content.

BİM454 Programming Language Concepts 1 Bottom-Up Parsing A bottom-up parser creates the parse tree of the given input starting from leaves towards the root. A bottom-up parser tries to find the right-most derivation of the given input in the reverse order. S ... ϖ (the right-most derivation of ϖ ) (the bottom-up parser finds the right-most derivation in the reverse order) Bottom-up parsing is also known as shift-reduce parsing because its two main actions are shift and reduce. At each shift action, the current symbol in the input string is pushed to a stack. At each reduction step, the symbols at the top of the stack (this symbol sequence is the right side of a production) will replaced by the non-terminal at the left side of that production. There are also two more actions: accept and error.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
BİM454 Programming Language Concepts 2 Shift-Reduce Parsing A shift-reduce parser tries to reduce the given input string into the starting symbol. a string the starting symbol reduced to At each reduction step, a substring of the input matching to the right side of a production rule is replaced by the non-terminal at the left side of that production rule. If the substring is chosen correctly, the right most derivation of that string is created in the reverse order. Rightmost Derivation: S ϖ Shift-Reduce Parser finds: ϖ ... S * rm rm rm
BİM454 Programming Language Concepts 3 Shift-Reduce Parsing -- Example S aABb input string: aa a bb A aA | a a aA bb B bB | b aA b b reduction aABb S S aABb aA b b a aA bb aa a bb Right Sentential Forms How do we know which substring to be replaced at each reduction step? rm rm

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
BİM454 Programming Language Concepts 4 Handle Informally, a handle of a string is a substring that matches the right side of a production rule. But not every substring matches the right side of a production rule is handle A handle of a right sentential form γ ( αβϖ ) is a production rule A β and a position of γ where the string β may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of γ . S α A ϖ If the grammar is unambiguous, then every right-sentential form of the grammar has exactly one handle. We will see that ϖ is a string of terminals. rm rm *
BİM454 Programming Language Concepts 5 Handle Pruning A right-most derivation in reverse can be obtained by handle-pruning . S= γ 0 γ 1 γ 2 ... γ n-1 γ n = ϖ input string Start from γ n , find a handle A n →β n in γ n , and replace β n in by A n to get γ n-1 . Then find a handle A n-1 n-1 in γ n-1 , and replace β n-1 in by A n-1 to get γ n-2 . Repeat this, until we reach S.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/20/2009 for the course CENG PLC taught by Professor Anyone during the Fall '09 term at Anadolu University.

### Page1 / 58

lec04-bottomupparser - Bottom-Up Parsing A bottom-up parser...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online