Intermediate Topic 24 of 50 Languages & Compilers
LL and LR Parsing
Families of deterministic parsing algorithms.
How it works
LL parsers read input left to right and build a leftmost derivation top-down, predicting which production to apply from lookahead tokens, using a table or recursive functions; they require grammars without left recursion and common prefixes. LR parsers read left to right and build a rightmost derivation in reverse, bottom-up, using a stack with shift and reduce actions driven by a state table, and handle a larger class of grammars, including left-recursive ones. Variants include SLR, LALR and canonical LR.
Step-by-step walkthrough
- LL parsers read left to right and build the tree top-down.
- They predict which rule to apply by looking ahead.
- LR parsers also read left to right and build bottom-up.
- They shift tokens onto a stack and reduce them with rules.
- LR handles more grammars, LL is easier to write by hand.
Where it is used
Parser generators such as ANTLR for LL(*) and Yacc, Bison and Menhir for LALR and LR are used to build compilers and tools.
Common pitfall
Grammar conflicts, shift/reduce and reduce/reduce, indicate ambiguity or insufficient lookahead and must be resolved by rewriting rules or specifying precedence. LR tables are big and errors can be cryptic. Semantic actions mixed into grammars become hard to maintain. Use precedence declarations carefully and test thoroughly.
Where it fits in the course
LL and LR Parsing is topic 24 of 50 in Languages & Compilers, rated Intermediate. It follows Recursive Descent Parsing. The next topic is Parser Generators.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Languages & Compilers quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is LL and LR Parsing?
Where do people use LL and LR Parsing?
What is the most common mistake with LL and LR Parsing?
What should I learn before this topic?
How can I practice it?
Is LL and LR Parsing hard to learn?
How long does it take to learn LL and LR Parsing?
Which topics are related to LL and LR Parsing?
What should I study after LL and LR Parsing?
Related topics
- Recursive Descent Parsing (Languages & Compilers)
- Parser Generators (Languages & Compilers)
- Compiler Phases (Languages & Compilers)
- Intermediate Representations (Languages & Compilers)