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
look_behind.py
Go to the documentation of this file.
1
''' @file examples/basics/look_behind.py
2
@brief Demonstration of using the look behind operators.
3
4
A demonstration of the positive (&&) and negative(!!)
5
look behind operators.
6
<br>*Note that UDT and back referencing operators are not allowed in
7
look behind mode.*
8
<br>Included is a demonstration of using anchors,
9
operators that match only the beginning and ending positions
10
of the input string.
11
'''
12
import
sys
13
import
os
14
# add the current working directory to the path
15
# DO NOT MOVE THE FOLLOWING STATEMENT
16
# if using autopep8 formatter, for example, set argument '--ignore=E402'
17
sys.path.append(os.getcwd())
18
from
apg_py.lib
import
utilities
as
utils
19
from
apg_py.lib.parser
import
Parser
20
from
apg_py.lib.trace
import
Trace
21
from
apg_py.api.api
import
Api
22
23
title =
'''A demonstration of the positive (&&) and negative(!!)
24
look behind operators. Included is a demonstration of using anchors,
25
operators that match only the beginning and ending positions
26
of the input string.
27
'''
28
print()
29
print(title)
30
31
# construct the grammar object
32
api =
Api
()
33
syntax =
'''line = &&line-begin text line-end
34
line-begin = (%^ / %d10)
35
line-end = (%$ / %d10)
36
text = 1*(%d32 / %d97-122 / %d65-90 / %d48-57)
37
'''
38
input_string =
'first line\nsecond line'
39
grammar = api.generate(syntax)
40
if(api.errors):
41
# report any errors
42
print(
'\nGrammar Errors'
)
43
print(api.display_errors())
44
else
:
45
# the first line
46
parser =
Parser
(grammar)
47
result = parser.parse(
48
utils.string_to_tuple(input_string),
49
sub_begin=0,
50
sub_length=11)
51
print(
'\n1) Parse the first line, begins at the beginning '
, end=
''
)
52
print(
'of the input string.'
)
53
print(result)
54
55
# the second line
56
result = parser.parse(
57
utils.string_to_tuple(input_string),
58
sub_begin=11,
59
sub_length=11)
60
print(
'\n2) Parse the second line, ends at the end '
, end=
''
)
61
print(
'of the input string.'
)
62
print(result)
63
64
# not a line
65
result = parser.parse(
66
utils.string_to_tuple(input_string),
67
sub_begin=1,
68
sub_length=10)
69
print(
'\n3) Parse some text that does not begin '
, end=
''
)
70
print(
'at the beginning of a line.'
)
71
print(result)
72
73
# negative look behind
74
syntax =
'''line = !!crlf text line-end
75
crlf = %d13.10
76
line-end = (%$ / %d10)
77
text = 1*(%d32 / %d97-122 / %d65-90 / %d48-57)
78
'''
79
input_string =
'\r\nfirst line\nsecond line'
80
grammar = api.generate(syntax)
81
if(api.errors):
82
# report any errors
83
print(
'\nGrammar Errors'
)
84
print(api.display_errors())
85
else
:
86
# the first line
87
parser =
Parser
(grammar)
88
# trace = Trace(parser, mode='dc')
89
result = parser.parse(
90
utils.string_to_tuple(input_string),
91
sub_begin=2,
92
sub_length=10)
93
print(
'\n4) First line may not be preceeded by CRLF.'
)
94
print(result)
95
96
# the second line
97
result = parser.parse(
98
utils.string_to_tuple(input_string),
99
sub_begin=13,
100
sub_length=11)
101
print(
'\n5) Second line is NOT preceeded by CRLF - OK'
)
102
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.trace
Definition:
trace.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.