L6-Parsing1 - Introduction to Compiler Design Syntax...

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

View Full Document Right Arrow Icon
Introduction to Compiler Design Syntax Analysis: Top-Down and Bottom-Up Parsing Professor Yi-Ping You Department of Computer Science http://www.cs.nctu.edu.tw/~ypyou/ Page 1 Introduction to Compiler Design, Spring 2010 ttp // cs ctu edu t / ypyou/
Background image of page 1

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

View Full DocumentRight Arrow Icon
Recall: Overview of Syntax Analysis Goal – determine if the input token stream tisfies the syntax of the program satisfies the syntax of the program What do we need to do this? n expressive way to describe the syntax An expressive way to describe the syntax A mechanism that determines if the input token stream atisfies the syntax description satisfies the syntax description ontext- ee Parser Context free grammar, G ken stream s Yes, if s in L(G) No, otherwise Token stream, s (from scanner) Error messages Page 2 Introduction to Compiler Design, Spring 2010
Background image of page 2
RE/Scanner RE/Scanner v.s v.s. Grammar/Parser . Grammar/Parser Lexical analysis e use gular expressions specify the lexical We use regular expressions to specify the lexical definitions of programming languages Given a character stream, a scanner outputs a token , p stream upon the lexical definitions Syntax analysis e use ontext ee grammars specify the We use context-free grammars to specify the grammatical structure of programming languages iven a token stream a arser utputs a syntax tree Given a token stream, a parser outputs a syntax tree (parse tree) upon the grammatical definitions Page 3 Introduction to Compiler Design, Spring 2010
Background image of page 3

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

View Full DocumentRight Arrow Icon
Outline Top-Down Parsing ecursive escent Parsing Recursive-Descent Parsing Predictive Parsing (LL(k) Parsing) Nonrecursive Predictive Parsing Error Recovery Bottom-Up Parsing hift educe Parsing Shift-Reduce Parsing LR(0) Parsing Simple LR (SLR) Parsing Page 4 Introduction to Compiler Design, Spring 2010
Background image of page 4
Type of Parsers Universal parser (any CFG) Cocke-Younger-Kasimi algorithm Earley’salgorithm Inefficient ( O ( n 3 ) , where n is the number of input tokens) op- own parser FG with restrictions) Top down parser (CFG with restrictions) Start at the root of the parse tree and grow toward leaves E.g., input: id+id E T + T T ( E ) | - E | id E + T T E + T T E + T T E + + T + T T Left-to-right scan, Leftmost derivation E T+T id +T id + id id id id id id id +T id lm lm lm (LL parser ) Bottom-up parser (CFG with restrictions) Start at the leaves and grow toward root Page 5 Introduction to Compiler Design, Spring 2010 g Left-to-right scan, Rightmost derivation (LR parser)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Outline Top-Down Parsing ecursive escent Parsing Recursive-Descent Parsing Predictive Parsing (LL(k) Parsing) Nonrecursive Predictive Parsing Error Recovery Bottom-Up Parsing hift educe Parsing Shift-Reduce Parsing LR(0) Parsing Simple LR (SLR) Parsing Page 6 Introduction to Compiler Design, Spring 2010
Background image of page 6
Top Top-Down Parsing Down Parsing Construct a parse tree for the input string, starting om the ot nd creating the nodes of the parse from the root and creating the nodes of the parse three in preorder (depth-first) inding a ftmost derivation r an input string Finding a leftmost derivation for an input string Recursive-descent parsing
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/25/2010 for the course ALL 0204 taught by Professor 79979 during the Spring '10 term at National Chiao Tung University.

Page1 / 74

L6-Parsing1 - Introduction to Compiler Design Syntax...

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

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