Parsing - Syntax Analysis Context-free grammar Top-down and...

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

View Full Document Right Arrow Icon
cs5363 1 Syntax Analysis Context-free grammar Top-down and bottom-up parsing
Background image of page 1

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

View Full DocumentRight Arrow Icon
cs5363 2 Front end Source program for (w = 1; w < 100; w = w * 2); Input: a stream of characters ‘f’ ‘o’ ‘r’ ‘(’ `w’ ‘=’ ‘1’ ‘;’ ‘w’ ‘<’ ‘1’ ‘0’ ‘0’ ‘;’ ‘w’… Scanning--- convert input to a stream of words (tokens) “for” “(“ “w” “=“ “1” “;” “w” “<“ “100” “;” “w”… Parsing---discover the syntax/structure of sentences forStmt assign less assign emptyStmt Lv(w) int(1) Lv(w) int(100) Lv(w) Lv(w) mult int(2)
Background image of page 2
cs5363 3 Context-free Syntax Analysis Goal: recognize the structure of programs Description of the language Context-free grammar Parsing: discover the structure of an input string Reject the input if it cannot be derived from the grammar
Background image of page 3

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

View Full DocumentRight Arrow Icon
cs5363 4 Describing context-free syntax Describe how to recursively compose programs/sentences from tokens forStmt: “for” “(” expr “;” expr “;” expr “)” stmt expr: expr + expr | expr – expr | expr * expr | expr / expr | ! expr …… stmt: assignment | forStmt | whileStmt | ……
Background image of page 4
cs5363 5 Context-free Grammar A context-free grammar includes (T,NT,S,P) A set of tokens or terminals --- T Atomic symbols in the language A set of non-terminals --- NT Variables representing constructs in the language A set of productions --- P Rules identifying components of a construct BNF: each production has format A ::= B (or A B) where A is a single non-terminal B is a sequence of terminals and non-terminals A start non-terminal --- S The main construct of the language Backus-Naur Form: textual formula for expressing context- free grammars
Background image of page 5

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

View Full DocumentRight Arrow Icon
cs5363 6 Example: simple expressions BNF: a collection of production rules e ::= n | e+e | e - e | e * e | e / e Non-terminals: e Terminal (token): n, +, -, *, / Start symbol: e Using CFG to describe regular expressions n ::= d n | d d ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Derivation: top-down replacement of non-terminals Each replacement follows a production rule One or more derivations exist for each program Example: derivations for 5 + 15 * 20 e=> e*e => e+e*e => 5+e*e => 5+15*e=>5+15*20 e=> e+e => 5+e => 5+e*e =>5 +15*e => 5+15*20
Background image of page 6
cs5363 7 Parse trees and derivations Given a CFG G=(T,NT,P,S), a sentence si belongs to L(G) if there is a derivation from S to si Left-most derivation replace the left-most non-terminal at each step Right-most derivation replace the right-most non-terminal at each step Parse tree: graphical representation of derivations e e e e e 5 * + 15 20 e e e e 5 + * e 15 20 Parse trees: Grammar: e ::= n | e+e | e - e | e * e | e / e Sentence: 5 + 15 * 20 Derivations: e=> e*e => e+e*e => 5+e*e => 5+15*e=>5+15*20 e=> e+e => 5+e => 5+e*e =>5 +15*e => 5+15*20
Background image of page 7

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

View Full DocumentRight Arrow Icon
cs5363 8 Languages defined by CFG e ::= num | string | id | e+e
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 42

Parsing - Syntax Analysis Context-free grammar Top-down and...

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

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