Friday, November 10, 2006

Grammar DUDU+ mi lenguaje de programación

Este es el grammar del lenguaje DUDU+, este grammar (BNF production) esta hecho para la generación de un parser con la herramienta javacc. Como se ve la sintaxis es la de java pero con otras reglas, ya que se trata de javacc, que es el generador de parser más popular, se puede encontrar documentación de este en la página: https://javacc.dev.java.net/ . Les recomiendo que utilicen el mailling list en el cual hay personas dispuestas a ayudarte :-D.

/*
* DUDU+ codename MiniJava
* HECHO... in Dominican Republic
* author VГ­ctor Sosa <victornsosa@gmail.com> vns java
*/


options{
LOOKAHEAD = 1;
STATIC = false;
JAVA_UNICODE_ESCAPE = true;
}

PARSER_BEGIN(MiniJava)

import java.util.*;

public class MiniJava {

public static void main(String[] args) throws ParseException, TokenMgrError {

MiniJava parser;
String file = null;
long time = 0;
long parseTime = 0;
long startTime = 0;

if (args.length == 0)
{
System.out.println("Error messages...");
return;

} else if ( args.length == 1 ){
file = args[0];
System.out.println("Start parsing...");

try
{
parser = new MiniJava(new java.io.FileInputStream(file));

} catch ( java.io.FileNotFoundException e ) {

System.out.println("Parser error: Filename " + file + " not found." );
return;
}
} else {
System.out.println("Debe escribir: java MiniJava inputfile");
return;
}
try
{
startTime = System.currentTimeMillis();
parser.Start();
parseTime = System.currentTimeMillis();
time = parseTime - startTime;

System.out.println(" Time of parsing: " + time + " ms");
System.out.println(" DuDu successfully end");

} catch ( ParseException pex) {

System.out.println( pex.getMessage() );
System.out.println(" ParserException during parse file" );
}
}/* main method*/
}

PARSER_END(MiniJava)

/* Eat white space and comment*/
SKIP : {
" "
| "\t"
| "\n"
| "\r"
| <"//" (~["\n","\r"])* ("\n" | "\r" | "\r\n")>
| <"/*" (~["*"])* "*" ("*" | ~["*","/"] (~["*"])* "*")* "/">
}

TOKEN : /* RESERVED WORDS AND LITERALS */
{
< BOOLEAN: "boolean" >
| < BYTE: "byte" >
| < CHAR: "char" >
| < CLASS: "class" >
| < DOUBLE: "double" >
| < FALSE: "false" >
| < FLOAT: "float" >
| < FINAL: "final" >
| < INT: "int" >
| < LONG: "long" >
| < PUBLIC: "public" >
| < SHORT: "short" >
| < STATIC: "static" >
| < TRUE: "true" >
| < VOID: "void" >
| < NEW: "new" >
| < MAKE: "make" >
| < STACK: "stack" >
| < REPEAT: "repeat" >
| < UNTIL: "until" >
| < ADDSTACK: "addStack" >
| < ORDSTACK: "ordStack" >
| < ASCENDENTE: "ascendente" >
| < DESCENDENTE: "descendente" >

}

TOKEN : /* SEPARATORS */
{
< LPAREN: "(" >
| < RPAREN: ")" >
| < LBRACE: "{" >
| < RBRACE: "}" >
| < LBRACKET: "[" >
| < RBRACKET: "]" >
| < SEMICOLON: ";" >
| < COMMA: "," >
}

TOKEN : /* OPERATORS */
{
< ASSIGN: "=" >
| < INCR: "++" >
| < DECR: "--" >
| < PLUS: "+" >
| < MINUS: "-" >
| < STAR: "*" >
| < SLASH: "/" >

}

TOKEN : /* LITERALS */
{
< INTEGER_LITERAL:
< DECIMAL_LITERAL > (["l","L"])?
>
|
< #DECIMAL_LITERAL: ["0"-"9"] (["0"-"9"])* >
|
< FLOATING_POINT_LITERAL:
(["0"-"9"])+ "." (["0"-"9"])* ( < EXPONENT > )? (["f","F","d","D"])?
| "." (["0"-"9"])+ ( < EXPONENT >)? (["f","F","d","D"])?
| (["0"-"9"])+ < EXPONENT > (["f","F","d","D"])?
| (["0"-"9"])+ (< EXPONENT >)? ["f","F","d","D"]
>
|
< #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
|
< CHARACTER_LITERAL:
"'"
( (~["'","\\","\n","\r"])
| ("\\"
( ["n","t","b","r","f","\\","'","\""]
| ["0"-"7"] ( ["0"-"7"] )?
| ["0"-"3"] ["0"-"7"] ["0"-"7"]
)
)
)
"'"
>
|
< STRING_LITERAL:
"\""
( (~["\"","\\","\n","\r"])
| ("\\"
( ["n","t","b","r","f","\\","'","\""]
| ["0"-"7"] ( ["0"-"7"] )?
| ["0"-"3"] ["0"-"7"] ["0"-"7"]
)
)
)*
"\""
>
}

TOKEN : {
<IDENTIFIER: < LETTER > ( < LETTER > | < DIGIT > )*>
| <#LETTER: ["$","A"-"Z","_","a"-"z"]>
| <#DIGIT: ["0"-"9"]>
}


