Anlise Sinttica Parte 1 Subfases Anlise lxica scanning
Análise Sintática – Parte 1 • Subfases: – Análise léxica (scanning): transforma o texto do programa em uma sequëncia de tokens (símbolos como identificadores, literais, operadores, palavras-chave, pontuação etc. – Parsing: verifica a seqüencia de tokens para determinar a estrutura das frases. – Representação da estrutura das frases (árvore sintática abstrata).
Programa e seqüencia de tokens let var y : Integer in ! new year (comentário) y : = y + 1 let Ident. y var Ident. y becomes : = colon : Ident. y Ident. Integer op + in in intlit 1 eot
Programa após parsing (1) Program Single-Command Declaration … Single-Command …
Programa após parsing (2) Declaration Single-Declaration Type-Denoter Ident. let var Ident. colon y : Ident. Integer in in
Programa após parsing (3) Single-Command Expression Primary-Expr. V-name Int-Lit Ident. op V-name Ident. in in Ident. y becomes : = Ident. y op + intlit 1 eot
Abstract Syntax Trees Program Let. Command Assign. Command Var. Declaration Binary. Expr. Simple. V. Vname. Expr. Simple. T. Int. Expr. Simple. V. Ident. y Integer y Ident. Op. Int. Lit. y + 1
Tokens em Java public class Token { public byte kind; public String spelling; public Token (byte kind, String spelling) { this. kind = kind; this. spelling = spelling; } …
Tokens em Java … public final static byte IDENTIFIER = 0, INTLITERAL = 1, OPERATOR = 2, BEGIN = 3, CONST = 4, DO = 5, ELSE = 6, END = 7, IF = 8, IN = 9, LET = 10, THEN = 11, VAR = 12, WHILE = 13, SEMICOLON = 14, COLON = 15, BECOMES = 16, IS = 17, LPAREN = 18, RPAREN = 19, EOT = 20; }
Gramáticas • Uso de EBNF: BNF + expressões regulares: – | , *, ( ) • Exemplos: – Mr |Ms – M (r|s) – ps*t – Ba(na)* – M(r|s)*
BNF estendida Expression : : = primary-Expression (Operator primary-Expression)* primary-Expression : : = Identifier | ( Expression ) Identifier : : = a | b | c | d | e Operator : : = + | - | * | /
Transformações em gramáticas: Fatoração à esquerda • XY|XZ • X (Y | Z)
Fatoração à esquerda: exemplo • Single-command : : = V-name : = expression | if Expression then single-Command else single-Command • Single-command : : = V-name : = expression | if Expression then single-Command ( | else single-Command)
Transformações: Eliminação de recursão à esquerda • N : : = X | N Y • N : : = X (Y)*
Eliminação de recursão à esquerda: exemplo • Identifier : : = Letter | Identifier Digit • Identifier : : = Letter | Identifier (Letter | Digit) • Identifier : : = Letter (Letter | Digit)*
Transformações: substituição de símbolos não terminais • single-Command : : = for Control-Variable : = Expression To-or-Downto Expression do single-Command Control-Variable : : = Identifier To-or-Downto : : = to | downto • single-Command : : = for Identifier : = Expression (to | downto) Expression do single-Command
Parsing • Algorítmos de parsing: são classificados em bottom-up parsing e top-down parsing • Definem a ordem em que a parse tree é construída (na prática ela não é construída de verdade)
Gramática de micro-inglês • Sentence : : = Subject Verb Object. Subject : : = I | a Noun | the Noun Object : : = me | a Noun | the Noun : : = cat | mat | rat Verb : : = like | is | sees
Bottom-up parsing • O parser examina os símbolos terminais da string de entrada, da esquerda para a direita, e reconstrói a árvore sintática de baixo (nós terminais) para cima (em direção ao nó-raiz). • Exemplo: the cat sees a rat.
Bottom-up parsing Sentence Subject Verb Object Noun the cat sees a rat .
Top-down parsing • O parser examina os símbolos terminais da string de entrada, da esquerda para a direita, e reconstrói a árvore sintática de cima (nóraiz) para baixo (em direção aos nós terminais). • Exemplo: the cat sees a rat.
Top-down parsing Sentence Subject Verb Object Noun the cat sees a rat .
Recursive descent parsing • Top-down. • Grupo de N métodos parse. N, um para cada símbolo não-terminal. • Elimine recursão à esquerda e fatorize à esquerda. • Cada método faz o parsing de uma frase-N: private void parse. Noun(); private void parse. Verb(); private void parse. Subject(); private void parse. Object(); private void parse. Sentence();
Parser class Public class Parser { private Terminal. Symbol current. Terminal; private void accept (Terminal. Symbol expected. Terminal) { if (current. Terminal matches expected. Terminal) current. Terminal = next input terminal; else report a syntax error }
parse. Sentence private void parse. Sentence ( ) { parse. Subject( ); parse. Verb( ); parse. Object( ); accept(‘. ’); }
parse. Subject private void parse. Subject ( ) { if (current. Terminal matches ‘I’) accept(‘I’); else if (current. Terminal matches ‘a’) { accept (‘a’); parse. Noun( ); } else if (current. Terminal matches ‘the’) { accept (‘the’); parse. Noun( ); } else report syntax error }
parse. Noun private void parse. Noun ( ) { if (current. Terminal matches ‘cat’) accept(‘cat’); else if (current. Terminal matches ‘mat’) accept (‘mat’); else if (current. Terminal matches ‘rat’) accept (‘rat’); else report syntax error }
parse private void parse ( ) { current. Terminal = first input terminal; parse. Sentence( ); check that no terminal follows the sentence }
- Slides: 27