Need help to create python parser for typescript/javascript target #4317
-
hi i have tried to create parser for typescript target using the python3ts grammar that exist in the grammar repo
i have changed the grammar file to the python2 but for javascript target this time,
|
Beta Was this translation helpful? Give feedback.
Replies: 15 comments 60 replies
-
@ericvergnaud hi, please i need help with this, its driving me crazy |
Beta Was this translation helpful? Give feedback.
-
You need help from the author of that grammar, it relies on antlr4ts which is a fork we don’t support.Alternately you need to rewrite Lexer::header and Lexer::members sections in the grammarEnvoyé de mon iPhoneLe 17 juin 2023 à 10:09, Elmahdi ***@***.***> a écrit :
@ericvergnaud hi, please i need help with this, its driving me crazy
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
@Elmahdi1962 This is an issue with the grammar, not Antlr per se. The grammar that you will want to use is python/python3, but it only works for CSharp, Java, and Python3. I'll make a port for TypeScript today in a PR to round out all the grammar for all 8 targets (not including Swift because I can't get it to install--never could). In fact, I created an issue on the Python grammars yesterday. before you wrote the question here. antlr/grammars-v4#3539. The state of the Python grammars in grammars-v4 is a mess. |
Beta Was this translation helpful? Give feedback.
-
The python3 grammar works for JavaScript. But it is slow. The only other viable Python3 grammar would be python/python, but there is no JavaScript port. Both grammars are old. |
Beta Was this translation helpful? Give feedback.
-
You're going to have to add output to the build script to see what it does. Here's the code for build.sh.
There is no "antlr" command-line call. It's called "antlr4", and it is a Python script to run the tool. This is right from the front page of antlr.org. It's installed by:
After installing antlr4-tools, type "antlr4" in a shell of your choice:
Explanation of the build.sh code
This line of code executes a python script in the file "transformGrammar.py" if it exists. It modifies the grammar to work with a particular target. NB: Antlr is NOT target independent code! It has target-specific code called "actions" in the grammar, between open curly brace
This code gets the version string from the package.json file. It is literally just "4.13.0".
This code executes the "antlr4" python script, which calls the Antlr4 tool to generate a recognizer. The target is JavaScript. For the "Parser.g4", it generates listener and visitor JavaScript classes. Why use the
This code reads the package.json file and downloads the required packages. How to run the programYou need to install Node. After installing Node, type |
Beta Was this translation helpful? Give feedback.
-
do i need to keep all those files and folders for the parser to work? or that neede just for the generator and the generated files can be used in my actuall app? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@Elmahdi1962 Have you read the book ? |
Beta Was this translation helpful? Give feedback.
-
its not free !! i have to pay to learn the package. nice one |
Beta Was this translation helpful? Give feedback.
-
i need help on creating a visitor and the docs didnt help its just a visitor with the visitchildren method which get called by it self if a i used tree.accept(the visitor) |
Beta Was this translation helpful? Give feedback.
-
@kaby76 @KvanTTT
and this in lexer grammar
and i regenerated all the parser file and i overridden the visitAssignment_stmt method
|
Beta Was this translation helpful? Give feedback.
-
An Antlr visitor visits everything in a parse tree, whether you implemented a method that acts on the node type or not. I really recommend that you don't change the grammar. You can either start from another rule (you will need to augment the rule with EOF), or write a visitor that does static semantic analysis, which would just return "false" for a parse tree that contains parse tree nodes that you don't want. To get the tokens associated with a node, I suggest you write a method that returns the frontier of a given parse tree. It would look just like the getText() function, which is linked above. It would create and return a list of tokens that you concatenate together from the children of the node. |
Beta Was this translation helpful? Give feedback.
-
also how can i generate parser in typescript target , i want typescript just for the autocomplete instead of diging in the source code to know the properties available to me |
Beta Was this translation helpful? Give feedback.
-
You will need the changes I have been making to the python grammars. See antlr/grammars-v4#3540. They code is at https://github.com/kaby76/grammars-v4/tree/733c2a79cad925e6eb2ad3ef70c9dfe673d13389/python/python3 |
Beta Was this translation helpful? Give feedback.
-
since i am using just the default grammar i am having a hard time extracting just variable assignments from the Expr_stmt rule, even chatgpt cant do it |
Beta Was this translation helpful? Give feedback.
@Elmahdi1962 This is an issue with the grammar, not Antlr per se. The grammar that you will want to use is python/python3, but it only works for CSharp, Java, and Python3. I'll make a port for TypeScript today in a PR to round out all the grammar for all 8 targets (not including Swift because I can't get it to install--never could).
In fact, I created an issue on the Python grammars yesterday. before you wrote the question here. antlr/grammars-v4#3539. The state of the Python grammars in grammars-v4 is a mess.