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.
19 sys.path.append(os.getcwd())
28 title =
'''Demonstrate the use of the Abstract Syntax Tree.
29 Demonstrates how to use the AST and why it is useful.
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'
41 print(
'the ABNF grammar syntax')
46 grammar = api.generate(abnf_syntax)
49 print(
'\n1) Grammar Errors')
50 print(api.display_errors())
54 parser.add_callbacks({
'A': parser_callbacks.A_rule,
55 'B': parser_callbacks.B_rule,
56 'X': parser_callbacks.X_rule})
58 {
'alt1': parser_callbacks.alt1_rule,
59 'alt2': parser_callbacks.alt2_rule})
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 +
'"')
66 print(
'NOTE: "xyz" found in rule X twice because', end=
' ')
67 print(
'the rule alt1 failed afer rule X succeeded.')
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 +
'"')
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)
84 input_string =
'xyzbbb'
85 result = parser.parse(utils.string_to_tuple(input_string))
87 print(
'\n3) AST Translation Result - input = "' + input_string +
'"')
91 input_string =
'xyzaaa'
92 result = parser.parse(utils.string_to_tuple(input_string))
94 print(
'\n4) AST Translation Result - input = "' + input_string +
'"')
A class for capturing the AST as the parser traverses the parse tree.
The Parser class for parsing an APG grammar.