Recently reading the prestigious "Dragon book", read the section of the grammatical analysis, about the compiler of the grammatical Analysis section, Dragon book mainly on the top-down grammar analysis and bottom-up analysis of two types of parsing methods, top-down grammar analysis mainly corresponds to ll grammar, and the bottom-up of the grammatical analysis mainly corresponds to the LR grammar, The details of the grammar are not discussed here. This paper mainly discusses the advantages and disadvantages of ll grammar and LR Grammar, and summarizes what grammar should be used to design language or compiler when appropriate.
The following are comparisons of the complexity, grammatical universality, error recovery, parsing table size, and analysis speed written by the parser. 1. complexity complexity refers to the complexity of the parser writing.
This is the LL parser dominance, and anyone who knows the details of LL grammar and LR Grammar should be able to understand this. If you have written the parser yourself, then the top-down parser (recursive down) of the program code should be more clear-cut, and the bottom-up parser code is not so easy to understand, especially when you see the LALR Analyzer Analysis table, will be crazy: P. 2. Universality
Universality refers to the number of context-independent grammars that the grammar can support. The following diagram is a relationship between context-independent grammars, LR grammars, and LL grammars:
This is the LR grammar outright, the LR grammar can support more context-independent grammars, because when a grammar is ll grammar, it must also be the LR grammar. The biggest difference between ll grammar and LR Grammar is that for LL grammar, you first have to extract the left factor from your own grammar and eliminate the left recursion, and the LR grammar does not have to consider this step because it is happy to accept grammars with left recursion or a common left factor.
Many existing computer languages already have lalr grammars, and it is not very difficult (not absolute) for you to convert LALR grammars into ll grammars. 3. Error Recovery because the LL-class parsing algorithm uses a top-down structure and is often written manually (the LR parser usually uses generators to generate), and you write it, it is easy to control the parsing process of the grammar, such as whether the program code is missing a semicolon, Curly braces, so the LL parser has better contextual information, so it's better at error recovery. 4. Parse Table size here is ll grammar more dominant, if you take the same syntax of LL analysis table and LR analysis table to compare, the results are obvious.
5. Parsing Speed This is inconclusive, depending on the pros or cons of your algorithm implementation or the parser generation tool.
from the above comparisons, ll grammars are better than LR grammars with their ease of implementation and ease of control, but this is not absolute, for example, when there is a large number of left and common left factors in the context-independent grammar of the language you are designing, You have to expend a lot of effort to modify the grammar to eliminate left recursion and extract left factor, which is not as simple as LR grammar, so it has to be chosen according to the actual situation in practical development.
Reference Document: 1. http://programmers.stackexchange.com/questions/19541/ What-are-the-main-advantages-and-disadvantages-of-ll-and-lr-parsing
2. http://stackoverflow.com/ questions/2842809/lexers-vs-parsers/22307370