Última atividade 1716315925

kristofer's Avatar kristofer revisou este gist 1716315925. Ir para a revisão

1 file changed, 3 insertions

symboltable1.md

@@ -1,5 +1,8 @@
1 1 describe how you would use python's dict data structure to build a symbol table for a small language compiler
2 2
3 + ## Phi3
4 +
5 +
3 6 Building a symbol table using Python's dictionary data structure for a small language compiler involves several
4 7 steps. The symbol table will be used to keep track of variables, their types, and scopes during the compilation
5 8 process. Here's how you can implement it:

kristofer's Avatar kristofer revisou este gist 1716315858. Ir para a revisão

1 file changed, 75 insertions

symboltable1.md(arquivo criado)

@@ -0,0 +1,75 @@
1 + describe how you would use python's dict data structure to build a symbol table for a small language compiler
2 +
3 + Building a symbol table using Python's dictionary data structure for a small language compiler involves several
4 + steps. The symbol table will be used to keep track of variables, their types, and scopes during the compilation
5 + process. Here's how you can implement it:
6 +
7 + 1. Define basic classes
8 + Firstly, create two essential classes - `Symbol` and `Scope`.
9 + ```python
10 + class Symbol:
11 + def __init__(self, name, symbol_type):
12 + self.name = name
13 + self.symbol_type = symbol_type
14 +
15 + class Scope:
16 + def __init__(self, parent=None):
17 + self.parent = parent
18 + self.scopes = [] # list of symbols in this scope
19 + ```
20 + 2. Initialize the global symbol table
21 + Create a class `SymbolTable` which will manage all scopes and keep track of symbols globally using Python's
22 + dictionary data structure.
23 + ```python
24 + class SymbolTable:
25 + def __init__(self):
26 + self.symbols = {} # Key: symbol_name, Value: (Scope object)
27 + ```
28 + 3. Add new symbols to the table
29 + Define a function `addSymbol` in `SymbolTable`, which adds symbols according to their scopes and types. This
30 + method ensures that overlapping names are handled gracefully by using the scope's parent as reference if
31 + necessary.
32 + ```python
33 + class SymbolTable:
34 + # ... existing code ...
35 +
36 + def addSymbol(self, symbol_name, symbol_type):
37 + current_scope = self.symbols.get(symbol_name)
38 +
39 + if not current_scope: # If the symbol doesn't exist in any scope yet, create it at global level
40 + new_scope = Scope()
41 + self.symbols[symbol_name] = (new_scope, SymbolType(symbol_type))
42 + return new_scope
43 +
44 + # Else check if we are dealing with an inner symbol
45 + else:
46 + scope = current_scope[0] # parent Scope object
47 +
48 + while scope.parent and scope.name in self.symbols: # Check all parents until a free name is found
49 + scope = scope.parent
50 +
51 + if not scope.parent or scope.name not in self.symbols: # Add the symbol to current scope if there's
52 + no conflict with any parent scope
53 + new_scope = Scope(parent=scope)
54 + self.symbols[symbol_name] = (new_scope, SymbolType(symbol_type))
55 + else:
56 + raise NameError('Symbol name already exists in this or one of the parent scopes')
57 + ```
58 + 4. Retrieve symbols from the table
59 + Define a method `getSymbol` to retrieve symbols by their names according to their respective scope and type
60 + information.
61 + ```python
62 + class SymbolTable:
63 + # ... existing code ...
64 +
65 + def getSymbol(self, symbol_name):
66 + if symbol_name in self.symbols:
67 + return self.symbols[symbol_name]
68 + else:
69 + raise NameError('No such symbol')
70 + ```
71 +
72 + With this implementation, you've created a basic symbol table using Python dictionaries that can track variables
73 + and their types for a small language compiler. You can extend it further by implementing methods to handle more
74 + complex scenarios like type inference or variable renaming during optimization stages of the compilation process.
75 +
Próximo Anterior