c06 - CS421 COMPILERS AND INTERPRETERS CS421 Parser...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Parser Generation: Page 1 of 27 Parser Generation •M a i n P r o b l em : given a grammar G , how to build a top-down parser or a bottom-up parser for it ? •p a r s e r : a program that, given a sentence, reconstructs a derivation for that sentence ---- if done sucessfully, it “recognize” the sentence all parsers read their input left-to-right , but construct parse tree differently. • bottom-up parsers --- construct the tree from leaves to root shift-reduce, LR, SLR, LALR, operator precedence • top-down parsers --- construct the tree from root to leaves recursive descent, predictive parsing, LL(1) Copyright 1994 - 2010 Zhong Shao, Yale University Parser Generation: Page 2 of 27 Bottom-Up Parsing • Construct parse tree “bottom-up” --- from leaves to the root • Bottom-up parsing always constructs right-most derivation • Important parsing algorithms: shift-reduce , LR parsing • LR parser components: input, stack (strings of grammar symbols and states ), driver routine, parsing tables. LR Parsing output s m X m .. s 1 X 1 s 0 parsing a 1 a 2 a 3 a 4 ...... a n $ input: stack Copyright 1994 - 2010 Zhong Shao, Yale University Parser Generation: Page 3 of 27 LR Parsing • A sequence of new state symbols s 0, s 1, s 2, ..., s m ----- each state sumarize the information contained in the stack below it. Parsing configurations : (stack, remaining input) written as (s 0 X 1 s 1 X 2 s 2 ...X m s m , a i a i+1 a i+2 ...a n $) next “move” is determined by s m and a i • Parsing tables : ACTION[s,a] and GOTO[s,X] Table A ACTION[s,a] --- s : state , a : terminal its entries (1) shift s k (2) reduce A -> (3) accept (4) error Table G GOTO[s,X] --- s : state , X : non-terminal its entries are states Copyright 1994 - 2010 Zhong Shao, Yale University Parser Generation: Page 4 of 27 Constructing LR Parser How to construct the parsing table ACTION and GOTO ? basic idea : first construct DFA to recognize handles, then use DFA to construct the parsing tables ! different parsing table yield different LR parsers SLR(1) , LR(1) , or LALR(1) augmented grammar for context-free grammar G = G (T,N,P,S) is defined as G’ = G’ (T, N { S’}, P { S’ -> S}, S’) ------ adding non- terminal S’ and the production S’ -> S, and S’ is the new start symbol. When S’ -> S is reduced, parser accepts. LR(0) item for productions of a context-free grammar G ----- is a production with dot at some position in the r.h.s. For A -> XYZ , its items are A -> . XYZ A -> X . YZ A -> XY.Z A -> XYZ. For A -> , its items are just A -> .
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Parser Generation: Page 5 of 27 LR(0) items and LR(0) DFA • Informally, item A -> X . YZ means a string derivable from X has been seen, and one from YZ is expected. LR(0) items are used as state names for LR(0) DFA or LR(0) NFA that recognizes viable prefixes.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 01/06/2012.

Page1 / 7

c06 - CS421 COMPILERS AND INTERPRETERS CS421 Parser...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online