-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
118 lines (102 loc) · 4.04 KB
/
app.py
File metadata and controls
118 lines (102 loc) · 4.04 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
from flask import Flask, render_template, request, jsonify
from compiler_phases.lexer import Lexer
from compiler_phases.parser import Parser
from compiler_phases.semantic_analyzer import SemanticAnalyzer
from compiler_phases.intermediate_code_generator import IntermediateCodeGenerator
from compiler_phases.optimizer import Optimizer
from compiler_phases.code_generator import CodeGenerator
from compiler_phases.target_optimizer import TargetOptimizer
from compiler_phases.symbol_table import SymbolTable
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/compile', methods=['POST'])
def compile_code():
try:
# Get input expression
expression = request.json.get('expression', '')
if not expression:
return jsonify({'error': 'No expression provided'}), 400
# Initialize symbol table
symbol_table = SymbolTable()
# Phase 1: Lexical Analysis
lexer = Lexer(expression)
tokens = lexer.tokenize()
# Phase 2: Parsing (AST Generation)
parser = Parser(tokens, symbol_table)
ast = parser.parse()
ast_visual = parser.visualize_ast(ast)
# Phase 3: Semantic Analysis
semantic_analyzer = SemanticAnalyzer(ast, symbol_table)
semantic_result = semantic_analyzer.analyze()
# Phase 4: Intermediate Code Generation (3-address code)
icg = IntermediateCodeGenerator(ast, symbol_table)
three_address_code = icg.generate()
# Phase 5: Optimization (Constant Folding)
optimizer = Optimizer(three_address_code)
optimized_code = optimizer.optimize()
# Phase 6: Code Generation (Assembly)
code_gen = CodeGenerator(optimized_code, symbol_table)
assembly_code = code_gen.generate()
# Phase 7: Target Code Optimization
target_optimizer = TargetOptimizer(assembly_code)
final_code = target_optimizer.optimize()
# Prepare response with all phases
result = {
'success': True,
'phases': {
'lexer': {
'title': 'Phase 1: Lexical Analysis',
'tokens': tokens
},
'parser': {
'title': 'Phase 2: Syntax Analysis (AST)',
'ast': ast_visual
},
'semantic': {
'title': 'Phase 3: Semantic Analysis',
'result': semantic_result
},
'icg': {
'title': 'Phase 4: Intermediate Code Generation',
'code': three_address_code
},
'optimizer': {
'title': 'Phase 5: Code Optimization',
'code': optimized_code
},
'codegen': {
'title': 'Phase 6: Code Generation',
'code': assembly_code
},
'target_optimizer': {
'title': 'Phase 7: Target Code Optimization',
'code': final_code
}
},
'symbol_table': symbol_table.get_table()
}
return jsonify(result)
except SyntaxError as e:
return jsonify({
'success': False,
'error': f'Syntax error: {str(e)}'
}), 400
except ValueError as e:
return jsonify({
'success': False,
'error': f'Invalid input: {str(e)}'
}), 400
except Exception as e:
# Log the error for debugging but don't expose details
print(f"Compilation error: {str(e)}")
return jsonify({
'success': False,
'error': 'An error occurred during compilation'
}), 400
if __name__ == '__main__':
import os
# Only enable debug in development
debug_mode = os.environ.get('FLASK_DEBUG', 'False').lower() == 'true'
app.run(debug=debug_mode, host='0.0.0.0', port=5000)