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
substrings.py
Go to the documentation of this file.
1
''' @file examples/basics/substrings.py
2
@brief Demonstrate parsing substrings.
3
Often, expecially as used by the pattern-matching engine
4
@ref exp.py, one needs to parse only a sub-string of
5
the entire input string.
6
Here this is explicitly demonstrated.
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 =
'''Demonstrate the parsing of substrings of the full input string.
19
'''
20
print()
21
print(title)
22
23
abnf_syntax =
'S = "a" S / "y"\n'
24
# abnf_syntax_strict = 'S = "a" S / "y"\r\n'
25
input_string =
'***aaay***'
26
beg = 3
27
len = 4
28
print(
'The grammar:'
, end=
' '
)
29
print(abnf_syntax)
30
print(
'The full input string:'
, end=
' '
)
31
print(input_string)
32
print(
'The sub string:'
, end=
' '
)
33
print(input_string[beg:beg + len])
34
35
# construct the grammar object
36
api =
Api
()
37
grammar = api.generate(abnf_syntax)
38
if(api.errors):
39
# report any errors
40
print(
'\n1) Grammar Errors'
)
41
print(api.display_errors())
42
else
:
43
# use the grammar object to parse an input string
44
# input string must be a tuple of positive integers
45
parser =
Parser
(grammar)
46
result = parser.parse(
47
utils.string_to_tuple(input_string),
48
sub_begin=beg,
49
sub_length=len)
50
print(
'\n1) Parser Result'
)
51
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.