{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec09mod

# lec09mod - CS415 Compilers Syntax Analysis CFG derivations...

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

CS415 Compilers Syntax Analysis CFG, derivations, ambiguity, introduction to parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

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

View Full Document
Lecture 9 2 cs415, spring 10 Parsing (Syntax Analysis) EAC Chapters 3.1 - 3.2
Lecture 9 3 cs415, spring 10 Review: The Front End Parser Checks the stream of words and their parts of speech (produced by the scanner) for grammatical correctness Determines if the input is syntactically well formed Guides checking at deeper levels than syntax Builds an IR representation of the code Source code Scanner IR Parser Errors tokens

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

View Full Document
Lecture 9 4 cs415, spring 10 The Study of Parsing The process of discovering a derivation for some sentence Need a mathematical model of syntax — a grammar G Need an algorithm for testing membership in L(G) Need to keep in mind that our goal is building parsers, not studying the mathematics of arbitrary languages Roadmap 1 Context-free grammars and derivations 2 Top-down parsing LL(1) parsers, hand-coded recursive descent parsers 3 Bottom-up parsing Automatically generated LR(1) parsers
Lecture 9 5 cs415, spring 10 Specifying Syntax with a Grammar Context-free syntax is specified with a context-free grammar SheepNoise SheepNoisebaa | baa This CFG defines the set of noises sheep normally make It is written in a variant of Backus–Naur form Formally, a grammar is a four tuple, G = (S,N,T,P) Sis the start symbol (set of strings in L(G)) Nis a set of non-terminal symbols (syntactic variables) Tis a set of terminal symbols (words) Pis a set of productionsor rewrite rules (P : N ( T) + )

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

View Full Document
Lecture 9 6 cs415, spring 10 Deriving Syntax We can use the SheepNoise grammar to create sentences use the productions as rewriting rules Rule Sentential Form SheepNoise 2b a a 1 SheepNoise baa a a b a a 1 SheepNoise baa 1 SheepNoise baa baa a a b a a baa And so on ...
Lecture 9 7 cs415, spring 10 A More Useful Grammar To explore the uses of CFG s,we need a more complex grammar Such a sequence of rewrites is called a derivation Process of discovering a derivation is called parsing We denote this derivation: Expr * id –num * id 1 Expr Op Expr 2 number 3 id 4 Op + 5 6 * 7 / Rule Sentential Form Expr 1 Expr Op Expr 3 < id, x > Op Expr 5 < id, x > 1 < id, x > 2 < id, x > < num, 2 > 6 < id, x > < num, 2 > * 3 < id, x > < num, 2 > * < id, y >

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.

{[ snackBarMessage ]}

### Page1 / 23

lec09mod - CS415 Compilers Syntax Analysis CFG derivations...

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

View Full Document
Ask a homework question - tutors are online