# Top-down parsing

﻿
Top-down parsing

Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis. It occurs in the analysis of both natural languages and computer languages.

Top-down parsing can be viewed as an attempt to find left-most derivations of an input-stream by searching for parse-trees using a top-down expansion of the given formal grammar rules. Tokens are consumed from left to right. Inclusive choice is used to accommodate ambiguity by expanding all alternative right-hand-sides of grammar rules. Aho, A.V., Sethi, R. and Ullman ,J.D. (1986) " Compilers: principles, techniques, and tools." " Addison-Wesley Longman Publishing Co., Inc. Boston, MA, USA. " ]

Simple implementations of top-down parsing do not terminate for left-recursive grammars, and top-down parsing with backtracking may have exponential time complexity with respect to the length of the input for ambiguous CFGs Aho, A.V and Ullman, J. D. (1972) " The Theory of Parsing, Translation, and Compiling, Volume 1: Parsing." "Englewood Cliffs, N.J.: Prentice-Hall." ] . However, more sophisticated top-down parsers have been created by Frost, Hafiz, and Callaghan Frost, R., Hafiz, R. and Callaghan, P. (2007) " Modular and Efficient Top-Down Parsing for Ambiguous Left-Recursive Grammars ." "10th International Workshop on Parsing Technologies (IWPT), ACL-SIGPARSE ", Pages: 109 - 120, June 2007, Prague.] Frost, R., Hafiz, R. and Callaghan, P. (2008) " Parser Combinators for Ambiguous Left-Recursive Grammars." " 10th International Symposium on Practical Aspects of Declarative Languages (PADL), ACM-SIGPLAN ", Volume 4902/2008, Pages: 167-181, January 2008, San Francisco.] which do accommodate ambiguity and left recursion in polynomial time and which generate polynomial-sized representations of the potentially-exponential number of parse trees.

Programming language application

A compiler parses input from a programming language to assembly language or an internal representation by matching the incoming symbols to Backus-Naur form production rules. An LL parser, also called a top-bottom parser or top-down parser, applies each production rule to the incoming symbols by working from the left-most symbol yielded on a production rule and then proceeding to the next production rule for each non-terminal symbol encountered. In this way the parsing starts on the Left of the result side (right side) of the production rule and evaluates non-terminals from the Left first and, thus, proceeds down the parse tree for each new non-terminal before continuing to the next symbol for a production rule.

For example:

* $A ightarrow aBC$
* $B ightarrow c|cd$
* $C ightarrow df|eg$

would match $A ightarrow aBC$ and attempt to match $B ightarrow c|cd$ next. Then $C ightarrow df|eg$ would be tried. As one may expect, some languages are more ambiguous than others. For a non-ambiguous language in which all productions for a non-terminal produce distinct strings: the string produced by one production will not start with the same symbol as the string produced by another production. A non-ambiguous language may be parsed by an LL(1) grammar where the (1) signifies the parser reads ahead one token at a time. For an ambiguous language to be parsed by an LL parser, the parser must lookahead more than 1 symbol, e.g. LL(3).

The common solution is to use an LR parser (also known as bottom-up or shift-reduce parser).

Accommodating left recursion in top-down parsing

A formal grammar that contains left recursion cannot be parsed by a naive recursive descent parser unless they are converted to a weakly equivalent right-recursive form. However, recent research demonstrates that it is possible to accommodate left-recursive grammars (along with all other forms of general CFGs) in a more sophisticated top-down parser by use of curtailment. A recognition algorithm which accommodates ambiguous grammars and curtails an ever-growing direct left-recursive parse by imposing depth restrictions w.r.t. input length and current input position, is described by Frost and Hafiz in 2006 Frost, R. and Hafiz, R. (2006) " A New Top-Down Parsing Algorithm to Accommodate Ambiguity and Left Recursion in Polynomial Time." "ACM SIGPLAN Notices", Volume 41 Issue 5, Pages: 46 - 54.] . That algorithm was extended to a complete parsing algorithm to accommodate indirect (by comparing previously-computed context with current context) as well as direct left-recursion in polynomial time, and to generate compact polynomial-size representations of the potentially-exponential number of parse trees for highly-ambiguous grammars by Frost, Hafiz and Callaghan in 2007 Frost, R., Hafiz, R. and Callaghan, P. (2007) " Modular and Efficient Top-Down Parsing for Ambiguous Left-Recursive Grammars ." "10th International Workshop on Parsing Technologies (IWPT), ACL-SIGPARSE ", Pages: 109 - 120, June 2007, Prague.] . The algorithm has since been implemented as a set of parser combinators written in the Haskell programming language. The implementation details of these new set of combinators can be found in a paper Frost, R., Hafiz, R. and Callaghan, P. (2008) " Parser Combinators for Ambiguous Left-Recursive Grammars." " 10th International Symposium on Practical Aspects of Declarative Languages (PADL), ACM-SIGPLAN ", Volume 4902/2008, Pages: 167-181, January 2008, San Francisco.] by the above-mentioned authors, which was presented in PADL'08.The [http://www.cs.uwindsor.ca/~hafiz/proHome.html X-SAIGA] site has more about the algorithms and implementation details.

