-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor_language_parser.py
More file actions
executable file
·145 lines (105 loc) · 3.95 KB
/
preprocessor_language_parser.py
File metadata and controls
executable file
·145 lines (105 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/env python3
# This file is a part of the Pepper project, https://github.com/devosoft/Pepper
# (C) Michigan State University, under the MIT License
# See LICENSE.txt for more information
"""
This is the Parser module for Pepper
This module implements the grammar for the preprocessor language, comprised of tokens from the Lexer module.
This module implements a main function, but this is only for debugging and will be removed on release.
"""
# flake8: noqa E501
import pepper.symbol_table as symtable
import pepper.abstract_symbol_tree as ast
import sys
import argparse
import ply.yacc as yacc
from pepper.preprocessor_language_lexer import lexer
from pepper.preprocessor_language_lexer import tokens # NOQA
import pepper.symbol_table as symtable
from pepper.evaluator import parse_lines, parse_macro
from pepper.symbol_table import Node
from typing import List, cast
print(f"tokens: {tokens}")
def p_statement(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
program : '#' preprocessing_statement NEWLINE
"""
pass
def p_preprocessing_statement_to_all_statement_types(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
preprocessing_statement : define_statement
| include_statement
"""
pass
def p_define_statement_structure(p: yacc.YaccProduction) -> yacc.YaccProduction:
pass
def p_define_expression_no_args(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
define_statement : PREPROCESSING_KEYWORD_DEFINE maybe_space IDENTIFIER maybe_space expressions
"""
# p[0] = symtable.MacroExpansion(p[3], p[5])
pass
# def p_define_expression_some_args(p: yacc.YaccProduction) -> yacc.YaccProduction:
# """
# define_expression : PREPROCESSING_KEYWORD_DEFINE maybe_space IDENTIFIER '(' identifier_list ')' maybe_space expressions
# """
# # print(f"Macro expansion for ident {p[3]} with args {p[5]}")
# # p[0] = symtable.MacroExpansion(p[3], p[8], args=p[5])
# pass
def p_include_expression_disambiguation(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
include_statement : include_statement_file
| include_statement_system
"""
p[0] = p[1]
def p_include_expression_file(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
include_statement_file : PREPROCESSING_KEYWORD_INCLUDE maybe_space STRING_LITERAL
"""
p[0] = ast.PreprocessorIncludeNode([p[3]], False)
def p_include_expression_system(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
include_statement_system : PREPROCESSING_KEYWORD_INCLUDE maybe_space SYSTEM_INCLUDE_LITERAL
"""
p[0] = ast.PreprocessorIncludeNode([p[3]], True)
def p_maybe_space_empty(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
maybe_space :
"""
pass
def p_maybe_space_nonempty(p: yacc.YaccProduction) -> yacc.YaccProduction:
"""
maybe_space : WHITESPACE
"""
pass
def p_expressions(p):
"""
expressions : IDENTIFIER
"""
pass
def p_error(p: yacc.YaccProduction) -> yacc.YaccProduction:
print(f"ERROR(line {p.lineno}): syntax error")
print(p)
raise symtable.PepperSyntaxError()
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('input_file',
type=argparse.FileType('r'),
default=sys.stdin,
help="The file to parse")
parser.add_argument('--debug_mode', action='store_true')
return parser.parse_args()
def parse(source: str, debug_mode: bool=False) -> Node:
if debug_mode:
parser = yacc.yacc(debug=True)
else:
parser = yacc.yacc(debug=False, errorlog=yacc.NullLogger())
parse_tree: Node = parser.parse(source, lexer=lexer)
return parse_tree
def main() -> None:
args = get_args()
# source = "\n".join(args.input_file.readlines())
parse_tree = parse(args.input_file.read(), args.debug_mode)
print(parse_tree)
if __name__ == "__main__":
main()