LL: - top-down, chooses the next production to apply based on k symbol lookahead. - for exercises we will usually consider LL(1) grammars - when given a nonterminal, it must choose which RHS to expand to using only a single token of lookahead. - less powerful than LR: - consider this LR(0) grammar: - S -> A | B A -> a a B -> a b - not LL(1) (FIRST(A) intersect FIRST(B) = {a} =/= null) - substitute: - S -> a a | a b - left factor: S -> a C C -> a | b - immediate left recursion: - open general algorithm: - http://en.wikipedia.org/wiki/Left_recursion#Removing_left_recursion - example from book: - C for ClockNoise - C -> C tick tock C -> tick tock - C -> tick tock C' C' -> epsilon C' -> tick tock C' - more general example including indirect left recursion: - scroll down in browser - A -> B a B -> A b B -> b - what does this generate? - (b a)+ - A -> B a B -> B a b B -> b - now remove direct left recursion: - A -> B a B -> b B' B' -> epsilon B' -> a b B' - is this LL(1)? - no, we can't choose between the two B' rules, because: - B' is nullable - a can follow B' - one of the productions starts with a - substitute: - A -> B B -> b B' a B' -> epsilon B' -> a b B' - A -> B B -> b C C -> B' a B' -> epsilon B' -> a b B' - A -> B B -> b C C -> a C -> a b B' a B' -> epsilon B' -> a b B' - A -> B B -> b C C -> a C -> a b C - A -> b C C -> a C -> a b C - rename: - A -> b B B -> a B -> a b B - left factor: - A -> b B B -> a B' B' -> epsilon B' -> b B - rename: - A -> b B B -> a C C -> epsilon C -> b B - if time: show parser and generator midterm review: - http://courses.cs.washington.edu/courses/cse401/15wi/exams/midterm-topics.html