fullExampleShadokList.tex 16 KB
Newer Older
Pierre Aubert's avatar
Pierre Aubert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

\part{Full example of Shadok list creation}

In this example, we want to generated a parser with the parser generator and show how to use the generated parser in a program.

\section{Parser generator configuration}

Let's use a configuration to generate a parser which parses a list of \textbf{Shadok}, in a file \textbf{shadok.pparser} :

\begin{pparser}
///Test class
Class Shadok{
	///Name of the Shadok
	String name[csVariableName];
	///Age of the Shadok
	UInt age[csUnsignedNumber];
}

///Parses a Shadok
Graph gShadok(Shadok s){
	"Shadok" "(" s.name OPTIONAL{"," s.age} ")"
}

///Main class
Class Source{
	///Vector of Shadok
	Vector(Shadok) vecShadok;
}

///Parses a complete source of Shadok
Graph gSource(Source s){
	REPEAT{s.vecShadok.pushBack(gShadok())}
}
\end{pparser}

\section{To launch the parser generator}

\begin{terminal}
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
39
phoenix_parser_generator -n TestParser -i shadok.pparser
Pierre Aubert's avatar
Pierre Aubert committed
40 41 42 43 44 45 46 47 48 49 50
\end{terminal}

Where :
\begin{itemize}
	\item \textbf{TestParser} : name of the generated parser class.
	\item \textbf{shadok.pparser} : name of the configuration file we have just created in the previous section.
\end{itemize}

The result is :

\begin{terminal}
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
51
phoenix_parser_generator -n TestParser -i shadok.pparser
Pierre Aubert's avatar
Pierre Aubert committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
graph_cpp_sourceToken : save file 'shadok.h'
graph_cpp_sourceToken : save file 'shadok.cpp'
graph_cpp_tabGraphTokenParser : save file 'TestParser.h'
graph_cpp_tabGraphTokenParser : save file 'TestParser.cpp'
repr_cpp_to_language : save file 'TestParser_to_language.h'
repr_cpp_to_language : save file 'TestParser_to_language.cpp'
graph_cpp_tabGraphTokenParserFull : save file 'TestParserFull.h'
\end{terminal}

Where :
\begin{itemize}
	\item \textbf{shadok.h} and \textbf{shadok.cpp} : contain the internal representation of the generated parser.
	\item \textbf{TestParser.h} and \textbf{TestParser.cpp} : contain the generated parser class.
	\item \textbf{TestParser_to_language.h} and \textbf{TestParser_to_language.cpp} : contain all the functions to be used to convert back the parsed internal representation to the initial \textbf{Shadok} language.
\end{itemize}

Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
68
The \textbf{phoenix_parser_generator} creates also a \textbf{CMakeLists.txt} and a \textbf{FindTESTPARSER.cmake} to ensure the generated projet can be used
Pierre Aubert's avatar
Pierre Aubert committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
with \textbf{CMake}.

\begin{terminal}
ll
total 56K
drwxr-xr-x 2 4,0K ./
drwxr-xr-x 3 4,0K ../
-rw-r--r-- 1  477 CMakeLists.txt
-rw-r--r-- 1 1,3K FindTESTPARSER.cmake
-rw-r--r-- 1 5,1K shadok.cpp
-rw-r--r-- 1 1,4K shadok.h
-rw-r--r-- 1  528 shadok.pparser
-rw-r--r-- 1  11K TestParser.cpp
-rw-r--r-- 1  306 TestParserFull.h
-rw-r--r-- 1 2,5K TestParser.h
-rw-r--r-- 1 1,3K TestParser_to_language.cpp
-rw-r--r-- 1  470 TestParser_to_language.h
\end{terminal}

	\subsection{The internal representation}
	
The files \textbf{Shadok.h} and \textbf{Shadok.cpp} describe the internal representation of the generated parser.	\\

\begin{advise}
The files are named \textbf{shadok.*} because the initial file was named \textbf{shadok.pparser}. Each \textbf{*.pparser} file will gives two files in the internal representation.
\end{advise}

