50 likes | 299 Views
CS310. Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho, Sethi, Ullman aka: The Dragon Book Section 4.4 – 4.8 October 30, 2006. Top-Down Parsing. LL(1) parser parse from Left to right produces a Leftmost derivation
E N D
CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho, Sethi, Ullman aka: The Dragon Book Section 4.4 – 4.8 October 30, 2006
Top-Down Parsing • LL(1) parser • parse from Left to right • produces a Leftmost derivation • always replace the left-most nonterminal first • with 1 lookahead symbol • LL(1) Grammars • FIRST and FOLLOW uniquely determine which productions to use to parse a string • not all grammars are LL(1) • common prefixes • left recursion (talked about Wednesday)
Parsing with JFLAP • FIRST? FOLLOW? Parse Table? (1) S -> AcB (2) A -> aAb (3) A ->aBb (4) B -> ccb (5) B -> b (1) S -> AcB (2) A -> aAb (3) A ->Bb (4) B -> bBb (5) B -> cba (1) S -> zAB (2) S -> AB (3) A ->aA (4) A -> BC (5) B -> cb (6) B -> b (7) C -> c cbabccba bcbabbccba abbcba cbacbabba aaacbbbcacb abbcb abcabc zcbcb abcc zacbcb zabcbcb abcbcb
LL(2) Parse Table (1) S -> AcB (2) A -> aAb (3) A ->aBb (4) B -> ccb (5) B -> b • Two lookahead symbols
Bottom Up Parsing • Shift-reduce parsing • used in many automatic parser generators, YACC/Bison • Reduce the string to the start symbol • Shift a symbol from the string on to a stack (1) E -> E +E (2) E -> E * E (3) E ->( E ) (4) E -> x