{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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 Lecture5
Background image of page 1

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

View Full Document Right Arrow Icon
CS 143 Lecture 5 2 Outline Regular languages revisited Parser overview Context-freegrammars (CFG’s) Derivations Ambiguity
Background image of page 2
CS 143 Lecture 5 3 Languages and Automata Formal languages arevery important in CS Especially in programming languages Regular languages The weakest formal languages widely used Many applications Wewill also study context-freelanguages, treelanguages
Background image of page 3

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

View Full Document Right Arrow Icon
CS 143 Lecture 5 4 Beyond Regular Languages Many languages arenot regular Strings of balanced parentheses arenot 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 finiteautomaton that runs long enough must repeat states Finiteautomaton 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 Document Right Arrow Icon
CS 143 Lecture 5 6 The Functionality of the Parser Input: sequenceof tokens fromlexer Output: parsetreeof theprogram (But someparsers never producea parsetree. . .)
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 Document Right 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 areprograms . . . . . . Parser must distinguish between valid and invalid strings of tokens Weneed 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 Document Right Arrow Icon
CS 143 Lecture 5 10 Context-Free Grammars Programming languageconstructs haverecursivestructure An EXPR is if EXPR then EXPR elseEXPR fi while EXPR loop EXPR pool Context-freegrammars area natural notation for this recursivestructure
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 Document Right Arrow Icon
CS 143 Lecture 5 12 Notational Conventions In theselecturenotes Non-terminals are written upper-case Terminals arewritten lower-case The start symbol is theleft-hand sideof 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 Document Right Arrow Icon
CS 143 Lecture 5 14 Examples of CFGs (cont.) Simplearithmetic 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.

{[ snackBarMessage ]}