Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
113 #include "../../api/api.h"
115 static char* s_cpDescription =
116 "Example demonstrating the use and usefulness of the AST.";
118 static char* s_cppCases[] = {
119 "Display application information.",
120 "Illustrate the rule call back function pitfall and solution with AST.",
122 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
124 static int iHelp(
void){
127 printf(
"description: %s\n", s_cpDescription);
128 printf(
" usage: ex-trace [arg]\n");
129 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
130 printf(
" execute case number n\n");
131 printf(
" arg = anything else, or nothing at all\n");
132 printf(
" print this help screen\n");
134 for(; i < s_iCaseCount; i++){
135 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
203 int iReturn = EXIT_SUCCESS;
204 static void* vpApi = NULL;
205 static void* vpAst = NULL;
206 static void* vpMem = NULL;
207 static void* vpParser = NULL;
208 char* cpGrammar =
"S = (1*A 1*B) / (1*A 1*C)\n"
212 char* cpInput =
"aaaacc";
226 memset(&sMyData, 0,
sizeof(sMyData));
245 printf(
"\nThe parsing problem: count the occurrences of A in the input string.\n");
246 printf(
" the parser, without built-in protection against failed branches, counts the As twice\n");
247 printf(
" generating and translating an AST solves the problem\n");
248 memset(&sConfig, 0,
sizeof(sConfig));
257 printf(
"\nThe Input String\n");
258 printf(
"input string: %s\n", cpInput);
261 printf(
"\nParser State without PPPT\n");
265 printf(
"\nMatched Rule Counts from Parser (notice the A rules get counted twice)\n");
271 memset(&sMyData, 0,
sizeof(sMyData));
276 printf(
"\nMatched Rule Counts from AST\n");
282 printf(
"\nThe AST in XML Format\n");
291 iReturn = EXIT_FAILURE;
314 int main(
int argc,
char **argv) {
317 iCase = atol(argv[1]);
319 if((iCase > 0) && (iCase <= s_iCaseCount)){
320 printf(
"%s\n", s_cppCases[iCase -1]);
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool try
True for the try block, false for the catch block.
void vAstSetRuleCallback(void *vpCtx, aint uiRuleIndex, ast_callback pfnCallback)
Define a callback function for a single rule on the AST.
const achar * acpInput
Pointer to the input string.
#define ID_AST_POST
indicates post-node-traversal AST callback state (up the tree)
void * vpApiCtor(exception *spEx)
Construct an API component context (object).
aint uiParserState
[read only] ID_ACTIVE if the parser is going down the tree. ID_MATCH or ID_NOMATCH if coming up the t...
aint uiParserRuleLookup(void *vpCtx, const char *cpRuleName)
Find the rule index corresponding to a rule name.
#define ID_AST_OK
normal AST callback function return
void * vpUserData
Pointer to user data, if any. Not examined or used by the parser in any way. Presented to the user's ...
Input data to the AST callback functions.
void vAstTranslate(void *vpCtx, void *vpUserData)
Do a depth-first traversal of the AST with user-defined callback functions to translate the AST recor...
void vApiDtor(void *vpCtx)
The API component destructor.
void vParserSetRuleCallback(void *vpCtx, aint uiRuleId, parser_callback pfnCallback)
Set a call back function for a specific rule.
abool bUtilAstToXml(void *vpAst, char *cpType, const char *cpFileName)
Convert the AST records to XML format.
uint_fast32_t aint
The APG parser's unsigned integer type.
aint uiInputLength
Number of input string alphabet characters.
Defines the input string and other configuration parameters for the parser,.
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
The parser's final state.
A structure to describe the type and location of a caught exception.
Defines a pointer to an achar array plus its length. That is, a phrase as is often used by APG.
void vMemFree(void *vpCtx, const void *vpData)
Free memory previously allocated with vpMemAlloc().
#define ID_MATCH
indicates a matched phrase parser state on return from parse tree below this node
uintmax_t luint
luint is used to cast integers suitable for the %"PRIuMAX" printf format.
void * vpApiOutputParser(void *vpCtx)
Generate a parser object directly from the specified SABNF grammar.
The data struct passed to each callback function.
void * vpMemCtor(exception *spException)
Construct a memory component.
void * vpUserData
[input/output] User-defined data passed to to the parser in parser_config.
void * vpAstCtor(void *vpParserCtx)
The AST object constructor.
aint uiLength
The number of characters in the array.
void vParserDtor(void *vpCtx)
Clears the parser component's context and frees all heap memory associated with this parser.
void vUtilApgInfo(void)
Display the current state of apg.h.
aint uiStartRule
Index of the start rule. Any rule in the SABNF grammar may be used as the start rule.
apg_phrase * spUtilStrToPhrase(void *vpMem, const char *cpStr)
Convert a null-terminated ASCII string to an apg_phrase.
void vApiString(void *vpCtx, const char *cpString, abool bStrict, abool bPppt)
Quicky way to generate a parser from a grammar string.
void vUtilPrintException(exception *spEx)
Prints exception information from an exception structure.
void vUtilCurrentWorkingDirectory(void)
Display the current working directory.
void vUtilPrintParserState(parser_state *spState)
Display the parser state in human-readable format to stdout.
const achar * acpPhrase
Pointer to an array of type achar APG alphabet characters.
void vParserParse(void *vpCtx, parser_config *spConfig, parser_state *spState)
Parse an input string of alphabet characters.
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.