You are on page 1of 32

Universidade do Minho

An alise e Concep ca o de Software


An alise e Transforma c ao de Software 2012/13

3o Trabalho

Tiago Campos No 22825 Tiago Dias No 22795 February 18, 2013

Contents
1 Parte 1 - ANTLR 1.1 Introdu c ao . . . . . 1.2 Gram atica . . . . . 1.3 C odigo ANTLR . . 1.4 Exemplos Pr aticos 1.4.1 Exemplo 1 1.4.2 Exemplo 2 1.4.3 Exemplo 3 1.4.4 Exemplo 4 2 Parte 2 - TOM 2.1 Introdu c ao . . . . . 2.2 Gramatica ANTLR 2.3 GOM . . . . . . . 2.4 TOM . . . . . . . . 2.5 Build.xml . . . . . 3 Parte 3 - MPS 3.1 Gramatica ANTLR 3.2 GOM . . . . . . . 3.3 Build.xml . . . . . 3.4 Main.t . . . . . . . 4 ANEXOS 4.1 ANEXO 4.2 ANEXO 4.3 ANEXO 4.4 ANEXO 2 2 2 4 7 7 7 7 7 9 9 9 11 12 13 14 14 17 19 20 25 25 27 29 31

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Factorial . . . . . . . . . Compara c ao de n umeros N umeros Reversos . . . . Try-Catch . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . .

. . . . . . . Nova vers ao . . . . . . . . . . . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . .

Nova vers ao . . . . . . . . . . . . . . . . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

1 2 3 4

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

Chapter 1

Parte 1 - ANTLR
1.1 Introdu c ao

O objetivo desta primeira parte e desenvolver uma gramatica em ATLR e apresentar uma representa c ao gen erica para arvore de sintaxe abstracta. A gramatica desenvolvida e na linguagem java. Neste capitulo e apresentado a gram atica desenvolvida, gram atica em ANTLR e exemplos praticos da gramatica desenvolvida.

1.2

Gram atica

Uma gram atica e constituida por um conjunto de simbolos terminais, simbolos n ao terminais, inicial e produ c oes. G e a gram atica. T e o conjunto de simbolos b asicos da linguagem e denem o seu alfabeto. N e o conjunto de simbolos n ao terminais nomeadamente classes (ou variaveis) sint aticas que denem conjuntos de frases. S representa a linguagem denida pela gram atica. P s ao as regras de sintaxe, em que uns simbolos s ao denidos em termos de outros. Uma gramatica G e um quadruplo G = (T, N, S, P) T = INT, PONT, MaM, MeM, IGUAL, DPONT, PVIRG, VIRG, TVAL, APR, FPR, APC, FPC, MENOR, MENORI, MAIOR, MAIORI, MAIS, MENOS, VEZ, DIV, ACH, FCH, DIF, LET, IDENT N = program, declarationClass, varGlobal, constructor, method, statement, varDed, atribuicao, expression, testing expression, numeric expression, logic expression, statement if, testing expresson if, statement for, expressaofor, statement do, statement while, statement case, type, type char, parames, paramesClass, exitStatement, returnStatement, call, typeName, modier S = program P = { program declarationClass* EOF declarationClass public class type char varGlobal* { constructor+ method* } varGlobal ( public | private | protected)* typerName type char (= parames)? ;

constructor modier type char paramesClass* { statement* } method modier typeName type char paramesClass* { statement* } statement expression | statement if | statement for | statement while | statement case | statement try | atribuicao | exitStatement | returnStatement | varDecl | call varDecl typeName type char ( = parames)? ; atribuicao type char = expression; | numeric expression; | type char = type; expression testing expression | numeric expression | logic expression testing expression type ( < | > | <= | >= | == | != ) type numeric expression ( | ++) type | type ( ++ | ) | type ( + | - | * | \ | $) type) logic expression (! type) | (type (&& | and| or| ||) type) statement if if ( testing expression if ) { statement* } | ( elseif ( testing expression if ) statement* | ( else statement* )? testing expression if testing expression ( || testing expression)* (&& testing expression)* statement for for ( expressaofor ) statement* expressaofor int LET = INT; testing expression; LET (MaM|MeM | - INT | +INT) statement while while ( testing expression ) statement* | do statement* while ( testing expression ) statement case switch ( type char ) (case(type) : statement* break;)+ (default : statement ; )?

statement try try statement* catch ( LET LET? ( , (LET LET?))* ) statement* )* ( nally statement* )? type IDENT|LET|INT type char IDENT|LET parames type char ( , type char )* paramesClass typeName type char ( , typeName type char )*

