Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
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)
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.