-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0842cc5
commit f194bf5
Showing
19 changed files
with
1,118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# IF688 - Teoria e Implementação de Linguagens Computacionais | ||
|
||
## Análise Semântica - Escopo, Tipos, Tabelas de Símbolos | ||
|
||
### Objetivo | ||
|
||
O objetivo desta aula é introduzir alguns conceitos gerais relacionados à análise semântica, como questões associadas a escopo, tipos e tabelas de símbolos. | ||
|
||
### Vídeos | ||
|
||
- [Análise Semântica (Tipos, Escopo & Tabela de Símbolos)](https://www.youtube.com/playlist?list=PLHoVp5NAbKJb4L5y5qIZ8JKGujjYTVzIM) | ||
|
||
### Links Relacionados | ||
|
||
- [Symbol Table](https://en.wikipedia.org/wiki/Symbol_table) | ||
- [Type System](https://en.wikipedia.org/wiki/Type_system) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# IF688 - Teoria e Implementação de Linguagens Computacionais | ||
|
||
## Análise Semântica - Tabelas de Símbolos e Type Checking | ||
|
||
### Objetivo | ||
|
||
O objetivo desta aula é consolidar os conceitos relacionados à análise semântica, em particular a noção de tabelas de símbolos e a verificação de tipos usando *visitors*. | ||
|
||
### Questões para Discussão | ||
|
||
- Como usar tabelas de símbolos como estruturas auxiliares para análise semântica? | ||
- Como coletar informações e armazenar ao executar uma travessia da AST com *visitors*? | ||
|
||
### Material usado em aula | ||
|
||
- Código desenvolvido em sala de aula | ||
- [versão para o início da aula (.zip com código incompleto)](https://drive.google.com/file/d/1FEeA9LKLOs4gB9luTpHRHieyqBhpvVKN/view) | ||
- [versão do código ao final da aula (.zip com código escrito durante a aula](https://drive.google.com/file/d/1HOl8xT7f8UkWsTKXFv9LWrzCRFebgD6Y/view) | ||
|
||
### Links Relacionados | ||
|
||
- [Symbol Table](https://en.wikipedia.org/wiki/Symbol_table) | ||
- [Type System](https://en.wikipedia.org/wiki/Type_system) | ||
- [Type Safety](https://en.wikipedia.org/wiki/Type_safety) | ||
- [Strong and Weak Typing](https://en.wikipedia.org/wiki/Strong_and_weak_typing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
class Program(object): | ||
def __init__(self,name,ls): | ||
self.name = name | ||
self.stmts = ls | ||
|
||
class Stmt(object): | ||
pass | ||
|
||
class PrintStmt(Stmt): | ||
def __init__(self, exp): | ||
self.exp = exp | ||
|
||
class InputStmt(Stmt): | ||
def __init__(self, nome): | ||
self.id = nome | ||
|
||
class VarDeclStmt(Stmt): | ||
def __init__(self, nome, t): | ||
self.id = nome | ||
self.type = t | ||
|
||
class LetStmt(Stmt): | ||
def __init__(self, nome, e): | ||
self.id = nome | ||
self.exp = e | ||
|
||
class BlockStmt(Stmt): | ||
def __init__(self, nome, corpo): | ||
self.name = nome | ||
self.body = corpo | ||
|
||
class IfStmt(Stmt): | ||
def __init__(self, c, corpo): | ||
self.cond = c | ||
self.body = corpo | ||
|
||
class WhileStmt(Stmt): | ||
def __init__(self, c, corpo): | ||
self.cond = c | ||
self.body = corpo | ||
|
||
class Expr(object): | ||
pass | ||
|
||
class IdExpr(Expr): | ||
def __init__(self, nome): | ||
self.id = nome | ||
|
||
class StringExpr(Expr): | ||
def __init__(self, s): | ||
self.str = s | ||
|
||
class ArithExpr(Expr): | ||
pass | ||
|
||
class SumExpr(ArithExpr): | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class SubExpr(ArithExpr): | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class MulExpr(ArithExpr): | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class DivExpr(ArithExpr): | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class UnaryPlusExpr(ArithExpr): | ||
def __init__(self, e): | ||
self.exp = e | ||
|
||
class UnaryMinusExpr(ArithExpr): | ||
def __init__(self, e): | ||
self.exp = e | ||
|
||
class NumExpr(ArithExpr): | ||
def __init__(self, valor): | ||
self.v = valor | ||
|
||
class BooleanExpr(Expr): | ||
pass | ||
|
||
class EqualsExpr(BooleanExpr):# left == right | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class NotEqualsExpr(BooleanExpr):# left != right | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class GreaterThanEqualsExpr(BooleanExpr):# left >= right | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class GreaterThanExpr(BooleanExpr):# left > right | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class LessThanEqualsExpr(BooleanExpr):# left <= right | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito | ||
|
||
class LessThanExpr(BooleanExpr):# left < right | ||
def __init__(self, ladoEsquerdo, ladoDireito): | ||
self.left = ladoEsquerdo | ||
self.right = ladoDireito |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from lexer import * | ||
from parse import * | ||
from visitor import * | ||
import sys | ||
|
||
def main(): | ||
if len(sys.argv) != 2: | ||
sys.exit("Erro: Precisamos de um arquivo como argumento.") | ||
with open(sys.argv[1], 'r') as inputFile: | ||
input = inputFile.read() | ||
|
||
lexer = Lexer(input) | ||
parser = Parser(lexer) | ||
program = parser.parse() | ||
|
||
print(program) | ||
# visitor = CountPrint(program) | ||
# print('Antes de contar: '+ str(visitor.numPrint)) | ||
# visitor.contar() | ||
# print('Depois de contar: '+ str(visitor.numPrint)) | ||
|
||
|
||
# visitor = PrintAST(program) | ||
# print(visitor.printAST()) | ||
|
||
# visitor = BuildSymbolTable(program) | ||
# symbolTable = visitor.build() | ||
# print(symbolTable) | ||
|
||
visitor = TypeCheckVisitor(program) | ||
visitor.typecheck() | ||
print(visitor.symbolTable) | ||
|
||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
PROGRAM outronome BEGIN | ||
PRINT "Type a number" | ||
DECL num : INT | ||
INPUT num | ||
IF num > 100 THEN | ||
PRINT "Maior que 100" | ||
ENDIF | ||
DECL x : INT | ||
IF num < 100 THEN | ||
PRINT "Menor que 100" | ||
ENDIF | ||
DECL y : INT | ||
IF num == 100 THEN | ||
PRINT "Digitou 100" | ||
ENDIF | ||
ENDPROGRAM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
PROGRAM fibonacci BEGIN | ||
PRINT "How many fibonacci numbers do you want?" | ||
DECL nums : INT | ||
INPUT nums | ||
DECL a : INT | ||
DECL b : INT | ||
DECL c : INT | ||
DECL d : INT | ||
DECL e : STRING | ||
DECL f : BOOLEAN | ||
LET a = 0 | ||
LET b = 1 | ||
|
||
WHILE nums > 0 REPEAT | ||
PRINT a | ||
LET c = a + b | ||
LET a = b | ||
LET b = c | ||
LET nums = nums - 1 | ||
ENDWHILE | ||
|
||
PRINT b | ||
BLOCK teste BEGIN | ||
DECL w : STRING | ||
LET w = "Algum valor" | ||
PRINT w | ||
ENDBLOCK | ||
PRINT b | ||
ENDPROGRAM |
Oops, something went wrong.