using(3)

๐Ÿ“ฆ version 3.4, in fedora 43

๐Ÿ’… 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:

  1. Writing ANTLR grammars (not covered in this manual);
  2. How ANTLR works (not covered in this manual);
  3. How to use the @sections with the C target
  4. Interoperation with the runtime within rule actions;
  5. 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