1 / 28

Análise Sintática – Parte 2

Análise Sintática – Parte 2. Árvores Sintáticas Abstratas ( AST s) Scanning. Representação da árvore (Programa). Program. C. AST s em Java – Programas. public abstract class AST { … } public class Program extends AST { public Command C; … }. Representação da árvore (Comando).

lea
Download Presentation

Análise Sintática – Parte 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Análise Sintática – Parte 2 • Árvores Sintáticas Abstratas (ASTs) • Scanning

  2. Representação da árvore (Programa) Program C

  3. ASTs em Java – Programas public abstract class AST { … }public class Program extends AST { public Command C; …}

  4. Representação da árvore (Comando) AssignCommand CallCommand SequentialCommand V E Identifier E C1 C2 spelling IfCommand WhileCommand LetCommand E C1 C2 E C D C

  5. ASTs em Java – Comandos public abstract class Command extends AST { … }public class AssignCommand extends Command { public Vname V; public Expression E; …}public class CallCommand extends Command { public Identifier I; public Expression E; …}

  6. ASTs em Java – Comandos (2) public class SequentialCommand extends Command { public Command C1,C2; …}public class IfCommand extends Command { public Expression E; public Command C1,C2; …}

  7. ASTs em Java – Comandos (3) public class WhileCommand extends Command { public Expression E; public Command C; …}public class LetCommand extends Command { public Declaration D; public Command C; …}

  8. Representação da árvore (Expressão) IntegerExpression VnameExpression IntegerLiteral V spelling BinaryExpression UnaryExpression E1 Operator E2 Operator E spelling spelling

  9. ASTs em Java – Expressões public abstract class Expression extends AST { … }public class UnaryExpression extends Expression { public Operator O; public Expression E; …}public class BinaryExpression extends Expression { public Operator O; public Expression E1,E2; …}

  10. Representação da árvore (V-name) SimpleVname Identifier spelling

  11. ASTs em Java – Vname public abstract class Vname extends AST { … }public class SimpleVname extends Vname { public Identifier I; …}

  12. Representação da árvore (Declaração) ConstDeclaration VarDeclaration Identifier E Identifier E spelling spelling SequentialDeclaration D1 D2

  13. ASTs em Java – Declaração public abstract class Declaration extends AST { … }public class ConstDeclaration extends Declaration { public Identifier I; public Expression E; …}public class VarDeclaration extends Declaration { public Identifier I; public TypeDenoter T; …}

  14. Representação da árvore (Denotador de Tipos) SimpleTypeDenoter Identifier spelling

  15. ASTs em Java – Denotador de Tipos public abstract class TypeDenoter extends AST { … }public class SimpleTypeDenoter extends TypeDenoter { public Identifier I; …}

  16. ASTs em Java – Terminais public abstract class Terminal extends AST { public String spelling; … }public class Identifier extends Terminal { …}public class IntegerLiteral extends Terminal { …}public class Operator extends Terminal { …}

  17. ASTs em Java – Construção da árvore private ASTN parseN ( ) { ASTN itsAST;parse N, at the same time constructing itsAST return itsAST;}

  18. ASTs em Java – Exemplo: ParseSingleDeclaration private DeclarationparseSingleDeclaration ( ) { Declaration declAST; switch (currentToken.kind) { case Token.CONST: { acceptIt( ); Identifier iAST = parseIdentifier( ); accept(Token.IS); Expression eAST = parseExpression( ); declAST = new ConstDeclaration(iAST, eAST); } break; case Token.VAR: … return declAST;}

  19. ASTs em Java – Exemplo: ParseCommand private DeclarationparseCommand ( ) { Command c1AST = parseSingleCommand( ); while (currentToken.kind == Token.SEMICOLON) { acceptIt( ); Command c2AST = parseSingleCommand( ); c1AST = new SequentialCommand(c1AST, c2AST); } return c1AST;}

  20. ASTs em Java – Exemplo: ParseIdentifier private IdentifierparseIdentifier ( ) { Identifier idAST; if (currentToken.kind == Token.IDENTIFIER) { idAST = new Identifier(currentToken.spelling); currentToken = scanner.scan( ); } else report error return idAST;}

  21. ASTs em Java – Exemplo: Parser public class Parser { private Token currentToken; … public Program parse ( ) { currentToken = scanner.scan( ); Program progAST = parseProgram( ); if (currentToken.kind != Token.EOT) report error return progAST;}

  22. Scanning (Análise léxica) • funciona de forma semelhante ao parser, mas em um maior nível de detalhe. • Especificado usando expressões regulares/EBNF (Gramática léxica) • Símbolos terminais são caracteres

  23. Especificação do Scanner Token ::= Identifier | Integer-Literal | Operator | ; | : | := | ~ | ( | ) | eotIdentifier ::= Letter | Identifier Letter | Identifier DigitInteger-Literal ::= Digit | Integer-Literal DigitOperator ::= + | - | * | / | < | > | = | \Separator ::= Comment | space | eolComment ::= ! Graphic* eol

  24. Especficação do Scanner (2) Token ::= Letter (Letter | Digit)* | Digit Digit* | + | - | * | / | < | > | = | \ | ; | : | := | ~ | ( | ) | eotSeparator ::= ! Graphic* eol | space | eol

  25. Exemplo: Scanner private byte scanToken( ) { switch (currentChar) { case ‘a’: case ‘b’: … case ‘z’: takeIt( ); while (isLetter(currentChar) || isDigit(currentChar)) takeIt( ); return Token.IDENTIFIER; … case ‘\n’ : takeIt( ); return Token.EOL case ‘:’ : takeIt( ); if (currentChar == ‘=’) { takeIt(); return Token.BECOMES; } else return Token.COLON; …

  26. Exemplo: Scanner (2) public class Scanner { private char currentChar = … private byte currentKind; private String currentSpelling; private void take (char expectedChar) {…} private void takeIt ( ) {…} private byte scanToken( ) {…} private void scanSeparator( ) {…}

  27. Exemplo: Scanner (3) public class Scanner { … public Token scan ( ) { while (currentChar == ‘!’ || currentChar == ‘ ’ || currentChar == ‘\n’) scanSeparator( ); currentSpelling = new StringBuffer(“”); currentKind = scanToken( ); return new Token(currentKind, currentSpelling.toString); }}

  28. Exemplo: Token public class Token { public byte kind; public String spelling; public Token (byte kind, String spelling) { this.kind = kind; this.spelling = spelling; if (kind == IDENTIFIER)verifica se é palavra reservada e muda o kind }}

More Related