lecture05 - Introduction to Parsing Le cture5 CS 143...

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

View Full Document Right Arrow Icon
CS 143 Lecture 5 1 Introduction to Parsing Lecture 5
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 143 Lecture 5 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations Ambiguity
Background image of page 2
CS 143 Lecture 5 3 Languages and Automata Formal languages are very important in CS Especially in programming languages Regular languages The weakest formal languages widely used Many applications We will also study context-free languages, tree languages
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS 143 Lecture 5 4 Beyond Regular Languages Many languages are not regular Strings of balanced parentheses are not regular: { } ( ) | 0 i i i
Background image of page 4
CS 143 Lecture 5 5 What Can Regular Languages Express? Languages requiring counting modulo a fixed integer Intuition: A finite automaton that runs long enough must repeat states Finite automaton can’t remember # of times it has visited a particular state
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS 143 Lecture 5 6 The Functionality of the Parser Input: sequence of tokens from lexer Output: parse tree of the program (But some parsers never produce a parse tree . . .)
Background image of page 6
CS 143 Lecture 5 7 Example Cool if x = y then 1 else 2 fi Parser input IF ID = ID THEN INT ELSE INT FI Parser output IF-THEN- ELSE = ID ID IN T IN T
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS 143 Lecture 5 8 Comparison with Lexical Analysis Phase Input Output Lexer String of characters String of tokens Parser String of tokens Parse tree
Background image of page 8
CS 143 Lecture 5 9 The Role of the Parser Not all strings of tokens are programs . . . . . . Parser must distinguish between valid and invalid strings of tokens We need A language for describing valid strings of tokens A method for distinguishing valid from invalid strings of tokens
Background image of page 9

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

View Full DocumentRight Arrow Icon
CS 143 Lecture 5 10 Context-Free Grammars Programming language constructs have recursive structure An EXPR is if EXPR then EXPR else EXPR fi while EXPR loop EXPR pool Context-free grammars are a natural notation for this recursive structure
Background image of page 10
CS 143 Lecture 5 11 CFGs (Cont.) A CFG consists of A set of terminals T A set of non-terminals N A start symbol S (a non-terminal) A set of productions { } 1 2 where and n i X YY Y X N Y T N ε ∈ ∪ L
Background image of page 11

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

View Full DocumentRight Arrow Icon
CS 143 Lecture 5 12 Notational Conventions In these lecture notes Non-terminals are written upper-case Terminals are written lower-case The start symbol is the left-hand side of the first production
Background image of page 12
CS 143 Lecture 5 13 Examples of CFGs A fragment of Cool: EXPR if EXPR then EXPR else EXPR fi | while EXPR loop EXPR pool | id
Background image of page 13

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

View Full DocumentRight Arrow Icon
CS 143 Lecture 5 14 Examples of CFGs (cont.) Simple arithmetic expressions: ( 29 E E E | E + E | E | id
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/05/2010 for the course COMPILER AC 1 taught by Professor Sergio during the Spring '10 term at Institute of Management Technology.

Page1 / 58

lecture05 - Introduction to Parsing Le cture5 CS 143...

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

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