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
exp
recursive.py
Go to the documentation of this file.
1
''' @file examples/exp/recursive.py
2
@brief Demonstrates using recursive rules for matching nested pairs.
3
'''
4
import
sys
5
import
os
6
# add the current working directory to the path
7
# DO NOT MOVE THE FOLLOWING STATEMENT
8
# if using autopep8 formatter, for example, set argument '--ignore=E402'
9
sys.path.append(os.getcwd())
10
from
apg_py.exp.exp
import
ApgExp
11
12
title =
'''This example will demonstrate the use of
13
recursive grammar rules for matching nested pairs.
14
Note that the begin and end of string anchors,
15
%^ and %$, are used so that the pattern must
16
match the entire string or fail.
17
'''
18
print()
19
print(title)
20
21
L =
'L = "("\n'
22
R =
'R = ")"\n'
23
# PM = 'match = %^ P %$\n'
24
PM =
'P = L P R / L R\n'
25
PM += L
26
PM += R
27
PG =
'match = %^ P %$\n'
28
PG +=
'P = L 1*P R / L R\n'
29
PG += L
30
PG += R
31
PT =
'match = %^ P %$\n'
32
PT +=
'P = L text 1*(P text) R / L text R\n'
33
PT +=
'L = "<"\n'
34
PT +=
'R = ">"\n'
35
PT +=
'text = *(%d32-59 / %d61 / %d63-126)\n'
36
header =
'RESULT'
37
testno = 0
38
39
# simple parentheses matching
40
exp =
ApgExp
(PM)
41
exp.include([
'P'
])
42
input =
'((()))'
43
result = exp.exec(input)
44
testno += 1
45
print(
'\n'
+ str(testno) +
') Simple parethesis matching'
)
46
print(
'input string: '
+ input)
47
print(header)
48
print(result)
49
input =
'((())'
50
result = exp.exec(input)
51
testno += 1
52
print(
'\n'
+ str(testno) +
') Simple parentheses matching - unmatched input'
)
53
print(
'input string: '
+ input)
54
print(header)
55
print(result)
56
57
# internal pairs parentheses matching
58
exp =
ApgExp
(PG)
59
exp.include([
'P'
])
60
input =
'(()(())())'
61
result = exp.exec(input)
62
testno += 1
63
print(
'\n'
+ str(testno) +
') Internal pairs of parentheses matching.'
)
64
print(
'input string: '
+ input)
65
print(header)
66
print(result)
67
68
# internal pairs of parentheses with text
69
exp =
ApgExp
(PT)
70
exp.include([
'P'
,
'text'
])
71
input =
'<up 1<up 2< middle 1 >between<middle 2>down 2>down 1>'
72
result = exp.exec(input)
73
testno += 1
74
print(
'\n'
+ str(testno) +
') Internal pairs of parentheses with text.'
)
75
print(
'input string: '
+ input)
76
print(header)
77
print(result)
apg_py.exp.exp.ApgExp
The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp
Definition:
exp.py:79
apg_py.exp.exp
Definition:
exp.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.