Pierre Aubert's avatar
Pierre Aubert committed
96
File \textbf{shadok.h} :
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
97
\cpp{../../build/TESTS/TEST_SHADOK_LIST/shadok.h}
Pierre Aubert's avatar
Pierre Aubert committed
98

Pierre Aubert's avatar
Pierre Aubert committed
99
File \textbf{shadok.cpp} :
Pierre Aubert's avatar
Pierre Aubert committed
100

Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
101
\cpp{../../build/TESTS/TEST_SHADOK_LIST/shadok.cpp}
Pierre Aubert's avatar
Pierre Aubert committed
102 103 104 105 106 107 108 109


	\subsection{The parser class}
	
The files \textbf{TestParser.h} and \textbf{TestParser.cpp} describe the class of the generated parser.


File \textbf{TestParser.h} :
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
110
\cpp{../../build/TESTS/TEST_SHADOK_LIST/PTestParser.h}
Pierre Aubert's avatar
Pierre Aubert committed
111 112 113

File \textbf{TestParser.cpp} :

Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
114
\cpp{../../build/TESTS/TEST_SHADOK_LIST/PTestParser.cpp}
Pierre Aubert's avatar
Pierre Aubert committed
115 116 117 118 119 120

	\subsection{The functions in to_language file}
	
The files \textbf{PTestParser_to_language.h} and \textbf{PTestParser_to_language.cpp} can store the internal representation of the generated \textbf{TestParser} class in the original language (here \textbf{shadok}).

File \textbf{PTestParser_to_language.h} :
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
121
\cpp{../../build/TESTS/TEST_SHADOK_LIST/PTestParser_to_language.h}
Pierre Aubert's avatar
Pierre Aubert committed
122 123 124

File \textbf{PTestParser_to_language.cpp} :

Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
125
\cpp{../../build/TESTS/TEST_SHADOK_LIST/PTestParser_to_language.cpp}
Pierre Aubert's avatar
Pierre Aubert committed
126 127 128 129 130 131 132 133


	\subsection{The functions in TestParserFull file}
	
The file \textbf{TestParserFull.h} is the file which includes all the others generated files.
It can be used to simplify the file inclusion in you onw sources.

File \textbf{TestParserFull.h} :
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
134
\cpp{../../build/TESTS/TEST_SHADOK_LIST/PTestParserFull.h}
Pierre Aubert's avatar
Pierre Aubert committed
135 136 137 138 139 140

	\subsection{The CMakeLists.txt file}

The file \textbf{CMakeLists.txt} is used by \textbf{CMake} to compile the generated project.

File \textbf{CMakeLists.txt} :
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
141
\cmake{../../build/TESTS/TEST_SHADOK_LIST/CMakeLists.txt}
Pierre Aubert's avatar
Pierre Aubert committed
142 143

\begin{advise}
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
144
The project uses \textbf{ParserGenerator} to handle basic statements.
Pierre Aubert's avatar
Pierre Aubert committed
145 146 147 148 149 150 151 152
\end{advise}

	\subsection{The FindTESTPARSER.cmake file}

The file \textbf{FindTESTPARSER.cmake} is used by \textbf{CMake} to find the install of the generated project.
It will be necessary to use the generaed library to parse your own language files.

File \textbf{FindTESTPARSER.cmake} :
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
153
\cmake{../../build/TESTS/TEST_SHADOK_LIST/FindPTESTPARSER.cmake}
Pierre Aubert's avatar
Pierre Aubert committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255



\section{Install and compile the generated library}

