using(3)
๐ using โ Using the ANTLR3 C Target ๐
okay bestie, so this is using(3), part of the ANTLR3C manual, and it's literally your whole guide to using the ANTLR3 C target. pull up a chair, grab a strawberry ๐, and let's get into it.
โจ NAME โจ
using โ Using the ANTLR3 C Target
๐ธ Introduction ๐ธ
so, getting cozy with the ANTLR target means you gotta know about a handful of things, and here's the whole list, no gatekeeping:
- Writing ANTLR grammars (not covered in this manual);
- How ANTLR works (not covered in this manual);
- How to use the
@sectionswith the C target - Interoperation with the runtime within rule actions;
- Implementing custom versions of the standard library methods;
if you're still new-new to how ANTLR works in general, the vibe is: go read the various wiki pages concerned with getting started. ๐ฆ but before you run off, there are a few things you should note, and they are important:
- The lexer is independent of the parser. You cannot control the lexer from within the parser;
- The tree parser is independent of the parser. You cannot control the parser from within the tree parser(s);
- Each tree parser is independent of other tree parsers.
this means that your lexer runs first and consumes all the input stream until you stop it programmatically, or it reaches the end of the input stream. it produces a complete stream of tokens, which the parser then consumes. like, the lexer eats first, then the parser gets its plate. ๐ฝ๏ธ no sharing, no cap.
๐ @sections in a C Targeted Grammar ๐
within a grammar file there are a number of special sections you can add that cause the code within them to be placed at strategic points in the generated code โ like before or after the #include statements in the .c file, within the generated header file, or within the constructor for the recognizer. it's giving "put my code exactly here" energy. ๐
many of the @sections used within a Java targeted grammar have some equivalent function within a C targeted grammar, but their use may well be subtly different. heads up: there are also additional sections that have meaning only within a grammar targeted for the C runtime. so don't just copy-paste your Java brain over here. ๐
detailed documentation of these sections is given here: Using Sections Within Grammar Files
๐ Interoperation Within Rule Actions ๐
rule actions have a limited number of elements they can access by name, independently of the target language generated. these are elements such as $line, $pos, $text and so on. โญ
where the $xxx returns a basic type such as int, then you can use these in C as you would in the Java target. but where a reference returns a string, you will get a pointer to the C runtime string implementation pANTLR3_STRING. this will give you access to things like token text but also provides some convenience methods such as pANTLR3_STRING->substring() and pANTLR3_STRING->toUTF8(). it's giving handy, honestly.
the generated code provides a number of C MACROs, which make it easier to access runtime components. always use these macros when available, to protect your action code from changes to the underlying implementation. fr fr, protect your future self. ๐ซถ
detailed documentation of macros and rule action interoperation is given here: Interoperation Within Rule Actions
๐ Implementing Customized Methods ๐
unless you wish to create your own tree structures using the built in ANTLR AST rewriting notation, you will rarely need to override the default implementation of runtime methods. so like, breathe. ๐ฎโ๐จ
the exception to this will be the syntax err reporting method, which is essentially a stub function that you will usually want to provide your own implementation for. and when you do, you should consider the built in function displayRecognitionError() as an example of where to start, as there can be no really useful generic error message display. like, the library can't read your mind about what a good error message is, bestie. ๐
and that's the whole page, you ate it ๐โจ
cute little sign-off: this is Version 3.3.1, dated Tue Jul 29 2025 00:00:00, from the ANTLR3C manual, page using(3). mwah, go slay some grammars. ๐๐ฆ๐
๐ฆ where this page comes from
- fedora 43: antlr3-C-devel 1:3.4-3.5.3.14.fc43 ยท BSD-3-Clause
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms