1using(3) ANTLR3C using(3)
2
3
4
6 using - Using the ANTLR3 C Target
7
8
10 Using the ANTLR target involves gaining knowledge of a number of
11 elements:
12
13 1. Writing ANTLR grammars (not covered in this manual);
14
15 2. How ANTLR works (not covered in this manual);
16
17 3. How to use the @sections with the C target
18
19 4. Interoperation with the runtime within rule actions;
20
21 5. Implementing custom versions of the standard library methods;
22
23 If you are as yet unfamiliar with how ANTLR works in general, then it
24 is suggested that you read the various wiki pages concerned with
25 getting started. However there are a few things that you should note:
26
27 • The lexer is independent of the parser. You cannot control the lexer
28 from within the parser;
29
30 • The tree parser is independent of the parser. You cannot control the
31 parser from within the tree parser(s);
32
33 • Each tree parser is independent of other tree parsers.
34
35 This means that your lexer runs first and consumes all the input stream
36 until you stop it programmatically, or it reaches the end of the input
37 stream. It produces a complete stream of tokens, which the parser then
38 consumes.
39
41 Within a grammar file there are a number of special sections you can
42 add that cause the code within them to be placed at strategic points in
43 the generated code such as before or after the #include statements in
44 the .c file, within the generated header file or within the constructor
45 for the recognizer.
46
47 Many of the @sections used within a Java targeted grammar have some
48 equivalent function within a C targeted grammar, but their use may well
49 be subtly different. There are also additional sections that have
50 meaning only within a grammar targeted for the C runtime.
51
52 Detailed documentation of these sections is given here: Using Sections
53 Within Grammar Files
54
56 Rule actions have a limited number of elements they can access by name,
57 independently of the target language generated. These are elements such
58 as $line, $pos, $text and so on. Where the $xxx returns a basic type
59 such as int, then you can use these in C as you would in the Java
60 target, but where a reference returns a string, you will get a pointer
61 to the C runtime string implementation pANTLR3_STRING. This will give
62 you access to things like token text but also provides some convenience
63 methods such as pANTLR3_STRING->substring() and
64 pANTLR3_STRING->toUTF8().
65
66 The generated code provides a number of C MACROs, which make it easier
67 to access runtime components. Always use these macros when available,
68 to protect your action code from changes to the underlying
69 implementation.
70
71 Detailed documentation of macros and rule action interoperation is
72 given here: Interoperation Within Rule Actions
73
75 Unless you wish to create your own tree structures using the built in
76 ANTLR AST rewriting notation, you will rarely need to override the
77 default implementation of runtime methods. The exception to this will
78 be the syntax err reporting method, which is essentially a stub
79 function that you will usually want to provide your own implementation
80 for. You should consider the built in function
81 displayRecognitionError() as an example of where to start as there can
82 be no really useful generic error message display.
83
84
85
86Version 3.3.1 Wed Jul 20 2022 using(3)