Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
substrings.py
Go to the documentation of this file.
1 ''' @file examples/basics/substrings.py
2 @brief Demonstrate parsing substrings.
3 Often, expecially as used by the pattern-matching engine
4 @ref exp.py, one needs to parse only a sub-string of
5 the entire input string.
6 Here this is explicitly demonstrated.
7 '''
8 import sys
9 import os
10 # add the current working directory to the path
11 # DO NOT MOVE THE FOLLOWING STATEMENT
12 # if using autopep8 formatter, for example, set argument '--ignore=E402'
13 sys.path.append(os.getcwd())
14 from apg_py.lib import utilities as utils
15 from apg_py.lib.parser import Parser
16 from apg_py.api.api import Api
17 
18 title = '''Demonstrate the parsing of substrings of the full input string.
19 '''
20 print()
21 print(title)
22 
23 abnf_syntax = 'S = "a" S / "y"\n'
24 # abnf_syntax_strict = 'S = "a" S / "y"\r\n'
25 input_string = '***aaay***'
26 beg = 3
27 len = 4
28 print('The grammar:', end=' ')
29 print(abnf_syntax)
30 print('The full input string:', end=' ')
31 print(input_string)
32 print('The sub string:', end=' ')
33 print(input_string[beg:beg + len])
34 
35 # construct the grammar object
36 api = Api()
37 grammar = api.generate(abnf_syntax)
38 if(api.errors):
39  # report any errors
40  print('\n1) Grammar Errors')
41  print(api.display_errors())
42 else:
43  # use the grammar object to parse an input string
44  # input string must be a tuple of positive integers
45  parser = Parser(grammar)
46  result = parser.parse(
47  utils.string_to_tuple(input_string),
48  sub_begin=beg,
49  sub_length=len)
50  print('\n1) Parser Result')
51  print(result)
The API class.
Definition: api.py:61
The Parser class for parsing an APG grammar.
Definition: parser.py:60
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.