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
stats.py
Go to the documentation of this file.
1
''' @file examples/basics/stats.py
2
@brief Demonstrates how to display the parser's statistics.
3
4
Demonstrate the display of the parser's statistics.
5
Displays a count of how many nodes of each kind were hit and how many times.
6
Also displays how many times each rule and UDT name is hit.
7
Note that operators and rule/UDT names that have
8
a 0(zero) hit count are not displayed.
9
'''
10
import
sys
11
import
os
12
# add the current working directory to the path
13
# DO NOT MOVE THE FOLLOWING STATEMENT
14
# if using autopep8 formatter, for example, set argument '--ignore=E402'
15
sys.path.append(os.getcwd())
16
from
apg_py.lib
import
utilities
as
utils
17
from
apg_py.lib
import
identifiers
as
id
18
from
apg_py.lib.parser
import
Parser
19
from
apg_py.lib.stats
import
Stats
20
from
apg_py.api.api
import
Api
21
22
title =
'''Demonstrate the display of the parser's statistics.
23
Displays a count of how many nodes of each kind were hit and how many times.
24
Also displays how many times each rule and UDT name is hit.
25
Note that operators and rule/UDT names that have
26
a 0(zero) hit count are not displayed.
27
'''
28
print()
29
print(title)
30
31
32
def
udt_sign
(cbData):
33
# matches '+', '-' or empty string
34
cbData[
'phrase_length'
] = 0
35
cbData[
'state'
] = id.EMPTY
36
if(cbData[
'phrase_index'
] < cbData[
'sub_end'
]):
37
char = cbData[
'input'
][cbData[
'phrase_index'
]]
38
if(char == 43
or
char == 45):
39
cbData[
'phrase_length'
] = 1
40
cbData[
'state'
] = id.MATCH
41
42
43
def
udt_integer
(cbData):
44
# matches any string of digits 0-9
45
index = cbData[
'phrase_index'
]
46
length = 0
47
while(index < cbData[
'sub_end'
]):
48
char = cbData[
'input'
][index]
49
if(char >= 48
and
char <= 57):
50
length += 1
51
index += 1
52
else
:
53
break
54
if(length > 0):
55
cbData[
'state'
] = id.MATCH
56
cbData[
'phrase_length'
] = length
57
else
:
58
cbData[
'phrase_length'
] = 0
59
cbData[
'state'
] = id.NOMATCH
60
61
62
float =
'''float = sign decimal exponent
63
sign = e_sign ;["+" / "-"]
64
decimal = integer [dot fraction]
65
/ dot fraction
66
integer = u_integer; 1*%d48-57
67
dot = "."
68
fraction = *digit
69
exponent = ["e" esign exp]
70
esign = ["+" / "-"]
71
exp = 1*digit
72
digit = %d48-57
73
'''
74
anbncn =
'S = &(A !bb) 1*aa B !cc\n'
75
anbncn +=
'A = aa [A] bb\n'
76
anbncn +=
'B = bb [B] cc\n'
77
anbncn +=
'aa = "a"\n'
78
anbncn +=
'bb = "b"\n'
79
anbncn +=
'cc = "c"\n'
80
81
# UDT stats
82
api =
Api
()
83
grammar = api.generate(float)
84
if(api.errors):
85
# report any errors
86
print(
'\n1) Grammar Errors'
)
87
print(api.display_errors())
88
else
:
89
# use the grammar object to parse an input string
90
# input string must be a tuple of positive integers
91
parser =
Parser
(grammar)
92
stats =
Stats
(parser)
93
parser.add_callbacks({
'e_sign'
: udt_sign,
'u_integer'
: udt_integer})
94
input_string =
'-123.456789E-10'
95
result = parser.parse(utils.string_to_tuple(input_string))
96
print(
'\n1) Parser Result - floating point number with UDTs'
)
97
print(result)
98
stats.display()
99
100
# look ahead stats
101
grammar = api.generate(anbncn)
102
if(api.errors):
103
# report any errors
104
print(
'\n2) Grammar Errors'
)
105
print(api.display_errors())
106
else
:
107
# use the grammar object to parse an input string
108
# input string must be a tuple of positive integers
109
parser =
Parser
(grammar)
110
stats =
Stats
(parser)
111
input_string =
'aaaabbbbcccc'
112
result = parser.parse(utils.string_to_tuple(input_string))
113
print(
'\n2) Parser Result - grammar with look ahead operators '
, end=
''
)
114
print(
'&(AND) and !(NOT)'
)
115
print(result)
116
stats.display()
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.lib.stats.Stats
Definition:
stats.py:5
apg_py.api.api
Definition:
api.py:1
apg_py.lib.parser
Definition:
parser.py:1
apg_py.lib.stats
Definition:
stats.py:1
apg_py.lib
Definition:
__init__.py:1
examples.basics.stats.udt_integer
def udt_integer(cbData)
Definition:
stats.py:43
examples.basics.stats.udt_sign
def udt_sign(cbData)
Definition:
stats.py:32
Generated by
1.9.1
Python APG, Version 1.0, is licensed under the
2-Clause BSD License
,
an
Open Source Initiative
Approved License.