Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
main.cpp
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"
42 int main(){
43  apg_uint uiTrace = APG_TRUE;
44  apg_uint uiStats = APG_TRUE;
45  apg_uint uiState = APG_TRUE;
46  apg_uint uiAstNodesOnly = APG_TRUE;
47  apg_uint uiAstCallbacks= APG_TRUE;
48  apg_uint uiSuccess;
49  const char* cpInput = "Hello World\n";
50  apg_uint uiInputLen = strlen(cpInput);
51  apg_achar acaInput[uiInputLen + 1];
52  vCharToAChar(acaInput, cpInput, uiInputLen);
53  CppDemo oCpp(vTerminalAlert);
54  APG_CALLBACK saSyntaxRuleCallbacks[oCpp.RULEID_COUNT];
55  APG_CALLBACK saSyntaxUdtCallbacks[oCpp.UDTID_COUNT];
56  APG_CALLBACK saAstRuleCallbacks[oCpp.RULEID_COUNT];
57  APG_CALLBACK saAstUdtCallbacks[oCpp.UDTID_COUNT];
58  apg_uint saAstRuleNodes[oCpp.RULEID_COUNT];
59  apg_uint saAstUdtNodes[oCpp.UDTID_COUNT];
60 
61  printf("CPPDEMO: BEGIN\n");
62 
63  printf("CppDemo Grammar Information:\n");
64  vDisplayGrammarInfo(NULL, oCpp.m_vpParserInit);
65  printf("\n");
66  printf("\n");
67  oCpp.vDefineSyntaxCallbacks(saSyntaxRuleCallbacks, saSyntaxUdtCallbacks);
68  oCpp.uiSyntaxCallbacks(saSyntaxRuleCallbacks, saSyntaxUdtCallbacks);
69 
70  if(uiAstNodesOnly){
71  oCpp.vDefineAstNodes(saAstRuleNodes, saAstUdtNodes);
72  oCpp.vAstInitNodes(saAstRuleNodes, saAstUdtNodes);
73  }
74  if(uiAstCallbacks){
75  vCharToAChar(acaInput, cpInput, uiInputLen);
76  oCpp.vDefineAstCallbacks(saAstRuleCallbacks, saAstUdtCallbacks);
77  oCpp.vAstInitCallbacks(saAstRuleCallbacks, saAstUdtCallbacks);
78  }
79 
80  if(uiTrace){
81  printf("PARSER: TRACE ENABLED\n");
82  oCpp.uiTraceEnable();
83 // oCpp.vTraceOp(APG_TRACE_TBS);
84 // oCpp.vTraceRule(oCpp.RULEID_CRLF, APG_FALSE);
85 // oCpp.vTraceUdt(oCpp.UDTID_U_WORLD, APG_FALSE);
86 // oCpp.vTraceRange(3, 4);
87  }
88 
89  if(uiStats){
90  oCpp.uiStatsEnable();
91  }
92 
93  printf("PARSER: SYNTAX ANALYSIS\n");
94  uiSuccess = oCpp.uiSyntaxAnalysis(oCpp.RULEID_HELLOWORLD, acaInput, uiInputLen);
95  if(uiSuccess){printf("oCpp.uiSyntaxAnalysis: success\n");}
96  else{printf("oCpp.uiSyntaxAnalysis: failed\n");}
97 
98  if(uiState){
99  printf("\nPARSER: STATE\n");
100  FILE* spOut = stdout;
101  APG_PARSER_STATE sState;
102  oCpp.uiState(&sState);
103  vDisplayState(spOut, &sState);
104  }
105 
106  if(uiStats){
107 // printf("\nPARSER: STATISTICS\n");
108  FILE* spOut = stdout;
109  apg_uint uiTest;
110  apg_uint uiBufferSize = 10000;
111  char caBuffer[uiBufferSize];
112  APG_PARSER_STATS* spStats = (APG_PARSER_STATS*)&caBuffer[0];
113  oCpp.uiStatsGet(NULL, &uiTest);
114  if(uiTest <= uiBufferSize){
115  oCpp.uiStatsGet(spStats, &uiTest);
116  fprintf(spOut, "\nPARSER STATISTICS:\n");
117  vDisplayOperatorStats(spOut, spStats);
118  vDisplayRuleStats(spOut, spStats, "count");
119  } else{
120  fprintf(spOut, "\nNO PARSER STATISTICS: insufficient buffer size: needed: %u: have: %u\n",
121  (apg_uint)uiTest, (apg_uint)uiBufferSize);
122  }
123  }
124 
125  if(uiAstNodesOnly){
126  printf("\nAST: NODES - native record format\n");
127  void* vpParserCtx = oCpp.vpGetParserCtx();
128  vDisplayAstRecords(NULL, vpParserCtx);
129  printf("\nAST: NODES - XML format\n");
130  apg_uint uiTest = uiAstToXml(NULL, vpParserCtx);
131  CPPASSERT(uiTest, "uiAstToXml() failed");
132  }
133  if(uiAstCallbacks){
134  printf("\nAST: TRANSLATION\n");
135  oCpp.uiAstTranslate(NULL, NULL);
136  }
137 
138  printf("\nCPPDEMO: simple demo: end\n");
139  printf("\nCPPDEMO: END\n");
140  return EXIT_SUCCESS;
141 }
APG_PARSER_STATS
full set of statistics gathered during parsing, uiParserSyntaxAnalysis()
Definition: Apg.h:591
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
APG_CALLBACK
apg_uint(* APG_CALLBACK)(APG_CBDATA *spData)
Definition: Apg.h:515
vDisplayState
void vDisplayState(FILE *spOut, APG_PARSER_STATE *spState)
Definition: Utilities.c:391
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
apg_achar
unsigned char apg_achar
Definition: Apg.h:183
APG_PARSER_STATE::uiState
apg_uint uiState
Definition: Apg.h:563
uiAstToXml
apg_uint uiAstToXml(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:479
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
main
int main()
Definition: main.cpp:42
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.