- Source: Packrat parser
The Packrat parser is a type of parser that shares similarities with the recursive descent parser in its construction. However, it differs because it takes parsing expression grammars (PEGs) as input rather than LL grammars.
In 1970, Alexander Birman laid the groundwork for packrat parsing by introducing the "TMG recognition scheme" (TS), and "generalized TS" (gTS). TS was based upon Robert M. McClure's TMG compiler-compiler, and gTS was based upon Dewey Val Schorre's META compiler-compiler.
Birman's work was later refined by Aho and Ullman; and renamed as Top-Down Parsing Language (TDPL), and Generalized TDPL (GTDPL), respectively. These algorithms were the first of their kind to employ deterministic top-down parsing with backtracking.
Bryan Ford developed PEGs as an expansion of GTDPL and TS. Unlike CFGs, PEGs are unambiguous and can match well with machine-oriented languages. PEGs, similar to GTDPL and TS, can also express all LL(k) and LR(k). Bryan also introduced Packrat as a parser that uses memoization techniques on top of a simple PEG parser. This was done because PEGs have an unlimited lookahead capability resulting in a parser with exponential time performance in the worst case.
Packrat keeps track of the intermediate results for all mutually recursive parsing functions. Each parsing function is only called once at a specific input position. In some instances of packrat implementation, if there is insufficient memory, certain parsing functions may need to be called multiple times at the same input position, causing the parser to take longer than linear time.
Syntax
The packrat parser takes in input the same syntax as PEGs: a simple PEG is composed of terminal and nonterminal symbols, possibly interleaved with operators that compose one or several derivation rules.
= Symbols
=Nonterminal symbols are indicated with capital letters (e.g.,
{
S
,
E
,
F
,
D
}
{\displaystyle \{S,E,F,D\}}
)
Terminal symbols are indicated with lowercase (e.g.,
{
a
,
b
,
z
,
e
,
g
}
{\displaystyle \{a,b,z,e,g\}}
)
Expressions are indicated with lower-case Greek letter (e.g.,
{
α
,
β
,
γ
,
ω
,
τ
}
{\displaystyle \{\alpha ,\beta ,\gamma ,\omega ,\tau \}}
)
Expressions can be a mix of terminal symbols, nonterminal symbols and operators
= Operators
== Rules
=A derivation rule is composed by a nonterminal symbol and an expression
S
→
α
{\displaystyle S\rightarrow \alpha }
.
A special expression
α
s
{\displaystyle \alpha _{s}}
is the starting point of the grammar. In case no
α
s
{\displaystyle \alpha _{s}}
is specified, the first expression of the first rule is used.
An input string is considered accepted by the parser if the
α
s
{\displaystyle \alpha _{s}}
is recognized. As a side-effect, a string
x
{\displaystyle x}
can be recognized by the parser even if it was not fully consumed.
An extreme case of this rule is that the grammar
S
→
x
∗
{\displaystyle S\rightarrow x*}
matches any string.
This can be avoided by rewriting the grammar as
S
→
x
∗
!
.
{\displaystyle S\rightarrow x*!.}
= Example
={
S
→
A
/
B
/
D
A
→
'a'
S
'a'
B
→
'b'
S
'b'
D
→
(
'0'
−
'9'
)
?
{\displaystyle {\begin{cases}S\rightarrow A/B/D\\A\rightarrow {\textbf {'a'}}\ S\ {\textbf {'a'}}\\B\rightarrow {\textbf {'b'}}\ S\ {\textbf {'b'}}\\D\rightarrow ({\textbf {'0'}}-{\textbf {'9'}})?\end{cases}}}
This grammar recognizes a palindrome over the alphabet
{
a
,
b
}
{\displaystyle \{a,b\}}
, with an optional digit in the middle.
Example strings accepted by the grammar include:
'aa'
{\displaystyle {\textbf {'aa'}}}
and
'aba3aba'
{\displaystyle {\textbf {'aba3aba'}}}
.
= Left recursion
=Left recursion happens when a grammar production refers to itself as its left-most element, either directly or indirectly. Since Packrat is a recursive descent parser, it cannot handle left recursion directly. During the early stages of development, it was found that a production that is left-recursive can be transformed into a right-recursive production. This modification significantly simplifies the task of a Packrat parser. Nonetheless, if there is an indirect left recursion involved, the process of rewriting can be quite complex and challenging. If the time complexity requirements are loosened from linear to superlinear, it is possible to modify the memoization table of a Packrat parser to permit left recursion, without altering the input grammar.
= Iterative combinator
=The iterative combinator
α
+
{\displaystyle \alpha +}
,
α
∗
{\displaystyle \alpha *}
, needs special attention when used in a Packrat parser. As a matter of fact, the use of iterative combinators introduces a secret recursion that does not record intermediate results in the outcome matrix. This can lead to the parser operating with a superlinear behaviour. This problem can be resolved apply the following transformation:
With this transformation, the intermediate results can be properly memoized.
Memoization technique
Memoization is an optimization technique in computing that aims to speed up programs by storing the results of expensive function calls. This technique essentially works by caching the results so that when the same inputs occur again, the cached result is simply returned, thus avoiding the time-consuming process of re-computing. When using packrat parsing and memoization, it's noteworthy that the parsing function for each nonterminal is solely based on the input string. It does not depend on any information gathered during the parsing process. Essentially, memoization table entries do not affect or rely on the parser's specific state at any given time.
Packrat parsing stores results in a matrix or similar data structure that allows for quick look-ups and insertions. When a production is encountered, the matrix is checked to see if it has already occurred. If it has, the result is retrieved from the matrix. If not, the production is evaluated, the result is inserted into the matrix, and then returned. When evaluating the entire
m
∗
n
{\displaystyle m*n}
matrix in a tabular approach, it would require
Θ
(
m
n
)
{\displaystyle \Theta (mn)}
space. Here,
m
{\displaystyle m}
represents the number of nonterminals, and
n
{\displaystyle n}
represents the input string size.
In a naïve implementation, the entire table can be derived from the input string starting from the end of the string.
The Packrat parser can be improved to update only the necessary cells in the matrix through a depth-first visit of each subexpression tree. Consequently, using a matrix with dimensions of
m
∗
n
{\displaystyle m*n}
is often wasteful, as most entries would remain empty. These cells are linked to the input string, not to the nonterminals of the grammar. This means that increasing the input string size would always increase memory consumption, while the number of parsing rules changes only the worst space complexity.
= Cut operator
=Another operator called cut has been introduced to Packrat to reduce its average space complexity even further. This operator utilizes the formal structures of many programming languages to eliminate impossible derivations. For instance, control statements parsing in a standard programming language is mutually exclusive from the first recognized token, e.g.,
{
i
f
,
d
o
,
w
h
i
l
e
,
s
w
i
t
c
h
}
{\displaystyle \{if,do,while,switch\}}
.
When a Packrat parser uses cut operators, it effectively clears its backtracking stack. This is because a cut operator reduces the number of possible alternatives in an ordered choice. By adding cut operators in the right places in a grammar's definition, the resulting Packrat parser only needs a nearly constant amount of space for memoization.
The algorithm
Sketch of an implementation of a Packrat algorithm in a Lua-like pseudocode.
Example
Given the following context, a free grammar that recognizes simple arithmetic expressions composed of single digits interleaved by sum, multiplication, and parenthesis.
{
S
→
A
A
→
M
'+'
A
/
M
M
→
P
'*'
M
/
P
P
→
'('
A
')'
/
D
D
→
(
'0'
−
'9'
)
{\displaystyle {\begin{cases}S\rightarrow A\\A\rightarrow M\ {\textbf {'+'}}\ A\ /\ M\\M\rightarrow P\ {\textbf {'*'}}\ M\ /\ P\\P\rightarrow {\textbf {'('}}\ A\ {\textbf {')'}}\ /\ D\\D\rightarrow ({\textbf {'0'}}-{\textbf {'9'}})\end{cases}}}
Denoted with
⊣
{\displaystyle \dashv }
the line terminator we can apply the packrat algorithm
Implementation
See also
CYK algorithm
Context-free grammar
Parsing algorithms
Earley parser
References
External links
Packrat Parsing: Simple, Powerful, Lazy, Linear Time
Parsing Expression Grammars: A Recognition-Based Syntactic Foundation
Kata Kunci Pencarian:
- Daftar algoritme
- Packrat parser
- Parsing expression grammar
- Parsing
- CYK algorithm
- Memoization
- Top-down parsing
- Compiler-compiler
- Comparison of parser generators
- GLR parser
- Pack rat (disambiguation)