Let's start in the current directory :
\begin{terminal}
ll
total 56K
drwxr-xr-x 2 4,0K ./
drwxr-xr-x 3 4,0K ../
-rw-r--r-- 1  477 CMakeLists.txt
-rw-r--r-- 1 1,3K FindTESTPARSER.cmake
-rw-r--r-- 1 5,1K shadok.cpp
-rw-r--r-- 1 1,4K shadok.h
-rw-r--r-- 1  528 shadok.pparser
-rw-r--r-- 1  11K TestParser.cpp
-rw-r--r-- 1  306 TestParserFull.h
-rw-r--r-- 1 2,5K TestParser.h
-rw-r--r-- 1 1,3K TestParser_to_language.cpp
-rw-r--r-- 1  470 TestParser_to_language.h
\end{terminal}

In the \textbf{CMake} convention the files are built in a \textbf{build} directory, separated from the sources to avoid classical remove issue or stuff like that.	\\

Let's create this \textbf{build} directory :

\begin{terminal}
mkdir build
\end{terminal}

Now we check it :

\begin{terminal}
ll
total 64K
drwxr-xr-x 2 4,0K ./
drwxr-xr-x 3 4,0K ../
drwxr-xr-x 2 4,0K build/
-rw-r--r-- 1  477 CMakeLists.txt
-rw-r--r-- 1 1,3K FindTESTPARSER.cmake
-rw-r--r-- 1 5,1K shadok.cpp
-rw-r--r-- 1 1,4K shadok.h
-rw-r--r-- 1  528 shadok.pparser
-rw-r--r-- 1  11K TestParser.cpp
-rw-r--r-- 1  306 TestParserFull.h
-rw-r--r-- 1 2,5K TestParser.h
-rw-r--r-- 1 1,3K TestParser_to_language.cpp
-rw-r--r-- 1  470 TestParser_to_language.h
\end{terminal}

Let's move in the \textbf{build} directory :

\begin{terminal}
cd build
\end{terminal}

And call \textbf{CMake}

\begin{terminal}
cmake ..
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Phoenix_CORE headers : ~/usr/include/Phoenix/PLIB_CORE
-- Found lib Phoenix_CORE : ~/usr/lib
-- Configuring done
-- Generating done
-- Build files have been written to: ~/projects/Phoenix/TEST/PParserGenerator2/build
\end{terminal}

\begin{attention}
Do not forget the \textbf{..} after the \textbf{cmake} call.
\end{attention}

\begin{conseil}
The C or CXX compilers may be different depending on your system.
\end{conseil}

\begin{termmake}
make
Scanning dependencies of target testparser
[ 25%] Building CXX object CMakeFiles/testparser.dir/TestParser.cpp.o
[ 50%] Building CXX object CMakeFiles/testparser.dir/TestParser_to_language.cpp.o
[ 75%] Building CXX object CMakeFiles/testparser.dir/shadok.cpp.o
[100%] Linking CXX shared library libtestparser.so
[100%] Built target testparser
\end{termmake}

Now we can install the project :

Pierre Aubert's avatar
Pierre Aubert committed
256
\begin{termmake}
Pierre Aubert's avatar
Pierre Aubert committed
257 258 259 260 261 262 263 264 265
make install
[100%] Built target testparser
Install the project...
-- Install configuration: ""
-- Installing: ~/usr/lib/libtestparser.so
-- Installing: ~/usr/include/TESTPARSER/TestParser.h
-- Installing: ~/usr/include/TESTPARSER/TestParser_to_language.h
-- Installing: ~/usr/include/TESTPARSER/shadok.h
-- Installing: ~/usr/share/cmake/FindTESTPARSER.cmake
Pierre Aubert's avatar
Pierre Aubert committed
266
\end{termmake}
Pierre Aubert's avatar
Pierre Aubert committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

Now the generated project is installed in your home \textbf{~}.




\section{Use the generated library}

To use the generated library we need to create a \textbf{CMake} project.\\

This project will be composed of one \textbf{main.cpp} to use the generated parser and one \textbf{CMakeLists.txt} to compile it.

	\subsection{The main.cpp file}

First of all, we need to include a header to parse the program arguments :

\begin{cpp}
// For the option parsing
#include "PSystem/poptionparser.h"
\end{cpp}

Then our generated parser :

