From e9f537bf974d93613bd39cdbf8889f30488ad3ab Mon Sep 17 00:00:00 2001 From: Leopoldo Teixeira Date: Tue, 14 Nov 2023 15:55:14 -0300 Subject: [PATCH] atualizando material de 14.11.2023 --- 2023-11-14.md | 3 ++- 2023-11-14/basic/parse.py | 46 +++++++++++++++++++++++----------- 2023-11-14/basic/simples.basic | 3 +++ 2023-11-14/simples/exp.txt | 2 +- 2023-11-14/simples/input.txt | 2 +- 2023-11-14/simples/parse.py | 45 ++++++++++++++++++++++++++++++++- 6 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 2023-11-14/basic/simples.basic diff --git a/2023-11-14.md b/2023-11-14.md index ba96f84..93a441d 100644 --- a/2023-11-14.md +++ b/2023-11-14.md @@ -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) \ No newline at end of file diff --git a/2023-11-14/basic/parse.py b/2023-11-14/basic/parse.py index 8ff0d55..d9ad8df 100644 --- a/2023-11-14/basic/parse.py +++ b/2023-11-14/basic/parse.py @@ -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 | @@ -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 \ No newline at end of file + 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") \ No newline at end of file diff --git a/2023-11-14/basic/simples.basic b/2023-11-14/basic/simples.basic new file mode 100644 index 0000000..f5ec64f --- /dev/null +++ b/2023-11-14/basic/simples.basic @@ -0,0 +1,3 @@ +INPUT x +LET y = x +PRINT y \ No newline at end of file diff --git a/2023-11-14/simples/exp.txt b/2023-11-14/simples/exp.txt index c3203b7..df749fe 100644 --- a/2023-11-14/simples/exp.txt +++ b/2023-11-14/simples/exp.txt @@ -1 +1 @@ -2123123 + 321312 - 41231 \ No newline at end of file +1 - 2 + 3232 \ No newline at end of file diff --git a/2023-11-14/simples/input.txt b/2023-11-14/simples/input.txt index 22be1ca..4ce77fa 100644 --- a/2023-11-14/simples/input.txt +++ b/2023-11-14/simples/input.txt @@ -1 +1 @@ -a b b c d e \ No newline at end of file +a b c d e \ No newline at end of file diff --git a/2023-11-14/simples/parse.py b/2023-11-14/simples/parse.py index 83c5abd..a3f6b55 100644 --- a/2023-11-14/simples/parse.py +++ b/2023-11-14/simples/parse.py @@ -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) \ No newline at end of file