Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i access v8 parse tree how can it be done?

I like to take the v8 engine and to transform its code to other programming language based on this for example if i understand it right first step i need to get the parse tree

my question is : can i get it already from v8 or do i need to generate it from the js code . what is the easer way ?

like image 958
user63898 Avatar asked Dec 18 '11 09:12

user63898


People also ask

How do parse trees work?

Parse trees are an in-memory representation of the input with a structure that conforms to the grammar. The advantages of using parse trees instead of semantic actions: You can make multiple passes over the data without having to re-parse the input. You can perform transformations on the tree.

What is parse tree explain with example?

A parse tree is made up of nodes and branches. In the picture the parse tree is the entire structure, starting from S and ending in each of the leaf nodes (John, ball, the, hit). In a parse tree, each node is either a root node, a branch node, or a leaf node.

What is used to parse and execute JavaScript code?

Parser – It reads the JavaScript Code and parses it into a Data Structure called AST (Abstract Syntax Tree). AST is built by breaking down code into tokens and checks for the semantic and syntactic errors in the code. This tree is later used to generate machine code.


1 Answers

It looks hard to get the AST (Annotated Syntax Tree, the Parse tree) from V8 itself but there are plenty of other parsers for JavaScript that will do what you're looking for. I'd recommend having a look at Esprima (http://esprima.org/) which is a JavaScript parser written in JavaScript. This allows you to give some JavaScript source code and get back a JavaScript object version of the AST which you can transform into another language if you want (or modify then transform back into JavaScript or use for some other reason).

They've got some great online demos that should give you a feel for what it can do: http://esprima.org/demo/index.html

like image 145
Thomas Parslow Avatar answered Oct 06 '22 23:10

Thomas Parslow