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
back_reference.py
Go to the documentation of this file.
1
''' @file examples/basics/back_reference.py
2
@brief Demonstrates two modes of back referencing.
3
- "universal" mode - The back reference matches the last
4
of all previously matched phrases regardless of where on the parse
5
tree it occurred.
6
- "recursive" mode - The back reference matches the previously
7
matched phrase which has the same recursive rule parent node.
8
'''
9
import
sys
10
import
os
11
# add the current working directory to the path
12
# DO NOT MOVE THE FOLLOWING STATEMENT
13
# if using autopep8 formatter, for example, set argument '--ignore=E402'
14
sys.path.append(os.getcwd())
15
from
apg_py.lib
import
utilities
as
utils
16
from
apg_py.lib.parser
import
Parser
17
from
apg_py.api.api
import
Api
18
19
title =
'''Demonstrates back referencing.
20
A "universal" mode back reference matches the last
21
of all previous matched phrases.
22
A "recursive" mode back reference matches the previously
23
matched phrase which has the same recursive rule parent node.
24
'''
25
print()
26
print(title)
27
28
usyntax =
'''html = "<" tag-name ">" [html] "</" \\tag-name ">"
29
tag-name = alpha *(alphanum)
30
alpha = %d97-122 / %d65-90
31
alphanum = alpha / %d48-57
32
'''
33
rsyntax =
'''html = "<" tag-name ">" [html] "</" \\%rtag-name ">"
34
tag-name = alpha *(alphanum)
35
alpha = %d97-122 / %d65-90
36
alphanum = alpha / %d48-57
37
'''
38
rssyntax =
'''html = "<" tag-name ">" [html] "</" \\%s%rtag-name ">"
39
tag-name = alpha *(alphanum)
40
alpha = %d97-122 / %d65-90
41
alphanum = alpha / %d48-57
42
'''
43
44
# construct the grammar object
45
api =
Api
()
46
grammar = api.generate(usyntax)
47
if(api.errors):
48
# report any errors
49
print(
'\nGrammar Errors'
)
50
print(api.display_errors())
51
else
:
52
parser =
Parser
(grammar)
53
input =
'<level1><level2><level3></level3></level3></level3>'
54
result = parser.parse(utils.string_to_tuple(input))
55
print(
'\n1) Universal mode - all closing tag names must be equal'
, end=
' '
)
56
print(
'to the last level opening tag name - not correct HTML.'
)
57
print(result)
58
59
# recursive - case insensitive
60
grammar = api.generate(rsyntax)
61
if(api.errors):
62
# report any errors
63
print(
'\nGrammar Errors'
)
64
print(api.display_errors())
65
else
:
66
parser =
Parser
(grammar)
67
input =
'<level1><level2><level3></Level3></LEVEL2></level1>'
68
result = parser.parse(utils.string_to_tuple(input))
69
print(
'\n2) Recursive mode - all closing tag names must be equal'
, end=
' '
)
70
print(
'to the matching opening tag name, however, case insensitive.'
)
71
print(result)
72
73
74
# recursive - case sensitive
75
grammar = api.generate(rssyntax)
76
if(api.errors):
77
# report any errors
78
print(
'\nGrammar Errors'
)
79
print(api.display_errors())
80
else
:
81
parser =
Parser
(grammar)
82
input =
'<level1><LEVEL2><level3></level3></LEVEL2></level1>'
83
result = parser.parse(utils.string_to_tuple(input))
84
print(
'\n2) Recursive mode - all closing tag'
, end=
' '
)
85
print(
'names must be equal to the matching opening tag name,'
, end=
''
)
86
print(
' case sensitive.'
)
87
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.