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
basics
parsing_basics.py
Go to the documentation of this file.
1
''' @file examples/basics/parsing_basics.py
2
@brief Simple construction of a grammar object and parser.
3
4
A simple demonstration of the basics.
5
Generate a grammar object from an ABNF grammar syntax,
6
then use that object to parse an input string that matches the grammar.
7
'''
8
import
sys
9
import
os
10
# add the current working directory to the path
11
# DO NOT MOVE THE FOLLOWING STATEMENT
12
# if using autopep8 formatter, for example, set argument '--ignore=E402'
13
sys.path.append(os.getcwd())
14
from
apg_py.lib
import
utilities
as
utils
15
from
apg_py.lib.parser
import
Parser
16
from
apg_py.api.api
import
Api
17
18
title =
'''A simple demonstration of parsing basics.
19
Generate a grammar object from an ABNF grammar syntax.
20
Use the grammar object to parse an input string that matches the grammar.
21
'''
22
print()
23
print(title)
24
25
abnf_syntax =
'S = "a" S / "y"\n'
26
abnf_syntax_strict =
'S = "a" S / "y"\r\n'
27
input_string =
'aaay'
28
29
# construct the grammar object
30
api =
Api
()
31
grammar = api.generate(abnf_syntax)
32
if(api.errors):
33
# report any errors
34
print(
'\n1) Grammar Errors'
)
35
print(api.display_errors())
36
else
:
37
# use the grammar object to parse an input string
38
# input string must be a tuple of positive integers
39
parser =
Parser
(grammar)
40
result = parser.parse(utils.string_to_tuple(input_string))
41
print(
'\n1) Parser Result - correct grammar'
)
42
print(result)
43
44
# fails because of incorrect line end with strict ABNF
45
grammar = api.generate(abnf_syntax, strict=
True
)
46
if(api.errors):
47
print(
'\n2) Grammar Errors - strict specified but line ends not CRLF'
)
48
print(api.display_errors())
49
else
:
50
parser =
Parser
(grammar)
51
result = parser.parse(utils.string_to_tuple(input_string))
52
print(
'\n2) Parser Result'
)
53
print(result)
54
55
# strict ABNF succeeds because grammar syntax is strictly ABNF
56
grammar = api.generate(abnf_syntax_strict, strict=
True
)
57
if(api.errors):
58
print(
'\n3) Grammar Errors'
)
59
print(api.display_errors())
60
else
:
61
parser =
Parser
(grammar)
62
result = parser.parse(utils.string_to_tuple(input_string))
63
print(
'\n3) Parser Result - strict specification succeeds'
)
64
print(result)
apg_py.api.api.Api
The API class.
Definition:
api.py:61
apg_py.lib.parser.Parser
The Parser class for parsing an APG grammar.
Definition:
parser.py:60
apg_py.api.api
Definition:
api.py:1
apg_py.lib.parser
Definition:
parser.py:1
apg_py.lib
Definition:
__init__.py:1
Generated by
1.9.1
Python APG, Version 1.0, is licensed under the
2-Clause BSD License
,
an
Open Source Initiative
Approved License.