Can I find a single example for listeners, visitors or actions in C++?

767 views
Skip to first unread message

Joshua Scholar

unread,
Mar 27, 2017, 2:42:50 AM3/27/17
to antlr-discussion
I'm trying to write a DSL for a fairly complex language targeting C++.

I bought the book, but there isn't a single word about using C++ with Antlr in the book, and I can't find a single grammar for antlr that demonstrates doing basic things in C++.

I want to do the basics - make symbol tables, verify that variables are in scope and used as the right types, comprehend the tree and emit code from it.

I'd be happy if I could just translate the tree into my own AST tree structure so that I understand it, I already have fancy tree classes.

Something ANYTHING.  I need an example or two in lieu of an understandable explanation.

Please give me SOMETHING.

Mike Lischke

unread,
Mar 27, 2017, 3:04:14 AM3/27/17
to antlr-di...@googlegroups.com
Hi Joshua,

I bought the book, but there isn't a single word about using C++ with Antlr in the book, and I can't find a single grammar for antlr that demonstrates doing basic things in C++.

C++ target support is fairly new. No wonder it isn't mentioned in the book. Maybe in a future circulation this will change.


I want to do the basics - make symbol tables, verify that variables are in scope and used as the right types, comprehend the tree and emit code from it.

Some may consider that as fairly advanced stuff already. Basics are: create a grammar, generate parser files from that, create the code to load and parse input and walk the generated parse tree. In that light you can easily find examples for basic stuff, once you know how ANTLR is organized. In the ANTLR repository there is a runtime folder with subfolders for each supported target (https://github.com/antlr/antlr4/tree/master/runtime). Look in the individual folders to see what the target authors added. For instance in the Cpp you can find a demo for basic stuff. Additionally there is the doc folder (https://github.com/antlr/antlr4/tree/master/doc) which contains a markdown document for each  supported target language, with some details about the usage.

Symbol table creation and semantic checks are more advanced techniques and not so much related to ANTLR (as the underlying parser infrastructure doesn't play a big role here). Take a look at my antlr-graps project (https://github.com/mike-lischke/antlr-graps) which implements GRAmmar Parsing Services for ANTLR grammars and contains a simple symbol table implementation as well as semantic checks via a listener. All that is written in TypeScript, however.

In general use the available examples for any language. The individual targets in ANTLR usually try to stay close to the Java implementation, so it shouldn't be difficult to convert code examples from one target to the other.

Joshua Scholar

unread,
Mar 27, 2017, 3:25:14 AM3/27/17
to antlr-di...@googlegroups.com
I feel like you're lecturing me on what's basic. I don't need you to tell me that Antlr doesn't have symbol tables, I knew that.

I just need to extract information from the tree.  I'd be happy to walk it myself if someone would post something a human being can understand like, I don't know maybe:

1) this is how to get the bloody tree
2) this is how to identify and enumerate the children.  You know, like a identifier enum on the node?  How to get it?  Where the table of ids is?
3) this is how to get the source token from a lex node, with information like: the string, offset and the line

BASICS

And I'd rather not have to write a parser in Java in order to figure out how to make one in c++.  I'm not a java programmer.
Reply all
Reply to author
Forward
0 new messages