PARSER_BEGIN(ChordsParser)
package chords.parser;
import chords.Programa;
import chords.expression.*;
public class ChordsParser {
public static void main(String[] args) {
ChordsParser parser = new ChordsParser(System.in);
try {
Programa programa = parser.Input();
if (!programa.isValid()) {
System.out.println("Acorde inválido!");
} else {
programa.executar();
}
} catch (ParseException e) {
System.out.println("Chords Parser Version 0.0.1: Encountered errors during parse.");
}
}
}
PARSER_END(ChordsParser)
SKIP : /* WHITE SPACE */
{
" "
| "\t"
| "\n"
| "\r"
| "\f"
}
TOKEN : /* TOKENS DE NOTAS */
{
< NOTA: "C" | "D" | "E" | "F" | "G" | "A" | "B" >
}
TOKEN : /* TOKENS DE ACIDENTES */
{
< SUSTENIDO: "#" >
| < BEMOL: "b" >
}
TOKEN: /* TOKENS DE TRIADES */
{
< NTH: "2" | "4" | "6" | "9" | "11" | "13" >
| < MENOR: "m" | "-" >
| < DIM: "m(b5)" | "-(b5)" | "-(-5)" >
| < AUMENTADA: "(#5)" | "+" >
}
TOKEN: /* TOKENS DE TETRATES */
{
< SETIMAMAIOR: "^" | "^7" | "7M" >
| < SETIMAMENOR: "7" | "-" >
| < SETIMADIM: "o" >
}
Programa Input() :
{
Programa retorno;
}
{
retorno = PPrograma() <EOF>
{
return retorno;
}
}
Programa PPrograma() :
{
Nota retorno;
}
{
retorno = PAcorde()
{
return new Programa(retorno);
}
}
Nota PAcorde() :
{
Nota nota;
Token token;
}
{
nota = PVariacao()
[
(token = <SETIMAMENOR>
{
nota = new Exp7Menor(nota, token.toString());
})
|(token = <SETIMAMAIOR>
{
nota = new Exp7Maior(nota, token.toString());
})
|(token = <SETIMADIM>
{
nota = new Exp7Dim(nota, token.toString());
})
]
[
(token = <NTH>
{
nota = new ExpNth(nota, token.toString());
})
]
{
return nota;
}
}
Nota PVariacao() :
{
Nota nota;
}
{
nota = PExpAcidente()
[
<MENOR>
{
nota = new ExpMenor(nota);
}|
<DIM>
{
nota = new ExpDim(nota);
}|
<AUMENTADA>
{
nota = new ExpAumentada(nota);
}
]
{
return nota;
}
}
Nota PExpAcidente() :
{
Nota nota;
}
{
nota = PExpNota()
[<SUSTENIDO>
{
nota = new ExpSustenido(nota);
}|
<BEMOL>
{
nota = new ExpBemol(nota);
}]
{
return nota;
}
}
Nota PExpNota() :
{
Token token;
}
{
token = <NOTA>
{
return new ExpNota(token.toString());
}
}