@@ -518,17 +518,17 @@ Pegen
518
518
=====
519
519
520
520
Pegen is the parser generator used in CPython to produce the final PEG parser used by the interpreter. It is the
521
- program that can be used to read the python grammar located in :file: `Grammar/Python .gram ` and produce the final C
521
+ program that can be used to read the python grammar located in :cpy- file: `Grammar/python .gram ` and produce the final C
522
522
parser. It contains the following pieces:
523
523
524
524
* A parser generator that can read a grammar file and produce a PEG parser written in Python or C that can parse
525
- said grammar. The generator is located at :file: `Tools/peg_generator/pegen `.
525
+ said grammar. The generator is located at :cpy- file: `Tools/peg_generator/pegen `.
526
526
* A PEG meta-grammar that automatically generates a Python parser that is used for the parser generator itself
527
527
(this means that there are no manually-written parsers). The meta-grammar is
528
- located at :file: `Tools/peg_generator/pegen/metagrammar.gram `.
528
+ located at :cpy- file: `Tools/peg_generator/pegen/metagrammar.gram `.
529
529
* A generated parser (using the parser generator) that can directly produce C and Python AST objects.
530
530
531
- The source code for Pegen lives at :file: `Tools/peg_generator/pegen ` but normally all typical commands to interact
531
+ The source code for Pegen lives at :cpy- file: `Tools/peg_generator/pegen ` but normally all typical commands to interact
532
532
with the parser generator are executed from the main makefile.
533
533
534
534
How to regenerate the parser
@@ -539,18 +539,18 @@ parser (the one used by the interpreter) just execute: ::
539
539
540
540
make regen-pegen
541
541
542
- using the :file: `Makefile ` in the main directory. If you are on Windows you can
542
+ using the :cpy- file: `! Makefile ` in the main directory. If you are on Windows you can
543
543
use the Visual Studio project files to regenerate the parser or to execute: ::
544
544
545
545
./PCbuild/build.bat --regen
546
546
547
- The generated parser file is located at :file: `Parser/parser.c `.
547
+ The generated parser file is located at :cpy- file: `Parser/parser.c `.
548
548
549
549
How to regenerate the meta-parser
550
550
---------------------------------
551
551
552
552
The meta-grammar (the grammar that describes the grammar for the grammar files
553
- themselves) is located at :file: `Tools/peg_generator/pegen/metagrammar.gram `.
553
+ themselves) is located at :cpy- file: `Tools/peg_generator/pegen/metagrammar.gram `.
554
554
Although it is very unlikely that you will ever need to modify it, if you make any modifications
555
555
to this file (in order to implement new Pegen features) you will need to regenerate
556
556
the meta-parser (the parser that parses the grammar files). To do so just execute: ::
@@ -570,7 +570,7 @@ Pegen has some special grammatical elements and rules:
570
570
571
571
* Strings with single quotes (') (e.g. ``'class' ``) denote KEYWORDS.
572
572
* Strings with double quotes (") (e.g. ``"match" ``) denote SOFT KEYWORDS.
573
- * Upper case names (e.g. ``NAME ``) denote tokens in the :file: `Grammar/Tokens ` file.
573
+ * Upper case names (e.g. ``NAME ``) denote tokens in the :cpy- file: `Grammar/Tokens ` file.
574
574
* Rule names starting with ``invalid_ `` are used for specialized syntax errors.
575
575
576
576
- These rules are NOT used in the first pass of the parser.
@@ -592,7 +592,7 @@ to handle things like indentation boundaries, some special keywords like ``ASYNC
592
592
interactive mode and much more. Some of these reasons are also there for historical purposes, and some
593
593
others are useful even today.
594
594
595
- The list of tokens (all uppercase names in the grammar) that you can use can be found in the :file: `Grammar/Tokens `
595
+ The list of tokens (all uppercase names in the grammar) that you can use can be found in the :cpy- file: `Grammar/Tokens `
596
596
file. If you change this file to add new tokens, make sure to regenerate the files by executing: ::
597
597
598
598
make regen-token
@@ -601,7 +601,7 @@ If you are on Windows you can use the Visual Studio project files to regenerate
601
601
602
602
./PCbuild/build.bat --regen
603
603
604
- How tokens are generated and the rules governing this is completely up to the tokenizer (:file: `Parser/tokenizer.c `)
604
+ How tokens are generated and the rules governing this is completely up to the tokenizer (:cpy- file: `Parser/tokenizer.c `)
605
605
and the parser just receives tokens from it.
606
606
607
607
Memoization
@@ -627,7 +627,7 @@ To know if a new rule needs memoization or not, benchmarking is required
627
627
(comparing execution times and memory usage of some considerably big files with
628
628
and without memoization). There is a very simple instrumentation API available
629
629
in the generated C parse code that allows to measure how much each rule uses
630
- memoization (check the :file: `Parser/pegen.c ` file for more information) but it
630
+ memoization (check the :cpy- file: `Parser/pegen.c ` file for more information) but it
631
631
needs to be manually activated.
632
632
633
633
Automatic variables
@@ -777,7 +777,7 @@ two phases:
777
777
(see the :ref: `how PEG parsers work section <how-peg-parsers-work >` for more information).
778
778
779
779
You can find a collection of macros to raise specialized syntax errors in the
780
- :file: `Parser/pegen.h ` header file. These macros allow also to report ranges for
780
+ :cpy- file: `Parser/pegen.h ` header file. These macros allow also to report ranges for
781
781
the custom errors that will be highlighted in the tracebacks that will be
782
782
displayed when the error is reported.
783
783
@@ -803,13 +803,13 @@ Generating AST objects
803
803
----------------------
804
804
805
805
The output of the C parser used by CPython that is generated by the
806
- :file: `Grammar/Python .gram ` grammar file is a Python AST object (using C
806
+ :cpy- file: `Grammar/python .gram ` grammar file is a Python AST object (using C
807
807
structures). This means that the actions in the grammar file generate AST objects
808
808
when they succeed. Constructing these objects can be quite cumbersome (see
809
809
the :ref: `AST compiler section <compiler-ast-trees >` for more information
810
810
on how these objects are constructed and how they are used by the compiler) so
811
811
special helper functions are used. These functions are declared in the
812
- :file: `Parser/pegen.h ` header file and defined in the :file: `Parser/action_helpers.c `
812
+ :cpy- file: `Parser/pegen.h ` header file and defined in the :cpy- file: `Parser/action_helpers.c `
813
813
file. These functions allow you to join AST sequences, get specific elements
814
814
from them or to do extra processing on the generated tree.
815
815
@@ -823,8 +823,8 @@ from them or to do extra processing on the generated tree.
823
823
824
824
As a general rule, if an action spawns multiple lines or requires something more
825
825
complicated than a single expression of C code, is normally better to create a
826
- custom helper in :file: `Parser/action_helpers.c ` and expose it in the
827
- :file: `Parser/pegen.h ` header file so it can be used from the grammar.
826
+ custom helper in :cpy- file: `Parser/action_helpers.c ` and expose it in the
827
+ :cpy- file: `Parser/pegen.h ` header file so it can be used from the grammar.
828
828
829
829
If the parsing succeeds, the parser **must ** return a **valid ** AST object.
830
830
@@ -833,14 +833,14 @@ Testing
833
833
834
834
There are three files that contain tests for the grammar and the parser:
835
835
836
- * `` Lib/test/test_grammar.py ``.
837
- * `` Lib/test/test_syntax.py ``.
838
- * `` Lib/test/test_exceptions.py ``.
836
+ * :cpy-file: ` Lib/test/test_grammar.py `
837
+ * :cpy-file: ` Lib/test/test_syntax.py `
838
+ * :cpy-file: ` Lib/test/test_exceptions.py `
839
839
840
840
Check the contents of these files to know which is the best place to place new tests depending
841
841
on the nature of the new feature you are adding.
842
842
843
- Tests for the parser generator itself can be found in the :file: `Lib/test/test_peg_generator ` directory.
843
+ Tests for the parser generator itself can be found in the :cpy- file: `Lib/test/test_peg_generator ` directory.
844
844
845
845
846
846
Debugging generated parsers
@@ -854,7 +854,7 @@ new rules to the grammar you cannot correctly compile and execute Python anymore
854
854
to debug when something goes wrong, especially when making experiments.
855
855
856
856
For this reason it is a good idea to experiment first by generating a Python parser. To do this, you can go to the
857
- :file: `Tools/peg_generator/ ` directory on the CPython repository and manually call the parser generator by executing:
857
+ :cpy- file: `Tools/peg_generator/ ` directory on the CPython repository and manually call the parser generator by executing:
858
858
859
859
.. code-block :: shell
860
860
@@ -874,7 +874,7 @@ Verbose mode
874
874
------------
875
875
876
876
When Python is compiled in debug mode (by adding ``--with-pydebug `` when running the configure step in Linux or by
877
- adding ``-d `` when calling the :file: `PCbuild/build.bat ` script in Windows), it is possible to activate a **very ** verbose
877
+ adding ``-d `` when calling the :cpy- file: `PCbuild/build.bat ` script in Windows), it is possible to activate a **very ** verbose
878
878
mode in the generated parser. This is very useful to debug the generated parser and to understand how it works, but it
879
879
can be a bit hard to understand at first.
880
880
0 commit comments