This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Parsers Monday, September 13, 2010 Agenda • Terminology • LL(1) Parsers • Overview of LR Parsing Monday, September 13, 2010 Terminology • Grammar G = (V t , V n , S, P) • V t is the set of terminals • V n is the set of nonterminals • S is the start symbol • P is the set of productions • Each production takes the form: V n ! !  (V n  V t )+ • Grammar is contextfree (why?) • A simple grammar: G = ({a, b}, {S, A, B}, {S ! A B $, A ! A a, A ! a, B ! B b, B ! b}, S) Monday, September 13, 2010 Terminology • V is the vocabulary of a grammar, consisting of terminal (V t ) and nonterminal (V n ) symbols • For our sample grammar • V n = {S, A, B} • Nonterminals are symbols on the LHS of a production • Nonterminals are constructs in the language that are recognized during parsing • V t = {a, b} • Terminals are the tokens recognized by the scanner • They correspond to symbols in the text of the program Monday, September 13, 2010 Terminology • Productions (rewrite rules) tell us how to derive strings in the language • Apply productions to rewrite strings into other strings • We will use the standard BNF form • P = { S ! A B $ A ! A a A ! a B ! B b B ! b } Monday, September 13, 2010 Generating strings • Given a start rule, productions tell us how to rewrite a nonterminal into a different set of symbols • By convention, ¡rst production applied has the start symbol on the left, and there is only one such production S ! A B $ A ! A a A ! a B ! B b B ! b To derive the string “ a a b b b ” we can do the following rewrites: S 㱺 A B $ 㱺 A a B $ 㱺 a a B $ 㱺 a a B b $ 㱺 a a B b b $ 㱺 a a b b b $ Monday, September 13, 2010 Terminology • Strings are composed of symbols • A A a a B b b A a is a string • We will use Greek letters to represent strings composed of both terminals and nonterminals • L(G) is the language produced by the grammar G • All strings consisting of only terminals that can be produced by G • In our example, L(G) = a+b+$ • All regular expressions can be expressed as grammars for contextfree languages, but not viceversa • Consider: a i b i $ (what is the grammar for this?) Monday, September 13, 2010 Parse trees • Tree which shows how a string was produced by a language • Interior nodes of tree: non terminals • Children: the terminals and nonterminals generated by applying a production rule • Leaf nodes: terminals S A B A a B b B b b a Monday, September 13, 2010 Leftmost derivation • Rewriting of a given string starts with the leftmost symbol • Exercise: do a leftmost derivation of the input program F(V + V) using the following grammar: • What does the parse tree look like?...
View
Full
Document
This note was uploaded on 02/19/2012 for the course ECE 468 taught by Professor Test during the Fall '08 term at Purdue.
 Fall '08
 TEST

Click to edit the document details