Time and space complexity of top-down parsing

When top-down parser tries to parse an ambiguous input w.r.t. an ambiguous CFG, it may need exponential number of steps (w.r.t. the length of the input) to try all alternatives of the CFG in order to produce all possible parse trees, which eventually would require exponential memory space. The problem of exponential time complexity in top-down parsers constructed as sets of mutually-recursive functions has been solved by Norvig in 1991. Norvig, P. (1991) “Techniques for automatic memoisation with applications to context-free parsing.” "Journal - Computational Linguistics" Volume 17, Issue 1, Pages: 91 - 98. ] His technique is similar to the use of dynamic programming and state-sets in Earley's algorithm (1970), and tables in the CYK algorithm of Cocke, Younger and Kasami. The key idea is to store results of applying a parser ` p ` at position ` j ` in a memotable and to reuse results whenever the same situation arises. Frost, Hafiz and Callaghan Frost, R., Hafiz, R. and Callaghan, P. (2007) " Modular and Efficient Top-Down Parsing for Ambiguous Left-Recursive Grammars ." "10th International Workshop on Parsing Technologies (IWPT), ACL-SIGPARSE ", Pages: 109 - 120, June 2007, Prague.] Frost, R., Hafiz, R. and Callaghan, P. (2008) " Parser Combinators for Ambiguous Left-Recursive Grammars." " 10th International Symposium on Practical Aspects of Declarative Languages (PADL), ACM-SIGPLAN ", Volume 4902/2008, Pages: 167-181, January 2008, San Francisco.] also use memoization for refraining redundant computations to accommodate any form of CFG in polynomial time (Θ(n4) for left-recursive grammars and Θ(n3) for non left-recursive grammars). Their top-down parsing algorithm also requires polynomial space for potentially exponential ambiguous parse trees by 'compact representation' and 'local ambiguities grouping'. Their compact representation is comparable with Tomita’s compact representation of bottom-up parsing. Tomita, M. (1985) “Efficient Parsing for Natural Language.” "Kluwer, Boston, MA". ]

ee also

* Bottom-up parsing
* Parsing
* Recursive descent parser

References

* [http://www.cs.uwindsor.ca/~hafiz/proHome.html X-SAIGA] - eXecutable SpecificAtIons of GrAmmars

Wikimedia Foundation. 2010.

### Look at other dictionaries:

• Top-down parsing language — (TDPL) is a type of analytic formal grammar developed by Alexander Birman in the early 1970s in order to study formally the behavior of a common class of practical top down parsers that support a limited form of backtracking. Birman originally… …   Wikipedia

• Top-down and bottom-up design — Top down and bottom up are strategies of information processing and knowledge ordering, mostly involving software, but also other humanistic and scientific theories (see systemics). In practice, they can be seen as a style of thinking and… …   Wikipedia

• Top-down (disambiguation) — Top down may refer to:In systems theory, engineering and software: * Top down and bottom up design * Top down parsingIn computer games: * Top down perspective, a camera angle in computer and video games * Top down shooter, the computer and video… …   Wikipedia

• Parsing — In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of tokens to determine their grammatical structure with respect to a given (more or less) formal grammar.Parsing is also… …   Wikipedia

• Parsing expression grammar — A parsing expression grammar, or PEG, is a type of analytic formal grammar that describes a formal language in terms of a set of rules for recognizing strings in the language. A parsing expression grammar essentially represents a recursive… …   Wikipedia

• Parsing — Ein Parser [ˈpɑːɹsɚ] (engl. to parse „analysieren“ bzw. von lateinisch pars „Teil“; im Deutschen gelegentlich auch Zerteiler) ist ein Computerprogramm, das in der Computertechnik für die Zerlegung und Umwandlung einer beliebigen Eingabe in ein… …   Deutsch Wikipedia

• Bottom-up parsing — (also known as shift reduce parsing) is a strategy for analyzing unknown data relationships that attempts to identify the most fundamental units first, and then to infer higher order structures from them. It attempts to build trees upward toward… …   Wikipedia

• Chart Parsing — Ein Chartparser ist ein Parser für kontextfreie Grammatiken, der sich Teilanalysen (Teilkonstituenten) in einer Tabelle (Chart) merkt. Diese Zwischenspeicherung und Wiederverwendung von Teilanalysen verbessert die Effizienz erheblich und macht… …   Deutsch Wikipedia

• Memoization — Not to be confused with Memorization. In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs.… …   Wikipedia

• Parser Combinator — In mathematics and functional programming, Higher Order functions (HOF) are defined as the functions that can take functions as their input and can also produce functions as their output. The use of a HOF as an infix operator in a function… …   Wikipedia