Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
… an
A
BNF
P
arser
G
enerator
Main Page
Related Pages
Packages
Packages
Package Functions
All
a
b
c
d
e
f
g
h
i
k
l
m
n
p
r
s
t
u
v
x
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
p
r
s
t
u
v
x
Variables
a
b
c
d
e
f
g
h
i
k
l
m
n
p
r
s
t
u
v
Classes
Class List
Class Index
Class Members
All
_
a
b
c
d
e
f
g
h
i
l
m
n
o
p
r
s
t
u
w
Functions
_
a
c
d
e
f
g
h
i
o
p
r
s
t
u
w
Variables
a
b
c
e
f
g
h
i
l
m
n
o
p
r
s
t
u
Files
File List
•
All
Classes
Namespaces
Files
Functions
Variables
Pages
examples
grammar_object
save.py
Go to the documentation of this file.
1
''' @file examples/grammar_object/save.py
2
@brief Demonstrates how to save a grammar object for future use.
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
Follow this with examples/grammar_object/use.py for a demonstration
9
of how to use this saved grammar object.
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.api.api
import
Api
18
19
20
def
save_grammar
():
21
title =
'''Demonstrate how to save a grammar object to a file for later use.
22
The saved grammar object will be used in the companion example,
23
examples/grammar_object/main.py --use
24
'''
25
print()
26
print(title)
27
28
syntax =
'abnf = tls / tbs/ trg\n'
29
syntax +=
'tls = "A" "B"\n'
30
syntax +=
'tbs = %d67.68\n'
31
syntax +=
'trg = 1*%d128-255\n'
32
save_file =
'examples/grammar_object/abnf.py'
33
api =
Api
()
34
api.generate(syntax)
35
if(api.errors):
36
# report any errors
37
print(
'\n1) Grammar Errors'
)
38
print(api.display_errors())
39
else
:
40
# save the grammar object for use with example
41
# examples/use_grammar_object.py
42
api.write_grammar(save_file)
43
print(
'grammar saved to file '
+ save_file)
44
print(
'this file will be used by the example, examples/use_grammar_object.py'
)
apg_py.api.api.Api
The API class.
Definition:
api.py:61
apg_py.api.api
Definition:
api.py:1
examples.grammar_object.save.save_grammar
def save_grammar()
Definition:
save.py:20
Generated by
1.9.1
Python APG, Version 1.0, is licensed under the
2-Clause BSD License
,
an
Open Source Initiative
Approved License.