exitStatement exit when expression; returnStatement return type; call call type char ( parames* ) ; typeName int | boolean | char | String | void modier public | private | protected | static | nal | sychronized }

1.3

C odigo ANTLR

De seguida e apresentado o codigo da gram atica ANLTR grammar Ultima versao; program : declarationClass* EOF; declarationClass : public class(IDENT|LET)ACH varGlobal* main class? constructor? (method)* FCH; varGlobal : (public|private|protected)* typeName(IDENT|LET) (TVAL (INT|IDENT))? PVIRG ; main class : public static void main APC String APR FPR args FPC ACH statement* FCH; method : modier typeName(IDENT|LET)APC paramesClass* FPC ACH (statement)* FCH; statement :expression|statement if|statement while|statement case|statement for|statement try|atribuicao |exitStatement|returnStatement|varDecl|arrayStatement|call; expression : testing expression | numeric expression| logic expression | array expression; array expression: (IDENT|LET)APR type FPR = ( type | type(+|-|*|/) type ) | type TVAL(IDENT|LET)APR type FPR PVIRG testing expression : type(MAIOR|MENOR|MAIORI|MENORI|IGUAL|DIF) type; numeric expression : (|++)type | type(++) | type(MAIS | MENOS | VEZ | DIV| %) (type | numeric expression ) ; logic expression: ! type| type(&&| and| or| | | )type; testing expression if: testing expression ((|| testing expression)* (&& testing expression)*) ; statement if : if APC (testing expression if) FPC ACH ( statement )* FCH (elseif APC testing expression if FPC ACH (statement)* FCH)*(else ACH (statement)* FCH)?; expressaofor : int LET TVAL (INT|APC expression FPC) PVIRG testing expression PVIRG LET (MaM| MeM | MENOS INT| MAIS INT) ; statement for: for APC expressaofor FPC ACH (statement)*FCH | forAPC typeName IDENT : IDENT FPC ACH(statement)* FCH ;

statement while: whileAPC testing expression FPC ACH (statement)* FCH | doACH (statement)* FCHwhileAPC testing expression FPC PVIRG; statement case : switchAPC (IDENTLET) FPC ACH (case(type):(statement)* breakPVIRG)+ (default : statement)? FCH; statement try : try ACH (statement)* FCH (catch APC (LET LET?)(VIRG (LET LET?))* FPC ACH (statement)* FCH)* (nally ACH (statement)* FCH)? ; exitStatement : exit when expression PVIRG; arrayStatement:(public|private|protected)*typeName APR FPR (IDENT|LET) TVAL new typeName APR INT FPR PVIRG; returnStatement : return type PVIRG ; call : call(IDENT|LET) APC parames* FPC PVIRG ; varDecl : typeName (IDENT| LET) (TVAL type)? PVIRG ; atribuicao: (IDENT| LET)= ( (expression| type) | type ) PVIRG | numeric expression PVIRG; constructor : modier (IDENT| LET)APC paramesClass* FPC ACH (statement)* FCH ; type : IDENT | LET | INT; parames : (IDENT| LET) (VIRG (IDENT | LET))* ; paramesClass : typeName(IDENT| LET)(VIRG typeName(IDENT| LET))*; typeName :int | boolean | char | String | void ; modier :public | private | protected | static | nal | sychronized ; INT :(0..9)+; PONT : . ; MaM :++; MeM :; IGUAL : == ;

DPONT : : ; PVIRG : ; ; VIRG : , ; TVAL : = ; APR : [ ; FPR : ] ; APC : ( ; FPC : ) ; DIGUAL : /= ; MENOR : < ; MENORI : <= ; MAIOR : > ; MAIORI : >= ; MAIS : + ; MENOS : - ; VEZ : * ; DIV : / ; ACH : { ; FCH : } ; DIF : !=; LET : (a..z | A..Z)+ ; IDENT : (a..z | A..Z | ) (a..z | A..Z | 0..9 | )* ;

