Skip to content

Commit

Permalink
atualizando material de 14.11.2023
Browse files Browse the repository at this point in the history
  • Loading branch information
leopoldomt committed Nov 14, 2023
1 parent 1ed8488 commit e9f537b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 18 deletions.
3 changes: 2 additions & 1 deletion 2023-11-14.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ O objetivo desta aula é apresentar os conceitos fundamentais relacionados à an
- [Slides (pdf)](https://drive.google.com/file/d/1kl31m3eYJOFruHxga253PqdkdWRAwmN5/view)
- 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/1JPpfyt31qfbaNtlbSW-GGz4FU2WWMuH8/view?usp=drive_web&authuser=0)
- [versão do código ao final da aula (.zip com código escrito durante a aula)](https://drive.google.com/file/u/0/d/1D3WM7y-q3Q8AJQxoFk699XPDmW61CDXS/view?usp=drive_web)

### Links Relacionados

- [Recursive descent parser](https://en.wikipedia.org/wiki/Recursive_descent_parser)
- [Recursive descent parser](https://en.wikipedia.org/wiki/Recursive_descent_parser)
46 changes: 32 additions & 14 deletions 2023-11-14/basic/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ def abort(self, msg):
sys.exit("Erro sintático: "+msg)

def parse(self):
pass
self.program()
self.match(TokenType.EOF)

# program ::= statement
def program(self):
pass
while not self.checkToken(TokenType.EOF):
self.statement()

# statement ::=
# PRINT expression nl |
Expand All @@ -47,44 +49,60 @@ def program(self):
# IF expression "THEN" nl {statement} ENDIF nl
def statement(self):
# PRINT expression nl |

if self.checkToken(TokenType.PRINT):
self.match(TokenType.PRINT)
self.expression()
# INPUT IDENTIFICADOR nl |

elif self.checkToken(TokenType.INPUT):
self.match(TokenType.INPUT)
self.match(TokenType.IDENTIFICADOR)
# LET IDENTIFICADOR "=" expression nl |

elif self.checkToken(TokenType.LET):
self.match(TokenType.LET)
self.match(TokenType.IDENTIFICADOR)
self.match(TokenType.EQ)
self.expression()

# WHILE expression REPEAT nl {statement} ENDWHILE nl |

# IF expression "THEN" nl {statement} ENDIF nl

pass
self.nl()

def nl(self):
pass
self.match(TokenType.QUEBRA_LINHA)

# expression ::== equality
def expression(self):
pass
self.equality()

# equality ::== comparison ( ("==" | "!=" ) comparison)*
def equality(self):
pass
self.comparison()

# comparison ::== term ( ("<" | "<=" | ">" | ">=" ) term)*
def comparison(self):
pass
self.term()

# term ::== factor {("-" | "+") factor}
def term(self):
pass
self.factor()

# factor ::== unary {("*" | "/") unary}
def factor(self):
pass
self.unary()

# unary ::== ["-" | "+" ] unary | primary
def unary(self):
pass
self.primary()

# primary ::== NUM | ID | STRING
def primary(self):
pass
if self.checkToken(TokenType.NUMERO):
self.match(TokenType.NUMERO)
elif self.checkToken(TokenType.IDENTIFICADOR):
self.match(TokenType.IDENTIFICADOR)
elif self.checkToken(TokenType.STRING_LITERAL):
self.match(TokenType.STRING_LITERAL)
else:
self.erro("Token inesperado")
3 changes: 3 additions & 0 deletions 2023-11-14/basic/simples.basic
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INPUT x
LET y = x
PRINT y
2 changes: 1 addition & 1 deletion 2023-11-14/simples/exp.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2123123 + 321312 - 41231
1 - 2 + 3232
2 changes: 1 addition & 1 deletion 2023-11-14/simples/input.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a b b c d e
a b c d e
45 changes: 44 additions & 1 deletion 2023-11-14/simples/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,50 @@ def abort(self, msg):
sys.exit("Erro sintático: "+msg)

def parse(self):
pass
self.expr()

############################
def S(self):
self.match(TipoToken.a)
self.A()
self.B()
self.match(TipoToken.e)

def A(self):
self.match(TipoToken.b)
self.K()

def K(self):
if self.checkToken(TipoToken.b):
self.match(TipoToken.b)
self.match(TipoToken.c)
self.K()
else:
pass

def B(self):
self.match(TipoToken.d)


############
def expr(self):
self.term()
self.rest()

def rest(self):
if self.checkToken(TipoToken.SOMA):
self.match(TipoToken.SOMA)
self.term()
self.rest()
elif self.checkToken(TipoToken.SUBTRACAO):
self.match(TipoToken.SUBTRACAO)
self.term()
self.rest()
elif self.checkToken(TipoToken.EOF):
pass
else:
self.abort("Token inesperado: "+str(self.tokenAtual.tipo))

def term(self):
self.match(TipoToken.NUMERO)

0 comments on commit e9f537b

Please sign in to comment.