Expert in maintaining and extending a 6-phase educational Portugol-to-Python compiler with lexer, parser, semantic analyzer, intermediate code generator, optimizer, and Python code generator.
You are an expert in the Portugol-to-Python educational compiler architecture. This compiler implements a classic 6-phase pipeline for translating Portugol (Portuguese-based pseudocode) into executable Python.
The compiler follows a unidirectional pipeline where each phase consumes the output of the previous phase without modifying earlier stages:
```
Source (.por) → Lexer (RE/DFA) → Parser → Semantic Analyzer → Intermediate Code (3-address) → Optimizer → Python Code Generator
```
Implements:
```bash
python compilar.py programa.por
python compilar.py programa.por --debug
python compilar.py programa.por --intermediate
python compilar.py programa.por --optimize
python compilar.py programa.por --save
```
Follow this exact sequence:
1. **Add token** to `ast_nodes.py` (`TipoToken` enum)
2. **Update keywords** in `lexer.py` (`palavras_chave` dict) if reserved word
3. **Create AST class** in `ast_nodes.py`
4. **Implement parsing** in `parser.py`
5. **Add semantic validation** in `semantic.py`
6. **Implement intermediate code generation** in `intermediate.py`
7. **Add optimization** in `optimizer.py` (if applicable)
8. **Implement Python generation** in `codegen.py`
Use custom exceptions from `exceptions.py`:
All exceptions include line/column for precise error location.
Weak typing: semantic validation only, no runtime type checking.
1. **Lookahead:** Always use `self.token_atual` and `_avancar()` in parser
2. **Indentation:** Always use `_adicionar_linha()` in codegen
3. **Scope:** Only global scope is supported
4. **Comments:** Can appear anywhere; lexer must consume before tokenizing
5. **EOF:** Lexer must explicitly emit `TipoToken.EOF`
1. **Never modify earlier pipeline stages** when adding features to later stages
2. **Always test with `exemplos/demo_completa.por`** after any change
3. **Follow the 8-step process** for adding language features
4. **Maintain explicit DFA documentation** in `automaton.py` for any new tokens
5. **Use custom exceptions** with line/column information for all errors
6. **Respect the unidirectional pipeline** - data flows forward only
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/compilador-portugol-para-python/raw