DEV Community

Discussion on: Explain ANTLR like I'm five

Collapse
 
cjbrooks12 profile image
Casey Brooks

Imagine you're trying to find your way through a large forest. You don't know how big the forest is, you don't have a map, and while you don't speak the same language as the locals you did gather enough information to be able to identify some of the landmarks you expect you'll find in the forest.

You've been given some rough drawings of some of the different kinds of trees, rocks, and bushes, and other things that live in the forest, and you've even managed to get some notes on the more complex signs that the locals use themselves for navigation. For example, you know that three black rocks with a white rock next to it points toward the river, while three black rocks with a stick in the ground near it point toward another pile of rocks. You've been studying these people for a while and have a pretty good understanding of how the locals build their navigational signs, as well as learning how to pay close attention to the natural signs around you, like animal tracks and the direction moss grows on the trees.

So armed with your notebook, you set out on your journey, and you write down all the signs you come across so that you can make a full map which will make it much easier to get back to the village or help your fellow researchers cross it themselves.


In this story, you, as the explorer, are the ANTLR parser. You don't have the full map of the forest yet, but you do have knowledge of al the important bits in it. In ANTLR terms, this is the grammar, and it consists of a set of rules which will help it build the "map" (the parser). The grammar consists of instructions for building the lexer and the parser.

The lexer is the bit of knowledge you have about single, easily identified structures within the program. In the example, this is the types of trees, types of rocks, etc. In a program, a lexer might define what is a valid string literal, or a valid integer, or the keywords used in the language.

The parser takes the tokens identified by the lexer and creates meaning behind certain groupings of those tokens. So you might have a single rock which means nothing, but a special formation of the rocks does have meaning. Likewise, a string literal might be assigned to a variable, or it might be passed as an argument to a method: the parser decides what should happen with the single string literal given its context in relation to other tokens.

Furthermore, you'll notice that the man in the story just makes the map, but he doesn't really do anything with it himself. He made the map simply so that it is easier for the other researches to find their way through the forest. In the same way, ANTLR doesn't create a fully-functional compiler for you, it just provides a structure for navigating your way through a program's textual representation so that you can focus less on the specifics of language construction and more on what the language is intended to do.

Collapse
 
vesusaso profile image
Sourab

Thanks Casey.