Created
May 18, 2012 17:18
-
-
Save heat/2726513 to your computer and use it in GitHub Desktop.
Korg Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
versao aceita definicao de variaveis; | |
- atribuicao a variavel | |
definiçoes de funcoes | |
- corpo da funcao | |
- retorno da funcao | |
expressoes literais | |
- operadores | |
- parenteses de operacao | |
- podem ser funcao | |
Comparacao | |
- comparadores | |
- concatenacoa de comparacao | |
- comparacao em expressao | |
*/ | |
PARSER_BEGIN(NewKorg) | |
public class NewKorg { | |
/** Main entry point. */ | |
public static void main(String args[]) throws ParseException { | |
try { | |
NewKorg parser = new NewKorg(System.in); | |
parser.Input(); | |
} | |
catch(Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
PARSER_END(NewKorg) | |
SKIP : | |
{ | |
" " | |
| "\t" | |
| "\n" | |
| "\r" | |
} | |
TOKEN : /* SEPARATORS */ | |
{ | |
< LPAREN: "(" > | |
| < RPAREN: ")" > | |
| < LBRACE: "{" > | |
| < RBRACE: "}" > | |
| < LBRACKET: "[" > | |
| < RBRACKET: "]" > | |
| < SEMICOLON: ";" > | |
| < DOUBLEDOT: ":" > | |
| <ATT: "="> | |
| <COMP: ("eq"|"ne"|"gt"|"lt")> | |
| <CATCOMP: ("or"|"and")> | |
} | |
TOKEN : | |
{ | |
<VAR: "var" > | |
| <DEF: "def" > | |
| <DIF: "if"> | |
| <SET: "set" > | |
| <DELSE: "else"> | |
| <RETURN: "return"> | |
| <ID: (["a"-"z"])+ > | |
| <NUM:(["0"-"9"])+("."(["0"-"9"])+)?> | |
| <OPERATOR: ["*","-","+","/"]> | |
} | |
/** Root production. */ | |
void Input() : | |
{} | |
{ | |
(DefVar())* | |
(Functions())* | |
(Statement())*<EOF> | |
} | |
void Statement() : | |
{} | |
{ | |
<SET> <ID> <ATT> Expression() <SEMICOLON> | |
| IfThenStatement() | |
| Expression()<SEMICOLON> | |
} | |
void IfThenStatement() : | |
{} | |
{ | |
<DIF> "(" BooleanExpression() ")"<LBRACE>(Statement())*<RBRACE>(<DELSE><LBRACE>(Statement())*<RBRACE>)? | |
} | |
void BooleanExpression(): | |
{} | |
{ | |
Expression() <COMP> Expression() (<CATCOMP> BooleanExpression())* | |
} | |
void Expression() : | |
{} | |
{ | |
"("BooleanExpression()")""?" Expression() "|" Expression() | |
|Term() (<OPERATOR> Term())* | |
} | |
void Term() : | |
{} | |
{ | |
<ID> ("(" (Expression())* ")")? | |
| <NUM> | |
| "(" Expression() ")" | |
} | |
void Functions() : | |
{} | |
{ | |
<DEF> <ID> <LPAREN> (Parameters())? <RPAREN><LBRACE>FunctionBody()<RBRACE> | |
} | |
void FunctionBody() : | |
{} | |
{ | |
(DefVar())* (Statement())* <RETURN> Expression() <SEMICOLON> | |
} | |
void Parameters() : | |
{} | |
{ | |
<ID> ("," <ID>)* | |
} | |
void DefVar(): | |
{} | |
{ | |
<VAR> <ID> (<ATT> Expression())? <SEMICOLON> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment