I'm in a project, I need to parse python file, get the doc string, the properties, and the class name by Nodejs. I know there is a ast module in python to parse python source file to a syntax tree, is there similar module in Nodejs so that I can parse python source file?
There are Python grammars available for ANTLR.
And ANTLR can generate JS parsers.
Seems like you should be able to parse Python in JS that way.
ANTLR grammars: https://github.com/antlr/grammars-v4
ANTLR JavaScript target: https://github.com/antlr/antlr4/blob/master/doc/javascript-target.md
More info: http://www.antlr.org/
ANTLR is a LL(*) parser generator, the successor to PCCTS. Parsing languages is a complicated subject and actually parsing Python may be an overkill for what you really need but since you ask for a specific solution to your problem then this is something that should get you started.
Related
I have been using an antlr v4 grammar to parse code for my UML diagrammer. I would like to just replace my current 3.6 .g4 file with a 3.10 one. My current grammar does not support the new Python match statement.
I found the following but it generates Java code. I need one that generates Python code.
Does anyone know if such a thing exists?
Does anyone knows a good Python library to parse MSDOS files and obtain metadata and start()'s bytecodes? Like an alternative version of pefile library but for MSDOS? I can't seems to find any via Google.
If there isn't, is there a good source to refer to on MSDOS's file format? This way, I can create my own parser instead. I know there are tools like IDA Pro and Reko decompiler but I need a MSDOS file parser to automate some stuff. Thank you in advanced!
Reko decompiler maintainer here. For what it's worth, you can use Reko's MS-DOS source code and translate it to Python. It's not a lot of code and MS-DOS executables aren't that complex to parse -- it's quite a simple format. The relevant files are:
https://github.com/uxmal/reko/blob/master/src/ImageLoaders/MzExe/ExeImageLoader.cs
https://github.com/uxmal/reko/blob/master/src/ImageLoaders/MzExe/MsdosImageLoader.cs
You could also try executing the Reko code directly from Python. The Reko binaries are available as a nuget package: https://www.nuget.org/packages/Reko.Decompiler.Runtime
Use the class Reko.ImageLoaders.MzExe.ExeImageLoader in the Reko.ImageLoaders.MzExe class. Integration could be done with http://pythonnet.github.io/
I try to follow the Antlr4 reference book, with the Python3 target, but I got stuck in the calculator example. On the Antlr4 docs it says
The Python implementation of AntLR is as close as possible to the Java one, so you shouldn't find it difficult to adapt the examples for Python
but I don't get it yet.
The java code visitor has a .visit method and in python I don't have this method. I think it's because in java the visit method had parameter overloads of the tokens. In python we have visitProg(), visitAssign(), visitId() etc. But now I can't write value = self.visit(ctx.expr()) because we don't know what visit to call?
Or am I missing an instruction somewhere?
Looks like sometime in the last 3+ years this was fixed. I generated a parser from a grammar and targeted Python 3, using:
antlr4 -Dlanguage=Python3 -no-listener -visitor mygrammar.g4
It generates a visitor class that subclasses ParseTreeVisitor, which is a class in the antlr4-python3-runtime. Looking at the ParseTreeVisitor class, there is a visit method.
For those interested in working through the The Definitive ANTLR 4 Reference using Python, the ANTLR4 documentation points you towards this github repo:
https://github.com/jszheng/py3antlr4book
The Python2/3 targets do not yet have a visitor implemented. I tried to implement it myself, and a pull request is send to that antlr guy to see if I did it correctly. Follow the pull request here: https://github.com/antlr/antlr4-python3/pull/6
I'm using ANTLRWorks 1.5.2 for grammar creation and subsequent creation of the lexer and parser. I did that for Java target, but my preferred language is python. I'm quite puzzled by this: how can I specify my target language in ANTLRWorks 1.5.2 and get lexer and parser in python? I read somewhere that antlrworks in just for java target.
How can I install antlr3 and use python runtime?
I would be really appriciate if anyone can guid me.
thanks.
If you use the following options {...} block in your grammar:
options {
language=Python;
}
and then press CTRLSHIFT + G from within ANTLRWorks, the *.py lexer and parser files will be generated in the grammar's output/ directory.
However, debugging from within ANTLRWorks only works with the Java target.
As for a complete Python example, checkout this previous Q&A: ANTLR get and split lexer content
In my current work, I have written code generator using String Template without thinking about Parser ( I am instantiating Template files using direct Java Object). and code generator generator generates nice Java code.
Now, I have started to write Parser. B'coz of some nice editor features of xText, I am thinking to write parser in Xtext.
My question is "Is it possible to use code generator ( written using StringTemplate ) and Parse (written in Xtext) in same project?
Yes that's possible. Xtext offers a typed AST for the parsed files and you could easily pass them to your code generator (directly, iff they fulfil the same contract / interfaces, or indirectly by transforming them to the expected structure). Xtext does not impose any constraints on how you want to use the parsed information.