Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
83 #include "../../utilities/utilities.h"
84 #include "../../api/api.h"
88 static const char* cpMakeFileName(
char* cpBuffer,
const char* cpBase,
const char* cpDivider,
const char* cpName){
89 strcpy(cpBuffer, cpBase);
90 strcat(cpBuffer, cpDivider);
91 strcat(cpBuffer, cpName);
95 static char s_caBuf[PATH_MAX];
97 static char* s_cpDescription =
98 "Illustrate construction of and API object and demonstrate its features.";
100 static char* s_cppCases[] = {
101 "Display application information.",
102 "Input, concatenate and display multiple grammar files.",
103 "Input, validation fails with bad characters and no final EOL.",
104 "Illustrate a grammar with bad syntax.",
105 "Illustrate a grammar with bad semantics.",
106 "Illustrate generating a parser with and without PPPT.",
108 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
109 static void vPrintCase(
long int iCase){
110 if((iCase > 0) && (iCase <= s_iCaseCount)){
111 printf(
"%s\n", s_cppCases[iCase -1]);
113 printf(
"unknown case number %ld\n", iCase);
117 static int iHelp(
void){
120 printf(
"description: %s\n", s_cpDescription);
121 printf(
" usage: ex-api arg\n");
122 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
123 printf(
" execute case number n\n");
124 printf(
" arg = anthing else\n");
125 printf(
" print this help screen\n");
127 for(; i < s_iCaseCount; i++){
128 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
143 static int iInCat() {
144 int iReturn = EXIT_SUCCESS;
145 static void* vpApi = NULL;
147 "integer = 1*%d48-57\n"
158 cpApiInFile(vpApi, cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"float-top.abnf"));
160 cpApiInFile(vpApi, cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"float-bot.abnf"));
163 printf(
"\nThe Concatenated Grammar\n");
169 iReturn = EXIT_FAILURE;
175 static int iInBadChars() {
176 int iReturn = EXIT_SUCCESS;
177 static void* vpApi = NULL;
179 "float = sign decimal \x80 exponent\n"
180 "sign = [\"+\" / \"-\"]\n"
181 "decimal = integer [dot fraction]\n"
182 " / dot \xFF fraction\n"
183 "integer = 1*%d48-57\n"
185 "fraction = *%d48-57\n"
186 "exponent = [\"e\" esign exp]\n"
187 "esign = [\"+\" / \"-\"]\n"
210 printf(
"\nThe Grammar Errors\n");
214 printf(
"\nThe Full Grammar\n");
216 iReturn = EXIT_FAILURE;
222 static int iInBadSyntax() {
223 int iReturn = EXIT_SUCCESS;
224 static void* vpApi = NULL;
226 "float = sign decimal exponent\n"
227 "sign = [\"+\" / \"-\"]\n"
228 "decimal = integer [dot fraction]\n"
230 "integer = 1*%d48-57\n"
232 "fraction = *%d48-57\n"
233 "exponent = [\"e\" esign exp]\n"
234 "esign = [\"+\" / \"-\"]\n"
258 printf(
"\nThe Grammar Errors\n");
262 printf(
"\nThe Full Grammar\n");
264 iReturn = EXIT_FAILURE;
270 static int iInBadSemantics() {
271 int iReturn = EXIT_SUCCESS;
272 static void* vpApi = NULL;
274 "float = sign decimal exponent\n"
275 "sign = [\"+\" / \"-\"]\n"
276 "decimal = integer [dot fraction]\n"
278 "integer = 1*%d57-48\n"
280 "fraction = *%d48-57\n"
281 "exponent = [\"e\" esign exp]\n"
282 "esign = [\"+\" / \"-\"]\n"
307 printf(
"\nThe Grammar Errors\n");
311 printf(
"\nThe Full Grammar\n");
313 iReturn = EXIT_FAILURE;
319 static int iInPppt() {
320 int iReturn = EXIT_SUCCESS;
321 static void* vpMem = NULL;
322 static void* vpApi = NULL;
323 static void* vpParser = NULL;
324 static void* vpPpptParser = NULL;
326 "\"array\": [1,2,3,4],"
327 "\"object\": {\"t\": true, \"f\": false, \"n\":null}"
346 cpApiInFile(vpApi, cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"json.abnf"));
353 printf(
"\nThe PPPT sizes\n");
355 printf(
"minimum alphabet character: %"PRIuMAX
"\n", sPpptSize.
luiAcharMin);
356 printf(
"maximum alphabet character: %"PRIuMAX
"\n", sPpptSize.
luiAcharMax);
357 printf(
" bytes per PPPT map: %"PRIuMAX
"\n", sPpptSize.
luiMapSize);
358 printf(
" number of maps in table: %"PRIuMAX
"\n", sPpptSize.
luiMaps);
359 printf(
" total table size in bytes: %"PRIuMAX
"\n", sPpptSize.
luiTableSize);
363 memset(&sConfig, 0,
sizeof(sConfig));
368 printf(
"\nState showing node hits without PPPT\n");
374 memset(&sConfig, 0,
sizeof(sConfig));
379 printf(
"\nState showing node hits with PPPT\n");
385 iReturn = EXIT_FAILURE;
401 int main(
int argc,
char **argv) {
404 iCase = atol(argv[1]);
415 return iInBadChars();
418 return iInBadSyntax();
421 return iInBadSemantics();
#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 vApiPpptSize(void *vpCtx, pppt_size *spSize)
Compute the size of the PPPT maps and the number of bytes for the entire table.
const achar * acpInput
Pointer to the input string.
void * vpApiCtor(exception *spEx)
Construct an API component context (object).
void vUtilPrintMsgs(void *vpMsgs)
Display the list of messages in a message object to stdout.
aint uiParserRuleLookup(void *vpCtx, const char *cpRuleName)
Find the rule index corresponding to a rule name.
void vApiInValidate(void *vpCtx, abool bStrict)
Scans the input SABNF grammar for invalid characters and line ends.
luint luiMapSize
The size, in bytes, of a single PPPT table entry (map).
void vApiOpcodes(void *vpCtx)
Parse the SABNF grammar and translate its AST into opcodes for all the rules.
void vApiDtor(void *vpCtx)
The API component destructor.
void * vpApiGetErrorLog(void *vpCtx)
Get the internal message log.
luint luiAcharMax
The maximum character size in the grammar alphabet.
aint uiInputLength
Number of input string alphabet characters.
Defines the input string and other configuration parameters for the parser,.
luint luiMaps
The number of maps needed.
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
void vApiSyntax(void *vpCtx, abool bStrict)
Parse the SABNF grammar to validate that the grammar structure is valid.
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.
luint luiTableSize
The memory requirement, in bytes, of the full table.
void * vpApiOutputParser(void *vpCtx)
Generate a parser object directly from the specified SABNF grammar.
abool bApiAttrs(void *vpCtx)
Computes the grammar's attributes.
Size information for the **P**artially-**P**redictive **P**arsing **T**ables (PPPT) data.
void * vpMemCtor(exception *spException)
Construct a memory component.
const char * cpApiInFile(void *vpCtx, const char *cpFileName)
Reads an SABNF grammar byte stream from a file.
luint luiAcharMin
The minimum character size in the grammar alphabet.
void vApiInToAscii(void *vpCtx, const char *cpFileName)
Display the input lines with line numbers and character offsets.
const char * cpApiInString(void *vpCtx, const char *cpString)
Reads an SABNF grammar byte stream from a string.
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 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 vApiPppt(void *vpCtx, char **cppProtectedRules, aint uiProtectedRules)
Compute the Partially-Predictive Parsing Tables.
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.