Which statement correctly describes the main difference between top-down and bottom-up parsing?
Think about which end each parsing method starts from: the grammar or the input.
Top-down parsing starts from the grammar's start symbol and tries to generate the input string by applying production rules. Bottom-up parsing starts from the input string and tries to reduce it back to the start symbol by reversing productions.
Which of the following pairs correctly matches a parsing method with a common algorithm used for it?
Recall which parsing method uses recursive calls and which uses state machines.
Recursive descent parsers are a common implementation of top-down parsing. LR parsers are typical bottom-up parsers. Predictive parsers are a type of top-down parser. Recursive descent is not used for bottom-up parsing.
Why is left recursion a problem for top-down parsers but not for bottom-up parsers?
Consider how top-down parsers expand rules and how bottom-up parsers reduce input.
Top-down parsers expand rules starting from the start symbol, so left recursion causes infinite loops. Bottom-up parsers reduce input by matching patterns, so left recursion is handled naturally.
When do top-down and bottom-up parsers typically detect syntax errors during parsing?
Think about how each parser processes input and when it can tell something is wrong.
Top-down parsers try to match input from the start and can detect errors early when input doesn't match expected rules. Bottom-up parsers shift input and reduce later, so errors may be detected after more input is read.
Given an ambiguous grammar that can be parsed by both top-down and bottom-up methods, which parsing approach is generally more suitable and why?
Consider which parsing method is more powerful in handling complex grammar structures.
Bottom-up parsers, such as LR parsers, can handle ambiguous grammars better by using shift-reduce parsing and conflict resolution strategies. Top-down parsers struggle with ambiguity and often require grammar rewriting.