Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
Setup.c
Go to the documentation of this file.
1 /*******************************************************************************
2  APG Version 6.3
3  Copyright (C) 2005 - 2012 Lowell D. Thomas, all rights reserved
4 
5  author: Lowell D. Thomas
6  email: lowell@coasttocoastresearch.com
7  website: http://www.coasttocoastresearch.com
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see
21  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
22  or write to the Free Software Foundation, Inc.,
23  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 *******************************************************************************/
25 
26 #include "main.h"
27 #include "SetupGrammar.h"
30 // NOTE: error handling is done with the statement:
31 // if(error){vTerminalAlert(__LINE__, __FILE__);}
32 // prints line and file where error was detected and exits
41 void vDemoSetup(){
42  apg_uint uiBufferSize = 1028;
43  char caInputString[uiBufferSize];
44  apg_achar acaParserString[uiBufferSize];
45  apg_uint uiStrLen;
46  apg_uint uiTest;
47  apg_uint i = 0;
48  APG_PARSER_STATE sState;
49  apg_uint uiStatsBufferSize = 10000;
50  char caStatsBuffer[uiStatsBufferSize];
51  APG_PARSER_STATS* spStats = (APG_PARSER_STATS*)&caStatsBuffer[0];
52  apg_uint uiaRuleNodes[RULE_COUNT_SETUPGRAMMAR];
53  void* vpParser = NULL;
54  char* cpInFile = "Setup.input";
55  char* cpOutFile = "Setup.output";
56  FILE* spOut = NULL;
57 
58  // redirects output
59  printf("%s - output redirect file for Demonstration Setup\n", cpOutFile);
60  spOut = fopen(cpOutFile, "w");
61  if(!spOut){vTerminalAlert(__LINE__, __FILE__);}
62 
63  fprintf(spOut, "*** LICENSE NOTICE ***\n");
64  vLicenseNotice(spOut);
65  fprintf(spOut, "\n*** DEMO SETUP ***\n");
66  fprintf(spOut, " Demonstrates the fundamentals of building a parser.\n");
67  fprintf(spOut, " - contruction of the parser\n");
68  fprintf(spOut, " - handling the input string\n");
69  fprintf(spOut, " - examination of the parser state\n");
70  fprintf(spOut, " - collecting and viewing parsing statistics\n");
71  fprintf(spOut, " - setup and display of the AST\n");
72 
73  // display the integer sizes
74  fprintf(spOut, "\n*** TYPE SIZES ***\n");
75  vDisplayTypeSizes(spOut);
76 
77  // display the grammar information
78  fprintf(spOut, "\n*** GRAMMAR INFORMATION ***\n");
79  vDisplayGrammarInfo(spOut, vpParserInit_SetupGrammar);
80 
81  // get the input string
82  memset((void*)&caInputString[0], 0, uiBufferSize);
83  uiStrLen = uiGetFileSize(cpInFile);
84  if(!uiStrLen){vTerminalAlert(__LINE__, __FILE__);}
85  if(uiStrLen > uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
86  uiTest = uiGetFile(cpInFile, (void*)&caInputString[0]);
87  if(uiTest != uiStrLen){vTerminalAlert(__LINE__, __FILE__);}
88 
89  // display the input string
90  fprintf(spOut, "\n*** INPUT STRING WITH LINE NUMBERS AND CHARACTER OFFSETS ***\n");
91  vDisplayAscii(spOut, &caInputString[0], uiStrLen);
92 
93  // convert the input string to alphabet characters
94  vCharToAChar(&acaParserString[0], &caInputString[0], uiStrLen);
95 
96  // construct the parser
97  vpParser = vpParserCtor(vpParserInit_SetupGrammar, vTerminalAlert);
98  if(!vpParser){vTerminalAlert(__LINE__, __FILE__);}
99 
100  // parse the string
101  uiTest = uiParserSyntaxAnalysis(vpParser, RULE_SETUPGRAMMAR_FILE, &acaParserString[0], uiStrLen, NULL);
102  if(!uiTest){vTerminalAlert(__LINE__, __FILE__);}
103  uiParserState(vpParser, &sState);
104 
105  // examine the parser state
106  fprintf(spOut, "\n*** PARSER STATE ***\n");
107  vDisplayParserState(spOut, vpParser);
108 
109  // set up for parsing statistics
110  uiParserStatsEnable(vpParser, APG_TRUE);
111 
112  // parse again
113  uiTest = uiParserSyntaxAnalysis(vpParser, RULE_SETUPGRAMMAR_FILE, &acaParserString[0], uiStrLen, NULL);
114  if(!uiTest){vTerminalAlert(__LINE__, __FILE__);}
115 
116  // look at the parsing statistics
117  uiParserStatsGet(vpParser, NULL, &uiTest);
118  if(uiTest <= uiStatsBufferSize){
119  uiParserStatsGet(vpParser, spStats, &uiTest);
120  fprintf(spOut, "\n*** PARSER STATISTICS ***\n");
121  vDisplayOperatorStats(spOut, spStats);
122  vDisplayRuleStats(spOut, spStats, NULL);
123  vDisplayRuleStats(spOut, spStats, "alpha");
124  vDisplayRuleStats(spOut, spStats, "count");
125  } else{
126  fprintf(spOut, "\n*** NO PARSER STATISTICS: insufficient buffer size: needed: %lu: have: %lu\n",
127  (printf_uint)uiTest, (printf_uint)uiStatsBufferSize);
128  }
129 
130  // set up for AST generation
131  for(; i < RULE_COUNT_SETUPGRAMMAR; i++){
132  uiaRuleNodes[i] = APG_FALSE; // defaults to no AST nodes
133  }
134  // define AST nodes without defining syntax callback functions
135  uiaRuleNodes[RULE_SETUPGRAMMAR_FILE] = APG_TRUE;
136  uiaRuleNodes[RULE_SETUPGRAMMAR_BLANKLINE] = APG_TRUE;
137  uiaRuleNodes[RULE_SETUPGRAMMAR_LINE] = APG_TRUE;
138  uiaRuleNodes[RULE_SETUPGRAMMAR_LINENAME] = APG_TRUE;
139  uiaRuleNodes[RULE_SETUPGRAMMAR_LINEERROR] = APG_TRUE;
140  uiaRuleNodes[RULE_SETUPGRAMMAR_ELEMENT] = APG_TRUE;
141  uiaRuleNodes[RULE_SETUPGRAMMAR_ALTERNATION] = APG_TRUE;
142  uiaRuleNodes[RULE_SETUPGRAMMAR_CONCATENATION] = APG_TRUE;
143  uiaRuleNodes[RULE_SETUPGRAMMAR_HEXNUM] = APG_TRUE;
144  uiaRuleNodes[RULE_SETUPGRAMMAR_DECNUM] = APG_TRUE;
145  uiaRuleNodes[RULE_SETUPGRAMMAR_CCOMMENT] = APG_TRUE;
146  uiaRuleNodes[RULE_SETUPGRAMMAR_CPPCOMMENT] = APG_TRUE;
147  uiaRuleNodes[RULE_SETUPGRAMMAR_SEMICOMMENT] = APG_TRUE;
148  uiaRuleNodes[RULE_SETUPGRAMMAR_SINGLEQUOTEDSTRING] = APG_TRUE;
149  uiaRuleNodes[RULE_SETUPGRAMMAR_DOUBLEQUOTEDSTRING] = APG_TRUE;
150  vParserAstInitNodes(vpParser, &uiaRuleNodes[0], NULL);
151 
152  // parse again
153  uiTest = uiParserSyntaxAnalysis(vpParser, RULE_SETUPGRAMMAR_FILE, &acaParserString[0], uiStrLen, NULL);
154  if(!uiTest){vTerminalAlert(__LINE__, __FILE__);}
155 
156  // look at the parsed phrases (AST nodes)
157  fprintf(spOut, "\n*** AST NODES AND PHRASES ***\n");
158  vDisplayAstRecords(spOut, vpParser);
159  fprintf(spOut, "\n*** AST TO XML ***\n");
160  uiTest = uiAstToXml(spOut, vpParser);
161  if(!uiTest){vTerminalAlert(__LINE__, __FILE__);}
162 
163  // reset the standard output
164  fprintf(spOut, "\n*** DEMO SETUP - test ended successfully ***\n");
165  fclose(spOut);
166 }
vDisplayAscii
void vDisplayAscii(FILE *spOut, const char *cpSrc, apg_uint uiSrcLen)
Definition: Utilities.c:62
vDisplayParserState
void vDisplayParserState(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:373
APG_PARSER_STATS
full set of statistics gathered during parsing, uiParserSyntaxAnalysis()
Definition: Apg.h:591
uiParserStatsEnable
apg_uint uiParserStatsEnable(void *vpCtx, apg_uint uiEnable)
Definition: Parser.c:403
APG_PARSER_STATE
the state of the parser after parsing an input string.
Definition: Apg.h:561
apg_uint
unsigned int apg_uint
Definition: Apg.h:169
vDemoSetup
void vDemoSetup()
Definition: Setup.c:41
uiGetFileSize
apg_uint uiGetFileSize(const char *cpFileName)
Definition: Files.c:40
vDisplayGrammarInfo
void vDisplayGrammarInfo(FILE *spOut, void *vpHdr)
Definition: Utilities.c:409
vDisplayAstRecords
void vDisplayAstRecords(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:183
vDisplayOperatorStats
void vDisplayOperatorStats(FILE *spOut, APG_PARSER_STATS *spStats)
Definition: Utilities.c:296
APG_TRUE
#define APG_TRUE
Definition: Apg.h:187
vDisplayRuleStats
void vDisplayRuleStats(FILE *spOut, APG_PARSER_STATS *spStats, const char *cpType)
Definition: Utilities.c:338
vParserAstInitNodes
void vParserAstInitNodes(void *vpCtx, apg_uint *uipRules, apg_uint *uipUdts)
Definition: Parser.c:283
uiParserState
apg_uint uiParserState(void *vpCtx, APG_PARSER_STATE *spState)
Definition: Parser.c:379
vLicenseNotice
void vLicenseNotice(FILE *spFile)
Definition: Utilities.c:442
apg_achar
unsigned char apg_achar
Definition: Apg.h:183
printf_uint
unsigned long int printf_uint
Definition: ApgUtilities.h:52
uiAstToXml
apg_uint uiAstToXml(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:479
vpParserCtor
void * vpParserCtor(void *vpParserInit, PFN_ALERT pfnAlertHandler)
Definition: Parser.c:58
APG_FALSE
#define APG_FALSE
Definition: Apg.h:190
uiGetFile
apg_uint uiGetFile(const char *cpFileName, void *vpBuffer)
Definition: Files.c:71
vCharToAChar
void vCharToAChar(apg_achar *acpAChars, const char *cpChars, apg_uint uiLen)
Definition: Tools.c:79
vTerminalAlert
void vTerminalAlert(unsigned int uiLine, const char *cpFile)
Definition: Utilities.c:557
vDisplayTypeSizes
void vDisplayTypeSizes(FILE *spOut)
Definition: Utilities.c:279
uiParserSyntaxAnalysis
apg_uint uiParserSyntaxAnalysis(void *vpCtx, apg_uint uiStartRule, const apg_achar *acpSrc, apg_uint uiSrcLen, void *vpData)
Definition: Parser.c:228
uiParserStatsGet
apg_uint uiParserStatsGet(void *vpCtx, APG_PARSER_STATS *spStats, apg_uint *uipBufferSize)
Definition: Parser.c:422
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/licenses.html or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.