| 1 |
# Copyright 2022 Jeffrey Kegler |
| 2 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 3 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 4 |
# General Public License as published by the Free Software Foundation, |
| 5 |
# either version 3 of the License, or (at your option) any later version. |
| 6 |
# |
| 7 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 |
# Lesser General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU Lesser |
| 13 |
# General Public License along with Marpa::R2. If not, see |
| 14 |
# http://www.gnu.org/licenses/. |
| 15 |
|
| 16 |
=head1 NAME |
| 17 |
|
| 18 |
Marpa::R2::Tracing - Tracing your grammar |
| 19 |
|
| 20 |
=head1 Description |
| 21 |
|
| 22 |
This document is an overview of |
| 23 |
the techniques for tracing and |
| 24 |
debugging parses and grammars using Marpa's SLIF interface. |
| 25 |
|
| 26 |
=head1 Basic techniques |
| 27 |
|
| 28 |
=head2 Check the input location where parsing failed |
| 29 |
|
| 30 |
If parsing failed in the recognizer, |
| 31 |
look at the input location where it happened. |
| 32 |
Compare the input against the grammar. |
| 33 |
This step is fairly obvious, |
| 34 |
but I include it because |
| 35 |
even experts (actually, especially experts) |
| 36 |
will sometimes overlook the obvious |
| 37 |
in a rush to use more advanced techniques. |
| 38 |
|
| 39 |
=head2 Dump the parse value |
| 40 |
|
| 41 |
Sometimes, even when there is a parse error, you can still evaluate the parse |
| 42 |
using the L<SLIF recognizer's C<value()> |
| 43 |
method|Marpa::R2::Scanless::R/"value()">. |
| 44 |
If you are fortunate enough to have a parse value at the point of failure, |
| 45 |
it can be an excellent way |
| 46 |
to determine what the parser thinks it has seen so far. |
| 47 |
It is more likely that there will be a parse value if you are |
| 48 |
using incremental development, |
| 49 |
and the parse values will be especially helpful |
| 50 |
if your parse values are AST's. |
| 51 |
|
| 52 |
If you are trying to run diagnostics on a failed parse, |
| 53 |
it is useful to catch the exception using C<eval>: |
| 54 |
|
| 55 |
=for Marpa::R2::Display: |
| 56 |
name: SLIF debug example, part 2 |
| 57 |
partial: 1 |
| 58 |
normalize-whitespace: 1 |
| 59 |
|
| 60 |
my $eval_error = $EVAL_ERROR if not eval { $recce->read( \$test_input ); 1 }; |
| 61 |
$progress_report = $recce->show_progress( 0, -1 ); |
| 62 |
|
| 63 |
=for Marpa::R2::Display::End |
| 64 |
|
| 65 |
=head2 Trace terminals |
| 66 |
|
| 67 |
Set the L<C<trace_terminals> recognizer named |
| 68 |
argument|Marpa::R2::Scanless::R/"trace_terminals"> |
| 69 |
to 1. |
| 70 |
This tells you which tokens the recognizer is looking for and which ones it thinks it found. |
| 71 |
If the problem is in lexing, C<trace_terminals> tells you the whole story. |
| 72 |
Even if the problem is not in the lexing, |
| 73 |
tracing terminals can tell you a lot. |
| 74 |
|
| 75 |
=head2 Trace progress |
| 76 |
|
| 77 |
Tracing the recognizer's progress |
| 78 |
with |
| 79 |
C<show_progress> |
| 80 |
is the most powerful tool available |
| 81 |
in the basic toolkit. |
| 82 |
C<show_progress> |
| 83 |
should provide all the |
| 84 |
information necessary to |
| 85 |
debug an application's grammar. |
| 86 |
L<A separate document|Marpa::R2::Progress> |
| 87 |
explains how to interpret the progress reports. |
| 88 |
That document includes an |
| 89 |
example of the use of C<show_progress> |
| 90 |
to debug an error in a grammar. |
| 91 |
|
| 92 |
=head2 Double check rules and symbols |
| 93 |
|
| 94 |
It sometimes helps to look carefully at the output of |
| 95 |
L<C<show_symbols>|Marpa::R2::Scanless::G/"show_symbols()"> |
| 96 |
and |
| 97 |
L<C<show_rules>|Marpa::R2::Scanless::G/"show_rules()">. |
| 98 |
Check if anything there is |
| 99 |
not what you expected. |
| 100 |
For thorough checking, it can be helpful to use a |
| 101 |
verbosity level higher than 1. |
| 102 |
|
| 103 |
=head2 Other traces |
| 104 |
|
| 105 |
Setting the L<SLIF recognizer's C<trace_values> named argument|Marpa::R2::Scanless::R/"trace_values"> |
| 106 |
to a trace level of 1 |
| 107 |
traces the values of the parse tree nodes as they are pushed on, and |
| 108 |
popped off, the evaluation stack. |
| 109 |
|
| 110 |
=head2 Basic checklist |
| 111 |
|
| 112 |
A full investigation of a parse |
| 113 |
includes the following: |
| 114 |
|
| 115 |
=over 4 |
| 116 |
|
| 117 |
=item * Of course, the error message. |
| 118 |
|
| 119 |
=item * If the failed parse returns a value, a dump of that value. |
| 120 |
|
| 121 |
=item * Set the |
| 122 |
L<SLIF recognizer's C<trace_terminals> named |
| 123 |
argument|Marpa::R2::Scanless::R/"trace_terminals"> |
| 124 |
to level 1. |
| 125 |
|
| 126 |
=item * Run |
| 127 |
L<C<show_rules>|Marpa::R2::Scanless::G/"show_rules()"> |
| 128 |
on the SLIF grammar. |
| 129 |
|
| 130 |
=item * Run |
| 131 |
L<C<show_symbols>|Marpa::R2::Scanless::G/"show_symbols()"> |
| 132 |
on the SLIF grammar. |
| 133 |
|
| 134 |
=item * Run |
| 135 |
L<C<show_progress()>|Marpa::R2::Progress> |
| 136 |
on the SLIF recognizer. |
| 137 |
|
| 138 |
=back |
| 139 |
|
| 140 |
When considering how much tracing to turn on, |
| 141 |
remember that if the input text to the grammar is large, |
| 142 |
the outputs from |
| 143 |
C<trace_terminals>, |
| 144 |
C<show_progress>, |
| 145 |
and C<trace_values>, |
| 146 |
and the dump of the parse value, |
| 147 |
can be very lengthy. |
| 148 |
You want to work with short inputs when possible. |
| 149 |
|
| 150 |
=head1 Copyright and License |
| 151 |
|
| 152 |
=for Marpa::R2::Display |
| 153 |
ignore: 1 |
| 154 |
|
| 155 |
Copyright 2022 Jeffrey Kegler |
| 156 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 157 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 158 |
General Public License as published by the Free Software Foundation, |
| 159 |
either version 3 of the License, or (at your option) any later version. |
| 160 |
|
| 161 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 162 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 163 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 164 |
Lesser General Public License for more details. |
| 165 |
|
| 166 |
You should have received a copy of the GNU Lesser |
| 167 |
General Public License along with Marpa::R2. If not, see |
| 168 |
http://www.gnu.org/licenses/. |
| 169 |
|
| 170 |
=for Marpa::R2::Display::End |
| 171 |
|
| 172 |
=cut |
| 173 |
|