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
trace.py
Go to the documentation of this file.
1
''' @file examples/basics/trace.py
2
@brief Demonstrates how to display a trace of the parser's
3
path through the parse tree.
4
5
Tracing my generate a lot of output. It may be advantagous to
6
pipe stdout to an alternate location for more casual perusal.
7
For example, if using linux:
8
> python3 %examples/basics/trace.py | less
9
or
10
> python3 %examples/basics/trace.py > /tmp/trace<br>
11
> vi /tmp/trace
12
'''
13
import
sys
14
import
os
15
# add the current working directory to the path
16
# DO NOT MOVE THE FOLLOWING STATEMENT
17
# if using autopep8 formatter, for example, set argument '--ignore=E402'
18
sys.path.append(os.getcwd())
19
from
apg_py.lib
import
utilities
as
utils
20
from
apg_py.lib
import
identifiers
as
id
21
from
apg_py.lib.parser
import
Parser
22
from
apg_py.lib.trace
import
Trace
23
from
apg_py.api.api
import
Api
24
25
title =
'''Demonstrate the display a trace of the parser's path
26
through the parse tree.
27
Tracing my generate a lot of output. It may be advantagous to
28
pipe stdout to an alternate location for more casual perusal.
29
For example, if using linux:
30
python3 examples/trace.py | less
31
'''
32
print()
33
print(title)
34
35
36
float =
'''float = sign decimal exponent
37
sign = ["+" / "-"]
38
decimal = integer [dot fraction]
39
/ dot fraction
40
integer = 1*%d48-57
41
dot = "."
42
fraction = *%d48-57
43
exponent = ["e" esign exp]
44
esign = ["+" / "-"]
45
exp = 1*%d48-57
46
'''
47
bad_float =
'''float = sign decimal exponent
48
sign = ["+" / "-"]
49
decimal = integer [dot fraction]
50
/ dot fraction
51
integer = 1*%d48-57
52
dot = ","
53
fraction = *%d48-57
54
exponent = ["e" esign exp]
55
esign = ["+" / "-"]
56
exp = 1*%d48-57
57
'''
58
59
# trace with error
60
api =
Api
()
61
grammar = api.generate(float)
62
if(api.errors):
63
# report any errors
64
print(
'\n1) Grammar Errors'
)
65
print(api.display_errors())
66
else
:
67
parser =
Parser
(grammar)
68
# trace using ASCII characters when possible, otherwise decimal digits
69
trace =
Trace
(parser, mode=
'dc'
)
70
input_string =
'-123,456789E-10'
71
result = parser.parse(utils.string_to_tuple(input_string))
72
print()
73
print(
'\n1) Parser Result - fails on bad input '
, end=
''
)
74
print(
'(input string has comma instead of period)'
)
75
print(result)
76
77
grammar = api.generate(bad_float)
78
if(api.errors):
79
# report any errors
80
print(
'\n1) Grammar Errors'
)
81
print(api.display_errors())
82
else
:
83
parser =
Parser
(grammar)
84
# trace using ASCII characters when possible, otherwise decimal digits
85
trace =
Trace
(parser, mode=
'dc'
)
86
input_string =
'-123.456789E-10'
87
result = parser.parse(utils.string_to_tuple(input_string))
88
print()
89
print(
'\n2) Parser Result - fails on bad grammar '
, end=
''
)
90
print(
'(dot is defined as a comma instead of period)'
)
91
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.lib.trace.Trace
Class for tracing and displaying the progress of the parser through the parse tree.
Definition:
trace.py:15
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.