/*********************************************
* THE MINIJAVA LANGUAGE GRAMMAR STARTS HERE *
*********************************************/

/*
* Struts.
*/

void Start() throws ParseException :
{}
{
(TypeDeclaration() )*
}


void TypeDeclaration() :
{}
{
ClassDeclaration()
}

/*
* Declaracion de clases
*/
void ClassDeclaration() :
{}
{
( "final" | "public" | "static" )* "class" < IDENTIFIER >
"{" ( ClassBodyDeclaration() )* "}"

}

void ClassBodyDeclaration() :
{}
{
LOOKAHEAD(2)
FieldDeclaration()
|
Statement()
}

void FieldDeclaration() :
{}
{
( "public" | "static" | "final" )*
Type() VariableDeclarator() ( "," VariableDeclarator() )* ";"

}

void VariableDeclarator() :
{}
{
VariableDeclaratorId() [ "=" VariableInitializer() ]
}

void VariableDeclaratorId() :
{}
{
< IDENTIFIER > ( "[" "]" )*
}

void VariableInitializer() :
{}
{
ArrayInitializer()
|
Expression()

}

void ArrayInitializer() :
{}
{
"{" [ VariableInitializer() ( LOOKAHEAD(2) "," VariableInitializer() )* ] [ "," ] "}"
}

/*
* Tipos, nombre y sentencias
*/
void Type() :
{}
{
PrimitiveType() ( "[" "]" )*
}

void PrimitiveType() :
{}
{
"boolean"
|
"char"
|
"byte"
|
"short"
|
"int"
|
"long"
|
"float"
|
"double"
}

void Name() :
{ }
{
< IDENTIFIER >
}

/*
* Expression syntax
*/

void Expression() :
{}
{
ConditionalExpression() [ AssignmentOperator() AdditiveExpression() ]
}

void AssignmentOperator() :
{}
{
"="
}

void ConditionalExpression() :
{}
{
ConditionalOrExpression() [ "?" Expression() ":" ConditionalExpression() ]
}

void ConditionalOrExpression() :
{}
{
ConditionalAndExpression() ( "||" ConditionalAndExpression() )*

}

void ConditionalAndExpression() :
{}
{
EqualityExpression() ( "&&" EqualityExpression() )*

}

void EqualityExpression() :
{}
{
RelationalExpression() ( ( "==" | "!=" ) RelationalExpression() )*

}

void RelationalExpression() :
{}
{
AdditiveExpression() ( ( "<" | ">" | "<=" | ">=" ) AdditiveExpression() )*

}

void AdditiveExpression() :
{}
{
MultiplicativeExpression() ( LOOKAHEAD(2)( "+" | "-" ) MultiplicativeExpression() )*

}

void MultiplicativeExpression() :
{}
{
UnaryExpression() ( ( "*" | "/" ) UnaryExpression() )*

}

void UnaryExpression() :
{}
{
( "+" | "-" ) UnaryExpression()
|
UnaryExpressionNotPlusMinus()
}

void UnaryExpressionNotPlusMinus() :
{}
{
PostfixExpression()
}

void PostfixExpression() :
{}
{
PrimaryExpression() [ "++" | "--" ]
}

void PrimaryExpression() :
{}
{
PrimaryPrefix() ( PrimarySuffix() )*
}

void PrimaryPrefix() :
{}
{
Literal()
|
"(" Expression() ")"
|
AllocationExpression()
|
Name()
}

void PrimarySuffix() :
{}
{
"[" Expression() "]"
|
LOOKAHEAD(2)
"." "addStack" "(" ( Name() | Literal() ) ")"
|
"." "ordStack" "(" OrdenType() ")"

}

void OrdenType() :
{}
{
"ascendente"
|
"descendente"
}

void Literal() :
{}
{
< INTEGER_LITERAL >
|
< FLOATING_POINT_LITERAL >
|
< CHARACTER_LITERAL >
|
< STRING_LITERAL >
|
BooleanLiteral()
}

void BooleanLiteral() :
{}
{
"true"
|
"false"
}

void AllocationExpression() :
{}
{
"new" PrimitiveType() ArrayDimsAndInits()
|
MakeStack()
}

void MakeStack() :
{}
{
"make" "stack"
}

void ArrayDimsAndInits() :
{}
{
LOOKAHEAD(2)
( LOOKAHEAD(2) "[" Expression() "]" )+ ( LOOKAHEAD(2) "[" "]" )*
|
( "[" "]" )+ ArrayInitializer()
}

void Statement() :
{}
{
Expression() ";"
|
RepeatStatement()

}

void RepeatStatement() :
{}
{
"repeat" Statement() "until" "(" Expression() ")"

}

Sunday, November 05, 2006

Stevey en el pais de las maravillas (Google)

Interesante artculo de Stevey para decirnos que vivimos en un mundo de mentira :) que sincero, y de como se debe trabajar segun Google.

Los 10 lenguajes más demandados

En este artículo se confirma mas aun lo que dije anteriormente, para mis amigos que todavia estan pensando que M$ domina el negocio de las computadoras no significa que contole en que se programa.