DEV Community

Cover image for Abstract Syntax Trees: They're Actually Used Everywhere -- But What Are They?

Abstract Syntax Trees: They're Actually Used Everywhere -- But What Are They?

aruna-x on November 17, 2021

Isn't it wonderful how VS Code grays out obsolete lines of code? Oops, my return statement is on line 3. Line 4 won't run... But I haven't called t...
Collapse
 
nickytonline profile image
Nick Taylor

Gym teacher from Glee saying amazing!

Great stuff! There's another cool tool for ASTs called AST Explorer if you wanna check it out. It supports a bunch of languages.

Looking forward to your next post! 😎

Collapse
 
aruna profile image
aruna-x

Ah man. Thank you Nick! Much appreciated 😄

Collapse
 
pulljosh profile image
Josh Pullen

Thank you for this article! It is very relevant to a project I’m working on right now. In particular, I’d love to hear more about injecting code into user code (for your big O project). I’m trying to do essentially the exact same thing, and this is all new territory for me.

Collapse
 
aruna profile image
aruna-x

Hey Josh, that’s great to hear! I’m happy to write a follow up on injecting code. Coming soon 😄 I’m curious what project you’re working on.

Collapse
 
pulljosh profile image
Josh Pullen • Edited

I am working on a JavaScript editor that enables you to debug your code by time traveling:

I hope it will help new developers understand the language better. (I've been heavily inspired by the work of Bret Victor.)

The main trick here is to inject function calls into the user's code which report back information about the state of the program. That's how the timeline of values is built.

Thread Thread
 
aruna profile image
aruna-x

Josh! Apologies for the delay. I actually had a family emergency. This project sounds amazing! I won’t have a chance to write that blog post quickly enough, but I’d be happy to hop on a quick call to chat about what I learned and how you can apply it to your project. Let me know :)

Thread Thread
 
pulljosh profile image
Josh Pullen

Sorry to hear about your family. :( Hope things are getting better for you.

I would love to chat if you have the time! You are very kind to offer; I'm sure there's a lot I could learn from you.

I've been making progress on my babel plugin to inject the diagnostic function calls, but I am definitely just hacking around as I build it. There are probably lots of things I could be doing in a better way.

If you're still interested in talking, you can shoot me an email or a DM on twitter. :)

Collapse
 
pierresassoulas profile image
Pierre Sassoulas • Edited

Cool article aruna-x ! For those who want to do "applied ast", come contribute to pylint, it's a static analyser for python included in vs-code. We have a lot of issues to choose from and we'll help you with our custom ast, astroid :) We're using the visitor pattern so you can do something on all the node of a certain type easily. For example in our checker's 'visit_while' function you'll have access to all the while nodes. It's less graph/algorithmically intensive than you would think. (github.com/PyCQA/pylint/issues)

Collapse
 
aruna profile image
aruna-x

Thank you Pierre! I look forward to looking at pylint's open issues and contributing 😄

Collapse
 
dominicbraam profile image
Dominic Braam

Very interesting. I remember having a project in my first year at University which was to create a Big O Calculator. I didn’t get very far and I submitted with it only catering for one particular scenario. After reading this post, I’m ready to tackle it again. 😬

Collapse
 
aruna profile image
aruna-x

That’s wonderful! I’d love to take a look once it’s done 😄

Collapse
 
danielhansson profile image
Daniel Hansson

Interesting to read, learned something new today!

Collapse
 
edgarbonet profile image
Edgar Bonet

If there is a syntax error (missing brace, or bracket...) wouldn't you be unable to build the AST in the first place?

Collapse
 
aruna profile image
aruna-x • Edited

Hi Edgar, I was curious about that too when I first started playing with esprima. Wonderfully, esprima (which comes pre-baked with the rules of writing code we expect - it's fun to crawl through the esprima code and see how they've done this) throws helpful errors indicating the line and column numbers of syntactic issues. As long as you catch and handle them well (for example, by indicating the issue to the user so they can fix it, as with VS Code), you should be good :)

Collapse
 
edgarbonet profile image
Edgar Bonet

Oh, I see. So it's the process of attempting to build the AST that uncovers syntax errors. It's not the AST itself, which cannot be built if there is such an error. Makes sense.

Thread Thread
 
aruna profile image
aruna-x

Ah, I see what you meant to say! It seems I played fast and loose with my phrasing in this post. Thanks for highlighting this! I’ll edit the post so it’s more accurate :)

Collapse
 
squidbe profile image
squidbe

Excellent write-up! Thanks for your time/effort. 👍