\begin{cpp}
// To get the generated parser
#include <TestParserFull.h>
\end{cpp}

\begin{advise}
Remember, all the generated headers are included in this one.
\end{advise}

It is always better to have a program with a help function called on \textbf{--help} or \textbf{-h} :

\begin{cpp}
using namespace std;

///Print the help message
void helpFunction(){
	cout << "Options :" << endl;
	cout << "\tuse_parser --input=fileName.shadok" << endl;
	cout << "\tuse_parser -i fileName.shadok" << endl;
	cout << "Parameters :" << endl;
	cout << "\t-i FILENAME --input=FILENAME : input shadok file name" << endl;
}
\end{cpp}

The following function parses the input file name and print the parsed internal representation in the terminal :

\begin{cpp}
int printShadokFile(const std::string & fileName){
	//We create the parser
	TestParser parser;
	if(!parser.load(fileName)){
		cerr << "printShadokFile : can't load file '"<<fileName<<"'" << endl;
		return -1;
	}
	//We get the parsed internal representation
	Source & source = parser.getSourceS();
	std::vector<Shadok> & vecShadok = source.getVecShadok();
	for(std::vector<Shadok>::iterator it(vecShadok.begin()); it != vecShadok.end(); ++it){
		cout << "Shadok(name = " << it->getName() << ", age = " << it->getAge() << ")" << endl;
	}
	
	return 0;
}
\end{cpp}

\begin{advise}
It is saved a lot of memory to use references (\textbf{&}) instead of copying all the internal representation.
\end{advise}

The \textbf{main} function :

\begin{cpp}
int main(int argc, char** argv){
	if(pisOptionExist("help", "h", argc, argv)){
		helpFunction();
		return 0;
	}
	std::string fileName("");
	pparserOption(fileName, "input", "i", argc, argv);
	
	return printShadokFile(fileName);
}
\end{cpp}

\begin{advise}
You can call \textbf{use_parser --help} or \textbf{use_parser -h} thank to the \textbf{helpFunction} we wrote.
\end{advise}


The full \textbf{main.cpp} file :
Pierre Aubert's avatar
Pierre Aubert committed
360
\cpp{../../TESTS/TEST_SHADOK_LIST/main_test.cpp}
Pierre Aubert's avatar
Pierre Aubert committed
361 362 363 364 365 366 367 368




	\subsection{The CMakeLists.txt file}

The \textbf{CMakeLists.txt} file :

Pierre Aubert's avatar
Pierre Aubert committed
369
\cmake{../../TESTS/TEST_SHADOK_LIST/CMakeLists.txt}
Pierre Aubert's avatar
Pierre Aubert committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506


	\subsection{Compile the project}
	
As before, we create a \textbf{build} directory :

\begin{terminal}
mkdir build
cd build
\end{terminal}

We call \textbf{CMake} :

\begin{terminal}
cmake ..
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Phoenix_CORE headers : ~/usr/include/Phoenix/PLIB_CORE
-- Found lib Phoenix_CORE : ~/usr/lib
-- Found TESTPARSER headers : ~/usr/include/TESTPARSER
-- Found lib TESTPARSER : ~/usr/lib
-- Configuring done
-- Generating done
-- Build files have been written to: build
\end{terminal}

\begin{conseil}
The C or CXX compilers may be different depending on your system.
\end{conseil}

And we call \textbf{Make} :

\begin{termmake}
make
Scanning dependencies of target use_parser
[ 50%] Building CXX object CMakeFiles/use_parser.dir/main.cpp.o
[100%] Linking CXX executable use_parser
[100%] Built target use_parser
\end{termmake}

	\subsection{Use the parser}

Let's cfeate a simple file \textbf{file.shadok}
\begin{text}
Shadok(Shadoko)

Shadok(Devin)


Shadok(Marin, 9)
\end{text}

We get :
\begin{terminal}
./use_parser -i ../file.shadok
Shadok(name = Shadoko, age = 0)
Shadok(name = Devin, age = 0)
Shadok(name = Marin, age = 9)
\end{terminal}


