COP 3223H meeting -*- Outline -*- * grammar background ** background on grammars This is background for recursion. ------------------------------------------ CONTEXT-FREE GRAMMARS Example (statements in Python): ::= | assert | pass | return | return | ( ) | ( ) | if : else: | ... ::= = | ... ::= | , ::= ::= | ::= | + | ... ------------------------------------------ Explain the notation, point out nonterminals, terminals, alternatives Note where the grammar is recursive, and recursion patterns Q: What is the relationship between a grammar and a language? The grammar defines the set of programs in the language The grammar can be used to parse (check) programs in the language. Q: What are some examples in the grammar for ? x = 5 if b > 3: y = 4 else: pass ------------------------------------------ DERIVATIONS 1. Start with a nonterminal 2. Replace any nonterminal with the rhs of a production for that nonterminal 3. If there are nonterminals left, go to 2 Example: --> --> = --> x = --> x = --> x = 5 ------------------------------------------ we take advantage of the lexical grammar (not shown) that tells us that x is an and 5 is an ------------------------------------------ PARSING AND PARSE TREES Parsing: 1. Lexical grammar classifies strings (e.g., x is an , 5 is a ) 2. Find a production whose right hand side appears in the tree tops, reduce the right hand side to the nonterminal on the left hand side, by making a tree with that left hand side as the root and branches to the right hand side tree tops. 3. If haven't reached the goal symbol (e.g., ), go to step 2. x = 5 | | x = 5 | | | x = 5 | | \ | | | | | | | | | x = 5 | | | \ | | | | | | | | | x = 5 ------------------------------------------