1.4
1.4.1

Exemplos Pr aticos
Exemplo 1 - Factorial

public class NumberFactorial { public static void main(String[] args) { int number = 5; int factorial = number; for(int i =(number - 1); i > 1; i) { factorial = factorial * i; } } } Ver esquema da arvore no anexo 1.

1.4.2

Exemplo 2 - Compara c ao de n umeros

public static void main(String[] args) { int i = 0; if(i == 0) { return true; } else { return false; } } } Ver esquema da arvore no anexo 2.

1.4.3

Exemplo 3 - N umeros Reversos

public class JavaApplication2 { public static void main(String[] args) { int number = 1234; int reversedNumber = 0; int temp = 0; while(number > 0) { temp = number% 10; reversedNumber = reversedNumber * 10 + temp; number = number/10; } } } Ver esquema da arvore no anexo 3.

1.4.4

Exemplo 4 - Try-Catch

public class XPTO { int idade;

int altura; public XPTO() { idade = 0 ; altura = 0; } public int retorna() { int d; int a; try { d = 0; a = 42 / d; } catch (ArithmeticException e) { return 0; } nally { return 1; } }} Ver esquema da arvore no anexo 4.

Chapter 2

Parte 2 - TOM
2.1 Introdu c ao

Este trabalho pr atico vem no seguimento do trabalho realizado anteriormente para a gram atica do i. Este trabalho tem como objetivo combinar as ferramentas Tom, Gom e antlr recorrendo para isso ao programa Eclipse. Ao longo do trabalho e apresentado o c odigo do trabalho em quest ao, representando a gramatica Antlr, o cheiro Gom, Tom e o XML.

2.2

Gramatica ANTLR - Nova vers ao

No c odigo seguinte est a representada a gramatica i, contendo todas as fun c oes e express oes que comp oem a gramatica antlr. As assinaturas de cada uma das express oes s ao enviadas para o cheiro Gom, onde est a denida a sintaxe da arvore da gramatica.

10

2.3

GOM

O c odigo seguinte representa o cheiro Gom. O Gom tem como fun c ao facultar uma arvore na implementa c ao Java, que corresponde a uma especica c ao alg ebrica. O Gom gera um c odigo eciente que garante uma partilha m axima, fornece uma sintaxe para denir de forma concisa a sintaxe da arvore. Cada m odulo do Gom dene os operadores e os seus tipos.

11

2.4

TOM

O Tom e destinado a manipular estruturas de arvores e documentos XML, este e usado em ambientes Java. O tom apenas se limita a imprimir a arvore produzida pelo Gom.

12

2.5

Build.xml

13

Chapter 3

Parte 3 - MPS
3.1 Gramatica ANTLR - Nova vers ao

No c odigo seguinte est a representada a gramatica i, contendo todas as fun c oes e express oes que comp oem a gramatica antlr. As assinaturas de cada uma das express oes s ao enviadas para o cheiro Gom, onde est a denida a sintaxe da arvore da gramatica. Mais uma vez a gr amatica sofreu altera c oes, pois na sua vers ao anterior havia algumas inconsistencias. Desta forma, as seguintes imagens represetam a nova gramatica ANTLR:

14

15

16

3.2

GOM

O c odigo seguinte representa o cheiro Gom. O Gom tem como fun c ao facultar uma arvore na implementa c ao Java, que corresponde a uma especica c ao alg ebrica. O Gom gera um c odigo eciente que garante uma partilha m axima, fornece uma sintaxe para denir de forma concisa a sintaxe da arvore. Cada m odulo do Gom dene os operadores e os seus tipos.

17

18

3.3

Build.xml

O Build.xml e um arquivo XML e tem como objetivo de descrever as tarefas de forma o ANT intreprete e as execute.

19

3.4

Main.t

20

21

22

23

24

Chapter 4

ANEXOS

4.1

ANEXO 1

25

26

4.2

ANEXO 2

27

28

4.3

ANEXO 3

29

30

4.4

ANEXO 4

31

You might also like