Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
main.py
Go to the documentation of this file.
1 ''' @file examples/ast/main.py
2 @brief Example of using the Abstract Syntax Tree (AST).
3 The AST can be considered a sub-tree of the full parse tree.
4 It only has nodes for named phrases (rule name and UDT name).
5 The user has control over which rule and UDT names to retain on the AST
6 and the parser only retains those nodes with successful phrase matches.
7 This means that when traversing the AST, as opposed to the full
8 parse tree during parsing, only named nodes of interest are encountered
9 and the matched phrase is available in both the downward and upward directions.
10 This avoids reporting information from rules that are matched but exist
11 on parse tree branches that ultimately fail.
12 @dir examples/ast All of the files for the AST demonstration.
13 '''
14 import sys
15 import os
16 # add the current working directory to the path
17 # DO NOT MOVE THE FOLLOWING STATEMENT
18 # if using autopep8 formatter, for example, set argument '--ignore=E402'
19 sys.path.append(os.getcwd())
20 from apg_py.lib import utilities as utils
21 from apg_py.lib.parser import Parser
22 from apg_py.lib.ast import Ast
23 from apg_py.api.api import Api
24 import examples.ast.ast_callbacks as ast_callbacks
25 import examples.ast.parser_callbacks as parser_callbacks
26 
27 
28 title = '''Demonstrate the use of the Abstract Syntax Tree.
29 Demonstrates how to use the AST and why it is useful.
30 '''
31 print()
32 print(title)
33 
34 abnf_syntax = 'S = alt1 / alt2\n'
35 abnf_syntax += 'alt1 = X A\n'
36 abnf_syntax += 'alt2 = X B\n'
37 abnf_syntax += 'X = "xyz"\n'
38 abnf_syntax += 'A = "aaa"\n'
39 abnf_syntax += 'B = "bbb"\n'
40 print()
41 print('the ABNF grammar syntax')
42 print(abnf_syntax)
43 
44 # construct the grammar object
45 api = Api()
46 grammar = api.generate(abnf_syntax)
47 if(api.errors):
48  # report any errors
49  print('\n1) Grammar Errors')
50  print(api.display_errors())
51 else:
52  # translations done with rule callback functions
53  parser = Parser(grammar)
54  parser.add_callbacks({'A': parser_callbacks.A_rule,
55  'B': parser_callbacks.B_rule,
56  'X': parser_callbacks.X_rule})
57  parser.add_callbacks(
58  {'alt1': parser_callbacks.alt1_rule,
59  'alt2': parser_callbacks.alt2_rule})
60  data = []
61  input_string = 'xyzbbb'
62  result = parser.parse(utils.string_to_tuple(input_string), user_data=data)
63  print('\n1) Rule Translation Result - input = "' + input_string + '"')
64  for string in data:
65  print(string)
66  print('NOTE: "xyz" found in rule X twice because', end=' ')
67  print('the rule alt1 failed afer rule X succeeded.')
68  data = []
69  input_string = 'xyzaaa'
70  result = parser.parse(utils.string_to_tuple(input_string), user_data=data)
71  print('\n2) Rule Translation Result - input = "' + input_string + '"')
72  for string in data:
73  print(string)
74 
75  # translations done with AST callback functions
76  parser = Parser(grammar)
77  ast = Ast(parser)
78  ast.add_callback('A', ast_callbacks.A_ast)
79  ast.add_callback('B', ast_callbacks.B_ast)
80  ast.add_callback('X', ast_callbacks.X_ast)
81  ast.add_callback('alt1', ast_callbacks.alt1_ast)
82  ast.add_callback('alt2', ast_callbacks.alt2_ast)
83  data = []
84  input_string = 'xyzbbb'
85  result = parser.parse(utils.string_to_tuple(input_string))
86  ast.translate(data)
87  print('\n3) AST Translation Result - input = "' + input_string + '"')
88  for string in data:
89  print(string)
90  data = []
91  input_string = 'xyzaaa'
92  result = parser.parse(utils.string_to_tuple(input_string))
93  ast.translate(data)
94  print('\n4) AST Translation Result - input = "' + input_string + '"')
95  for string in data:
96  print(string)
The API class.
Definition: api.py:61
A class for capturing the AST as the parser traverses the parse tree.
Definition: ast.py:69
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.