Plan for Today Regular Expressions: repetition and choice Context Free Grammars let : “a” | “b” | “c” word : let + – models for specifying programming languages syntax semantics What regular expressions cannot express: – example grammars nesting, e.g. matching parentheses: ( ) | (( )) | ((( ))) | … – derivations to any depth Parse trees Why? A DFA has only a finite # states and thus cannot encode that it has seen N “(“-s and thus now must Syntax-directed translation see N “)”-s for the parentheses to match (for any N). – Used syntax-directed translation to interpret expression language For that we need some form of recursion: Top-down Predictive Parsing S : “(“ S “)” | ε CS453 Lecture Context Free Grammar Intro 1 CS453 Lecture Context Free Grammar Intro 2 Context Free Grammars Syntax and Semantics CFG: set of productions of the form Regular Expressions define what correct tokens are Non-terminal phrase | phrase | phrase … Context Free Grammars define what correctly formed programs are phrase: string of terminals and non-terminals But… are all correctly formed programs meaningful? terminals: tokens of the language non-terminals represent sets of strings of tokens of the language Example: stmt ifStmt | whileStmt ifStmt IF OPEN boolExpr CLOSE Stmt whileStmt WHILE OPEN boolExpr CLOSE Stmt CS453 Lecture Context Free Grammar Intro 3 CS453 Lecture Context Free Grammar Intro 4
Our Next Class of Languages Syntax and Semantics Regular Expressions define what correct tokens are Context-Free Languages Context Free Grammars define what correctly formed programs are But… are all correctly formed programs meaningful? R n b n { ww } { a } NO: the program can have semantic errors some can be detected by the compiler: type errors, undefined errors some cannot: run-time errors, Regular Languages program does not compute what it is supposed to a * b * ( a | b )* The semantics of a program defines its meaning. Here, we do syntax directed translation / interpretation CS453 Lecture Context Free Grammar Intro 5 Context-Free Languages Example A context-free grammar : S → aSb G S → ε Context-Free Pushdown Grammars Automata A derivation: Recursive definitions S aSb aaSbb aabb ⇒ ⇒ ⇒ stack We will start here Another derivation: FSA + S aSb aaSbb aaaSbbb aaabbb ⇒ ⇒ ⇒ ⇒
An Application of this Language Deriving another grammar Context-Free Languages S → aSb Gave a S → ε Can we derive a grammar n b n Grammar for: { a } R for: { ww } n n L ( G ) { a b : n 0 } = ≥ Regular Languages (((( )))) Describes parentheses: Representing All Properly Nested Example Parentheses A context-free grammar : G S → aSa S → bSb S → aSb S → ε S → ε A derivation: n n L ( G ) { a b : n 0 } = ≥ S aSa abSba abba ⇒ ⇒ ⇒ (((( )))) Another derivation: Describes parentheses: S aSa abSba abaSaba abaaba ⇒ ⇒ ⇒ ⇒ Can we build a grammar to include any valid combination of ()? For example ( () (()) )
A Possible Grammar Context-Free Grammars S → ( S ) A context-free grammar : G Grammar G = ( V , T , S , P ) S → SS Nonterminals Terminals Start S → ε symbol A derivation: S ⇒ SS ⇒ ( S ) S ⇒ () S ⇒ () Productions of the form: Another derivation: A → x Nonterminal String of symbols, S ⇒ SS ⇒ ( S ) S ⇒ () S ⇒ ()( S ) ⇒ ()() Nonterminals and terminals Derivation Order Derivation, Language Given a grammar with rules: Grammar: G=(V,T,S,P) 1 . S → AB 2. A → aaA 4. B → Bb Derivation: 3. A → ε 5. B → ε Start with start symbol S Keep replacing non-terminals A by their RHS x, Always expand the leftmost non-terminal until no non-terminals are left The resulting string (sentence) is part of the language L(G) Leftmost derivation: 1 2 3 4 5 The Language L(G) defined by the CFG G: S AB aaAB aaB aaBb aab ⇒ ⇒ ⇒ ⇒ ⇒ L(G) = the set of all strings of terminals that can be derived this way CS453 Lecture Context Free Grammar Intro 15
Grammar String Derivation Order Stm --> id := Exp a := ( b := ( c := 3, 2 ), 1 ) Given a grammar with rules: Exp --> num Exp --> ( Stm, Exp ) 1 . S → AB 2. A → aaA 4. B → Bb Leftmost derivation: 3. A → ε 5. B → ε Stm ==> a := Exp ==> a := ( Stm, Exp ) ==> a := ( b := Exp, Exp ) ==> a := ( b := ( Stm, Exp ), Exp ) ==> a := ( b := ( c := Exp, Exp ), Exp ) Always expand the rightmost non-terminal ==> a := ( b := ( c := 3, Exp ), Exp ) ==> a := ( b := ( c := 3, 2), Exp ) ==> a := ( b := ( c := 3, 2), 1) Rightmost derivation: Rightmost derivation: Stm ==> a := Exp ==> a := ( Stm, Exp ) ==> a := ( Stm, 1) 1 4 5 2 3 ==> S AB ABb Ab aaAb aab ⇒ ⇒ ⇒ ⇒ ⇒ Parse Trees Parse Trees S → AB S → AB A → aaA | ε B → Bb | ε A → aaA | ε B → Bb | ε S AB aaAB ⇒ ⇒ S ⇒ AB S S A B A B a a A
Parse Trees Parse Trees S → AB S → AB A → aaA | ε B → Bb | ε A → aaA | ε B → Bb | ε S AB aaAB aaABb S AB aaAB aaABb aaBb ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ S S A B A B a a A B b a a A B b ε Parse Trees Sentential forms S → AB S → AB A → aaA | ε B → Bb | ε A → aaA | ε B → Bb | ε S AB aaAB aaABb aaBb aab ⇒ ⇒ ⇒ ⇒ ⇒ S S ⇒ AB A B yield Partial parse tree S aa εε b a a A B b A B = aab ε ε
Sometimes, derivation order doesn � t matter Leftmost: S AB aaAB sentential ⇒ ⇒ S AB aaAB aaB aaBb aab ⇒ ⇒ ⇒ ⇒ ⇒ form Partial parse tree Rightmost: S S AB ABb Ab aaAb aab ⇒ ⇒ ⇒ ⇒ ⇒ A B S Same parse tree A B a a A a a A B b ε ε Does it matter here? How about here? Parse Tree Grammar Grammar Stm --> id := Exp (1) exp --> exp * exp Exp --> num (2) exp --> exp + exp Exp --> ( Stm, Exp ) (3) exp --> NUM String String a := ( b := ( c := 3, 2 ), 1 ) 42 + 7 * 6 What does this question mean? CS453 Lecture Context Free Grammar Intro 27 CS453 Lecture Context Free Grammar Intro 28
Syntax Directed Translation or Interpretation Interpret this program Parse Tree Use parse tree to … Grammar – Translate one language to another Stm --> id := Exp – Create a data structure of the program Exp --> num – Interpret, or evaluate, the program Exp --> ( Stm, Exp ) Works conceptually by… String – Parser discovers the parse tree – Parser executes certain actions while � traversing � the parse tree using a a := ( b := ( c := 3, 2 ), 1 ) depth-first, post-order traversal CS453 Lecture Context Free Grammar Intro 29 CS453 Lecture Context Free Grammar Intro 30 How about here? Semantic Rules for Expression Example Grammar (1) exp --> exp * exp (2) exp --> exp + exp (3) exp --> NUM String 42 + 7 * 6 CS453 Lecture Context Free Grammar Intro 31 CS453 Lecture Context Free Grammar Intro 32
Example: Parser Parser derivation input S → SS derivation input grammar string S → aSb aabb ? S → bSa S →ε Exhaustive Search S → SS | aSb | bSa | ε Phase 1: Find derivation of S ⇒ SS S ⇒ SS aabb aabb S ⇒ aSb S ⇒ aSb S ⇒ bSa S ⇒ bSa S ⇒ ε S ⇒ ε All possible derivations of length 1
Phase 2 S → SS | aSb | bSa | ε S → SS | aSb | bSa | ε Phase 2 S SS SSS S SS SSS ⇒ ⇒ ⇒ ⇒ S SS aSbS aabb aabb S SS aSbS ⇒ ⇒ ⇒ ⇒ Phase 1 S SS bSaS S SS S ⇒ ⇒ ⇒ ⇒ S SS S SS S ⇒ ⇒ ⇒ S aSb aSSb ⇒ ⇒ S aSb ⇒ S aSb aSSb ⇒ ⇒ S aSb aaSbb ⇒ ⇒ S aSb aaSbb ⇒ ⇒ Phase 3 S aSb abSab ⇒ ⇒ S aSb aaSbb aabb ⇒ ⇒ ⇒ S aSb ab ⇒ ⇒ Final result of exhaustive search For general context-free grammars: (top-down parsing) Parser The exhaustive search approach is extremely costly: O(|P| |w| ) S ⇒ SS input S ⇒ aSb There exists a parsing algorithm aabb 3 | w | that parses a string w in time S ⇒ bSa S ⇒ ε For LL(1) grammars, a simple type of derivation CFGs that we will meet soon, we can | w | use Predictive parsing and parse in S aSb aaSbb aabb ⇒ ⇒ ⇒ time
Example Predictive Parser: Recursive Descent Recursive Descent Parsing start -> stmts EOF start -> stmts EOF stmts -> stmts -> stmt | stmt stmts | stmt stmts stmt -> ifStmt | whileStmt stmt -> ifStmt | whileStmt ifStmt -> IF id { stmts } ifStmt -> IF id { stmts } whileStmt -> WHILE id { stmts } whileStmt -> WHILE id { stmts } Each non-terminal becomes a method void start() { switch(m_lookahead) { that mimics the RHSs of the productions associated with it case IF, WHILE: stmts(); match(Token.Tag.EOF); break; default: throw new ParseException(…); and choses a particular RHS: }} void stmts() { switch(this.m_lookahead) { an alternative based on a look-ahead symbol case IF,WHILE: stmt(); stmts(); break; and throws an exception if no alternative applies case EOF: break; default: throw new ParseException(…); }} void stmt() { switch(m_lookahead) { case IF: ifStmt();break; When does this work? case WHILE: whileStmt(); break; default: throw new ParseException(…); }} void ifStmt() {switch(m_lookahead) { case IF: Check_next_Token(id); Check_next_Token(OPENBRACE); stmts(); Check_next_Token(CLOSEBRACE); break; default: throw new ParseException(…); }} CS453 Lecture Context Free Grammar Intro 41 CS453 Lecture Context Free Grammar Intro 42
Recommend
More recommend