\section{Do the same example but in a simpler way}

The previous example is efficient if no iteration is needed between the parser generation and its use.
In the case you need to iterate or add new functionalities durring the development it is more efficient to recreate the parser on the fly.\\

That is why the package \textbf{Phoenix_PARSER_GENERATOR} provides a \textbf{CMake} macro to create parser on the fly with the function :

\begin{cmake}
useParserGenerator2(createLibraryTarget parserName programOption listParserFile)
\end{cmake}

Where :
\begin{itemize}
	\item \textbf{createLibraryTarget} : name of the library to be generated.
	\item \textbf{parserName} : name of the parser class to be generated.
	\item \textbf{programOption} : extra program option if needed.
	\item \textbf{listParserFile} : list of all the *.pparser file to be used to generate the parser.
\end{itemize}

	\subsection{The parser configuration file}
	
Let's keep the same \textbf{shadok.pparser} file as the example begining :
\pparser{../../../doc/example/shadokList/useCMakeMacroParserGenerator/shadok.pparser}
	
	\subsection{The main.cpp file}

Let's keep the same \textbf{main.cpp} file as the example begining :
\cpp{../../../doc/example/shadokList/useCMakeMacroParserGenerator/main.cpp}

	\subsection{The CMakeLists.txt file}

The full \textbf{CMakeLists.txt} file :
\cmake{../../../doc/example/shadokList/useCMakeMacroParserGenerator/CMakeLists.txt}


	\subsection{Compile the project}
	
As before, we create a \textbf{build} directory :

\begin{terminal}
mkdir build
cd build
\end{terminal}

We call \textbf{CMake} :

\begin{terminal}
cmake ..
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Phoenix_CORE headers : ~/usr/include/Phoenix/PLIB_CORE
-- Found lib Phoenix_CORE : ~/usr/lib
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
507 508
-- phoenix_parser_generator found in ~/usr/bin
-- phoenix_parser_generator executable : ~/usr/bin/phoenix_parser_generator
Pierre Aubert's avatar
Pierre Aubert committed
509
-- SCRIPT_LAUNCH_TARGET_DIR = ~/usr/share/Phoenix/PARSER_GENERATOR
Pierre Aubert's avatar
Fix doc  
Pierre Aubert committed
510
-- useParserGenerator2 : phoenix_parser_generator executable : ~/usr/bin/phoenix_parser_generator
Pierre Aubert's avatar
Pierre Aubert committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
-- Configuring done
-- Generating done
-- Build files have been written to: build
\end{terminal}

\begin{conseil}
The C or CXX compilers may be different depending on your system.
\end{conseil}

And we call \textbf{Make} :

\begin{termmake}
make
[ 12%] Built target parser_lib_target
Scanning dependencies of target parser_lib
[ 25%] Building CXX object CMakeFiles/parser_lib.dir/shadok.cpp.o
[ 37%] Building CXX object CMakeFiles/parser_lib.dir/TestParser.cpp.o
[ 50%] Building CXX object CMakeFiles/parser_lib.dir/TestParser_to_language.cpp.o
[ 62%] Linking CXX shared library libparser_lib.so
[ 75%] Built target parser_lib
Scanning dependencies of target use_parser
[ 87%] Building CXX object CMakeFiles/use_parser.dir/main.cpp.o
[100%] Linking CXX executable use_parser
[100%] Built target use_parser
\end{termmake}

All the files are compile in one time.

	\subsection{Use the parser}

Let's cfeate a simple file \textbf{file.shadok}
\begin{text}
Shadok(Shadoko)

Shadok(Devin)


Shadok(Marin, 9)
\end{text}

We get :
\begin{terminal}
./use_parser -i ../file.shadok
Shadok(name = Shadoko, age = 0)
Shadok(name = Devin, age = 0)
Shadok(name = Marin, age = 9)
\end{terminal}