Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
use.py
Go to the documentation of this file.
1 ''' @file examples/grammar_object/use.py
2 @brief Demonstrates how to use a saved a grammar object.
3 
4 For grammars that are used often,
5 saving the grammar object prevents the repeated and redundant
6 re-generation of the grammar object from the SABNF syntax over and over.
7 
8 This example must be preceeded by examples/grammar_object/save.py.
9 This example uses the saved object from that example.
10 '''
11 import sys
12 import os
13 # add the current working directory to the path
14 # DO NOT MOVE THE FOLLOWING STATEMENT
15 # if using autopep8 formatter, for example, set argument '--ignore=E402'
16 sys.path.append(os.getcwd())
17 from apg_py.lib.parser import Parser
18 import examples.grammar_object.abnf as grammar
19 
20 
22  title = '''Demonstrate how to use a saved grammar object.
23 Uses the grammar object saved by the companion example,
24 save_grammar_object.py.
25 '''
26  print()
27  print(title)
28  # display the original grammar
29  print('the ABNF syntax of the previously saved grammar object, ', end='')
30  print('examples/abnf.py')
31  print(grammar.to_string())
32 
33  # construct a parser and parse a string
34  parser = Parser(grammar)
35  result = parser.parse((128, 200, 255))
36  print(result)
The Parser class for parsing an APG grammar.
Definition: parser.py:60
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.