2022-05-22 22:18 GMT+02:00, Pierre Quentel <
pierre....@gmail.com>:
> After a Python-compliant tokenizer, the production of a Python-compliant
> AST and the generation of Javascript code from this AST, the latest
> development version includes the last piece of the puzzle : a
> Python-compliant parser which generates the AST from source code.
>
> - the Python script *make_full_grammar.py* generates */src/full_grammar.js*,
>
> a Javascript version of the standard Python grammar (Python.gram
> <
https://github.com/python/cpython/blob/main/Grammar/python.gram>)
> - source code of Python scripts is processed by class Parser in
> */src/python_parser.js*. This script implements a PEG parser as specified
> in PEP 617 <
https://peps.python.org/pep-0617/>. It is an adaptation in
> Javascript of the algorithm detailed in the academic paper mentioned in
> this PEP, Packrat Parsers Can Support Left Recursion
> <
http://web.cs.ucla.edu/~todd/research/pepm08.pdf>
> - the "grammar actions" (explained in PEP 617, adapted to Javascript)
> generate the AST ; they use code in scripts *action_helpers.js*,
> *string_parser.js* and *number_parser.js*
>
> All these scripts are grouped in a new bundle,
> */src/brython_with_parser.js*.
> If it is inserted in the page along with *brython.js*, this new parser
> replaces the custom parser in *py2js.js* that has been used since the first
>
> versions of Brython.
>
> With the new parser, the AST is guaranteed to be the same as with CPython,
> and the error messages are the same. This is a clear improvement to the
> current situation, where syntax errors can be ignored, or not reported
> exactly as in CPython.
Congrats, Pierre!!!!
This is a nice piece of work. Thanks for all the time and effort put into this.
>
> Unfortunately, there is a major drawback: speed... Applying the dozens of
> rules in the grammar, with backtracking and unlimited lookahead, takes a
> very long time, around 3 to 5 times more than the current parser. This is
> terribly disappointing, but unless there is a way to improve performance
> (and I tried all sorts of ways, unsuccessfully) I will have to stick to the
>
> current parser. I publish the Python-compliant version for those who might
> be interested by the algorithm.
>
> Another issue with this approach would have been that the scripts adapted
> from CPython code (eg *action_helpers.js* adapted from *action_helpers.c*)
> are a "hand-made" translation where C syntax is replaced by more or less
> equivalent Javascript syntax. Future changes to the C code would have
> required to track the changes and manually reproduce them in the JS code,
> which is boring and error-prone. It would have been worthwile if speed was
> not such an issue, though.
>
> Of course if some of you have the time and interest to dive into the
> parsing algorithm and find ways to improve performance it would be great !
>
I have a question. Are the new parser creating 'lighter' js? Can the
new parser be used to generate an AOT 'production' js version of the
python code?
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
brython+u...@googlegroups.com.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/brython/ad98e0d1-d177-4f19-b992-0f7363a7d983n%40googlegroups.com.
>