Skip to content

Instantly share code, notes, and snippets.

@majimenezp
Created December 13, 2011 20:19
Show Gist options
  • Save majimenezp/1473702 to your computer and use it in GitHub Desktop.
Save majimenezp/1473702 to your computer and use it in GitHub Desktop.
Paser para DSL de reglas en ANTLR
grammar DSL;
/*
si datos.mensaje contiene "hidrostatica" o "pnd" o "corrosivas"
entonces devuelve "formato pruebas 01"
sino devuelve "no encontrado"
*/
tokens {
SI='si';
SINO='sino';
CONTIENE='contiene';
ENTONCES='entonces';
DEVUELVE='devuelve';
YADEMAS='y ademas';
OTROMODO='de otro modo'
O='o';
}
regla
: linea (NEWLINE linea)* EOF
;
linea
:(condicional|consecuencia) espacio (qid|DEVUELVE) espacio adicionales*
;
qid : ID ('.' ID)* ;
valor
:'"' .* '"';
adicionales
: preposicion
| espacio
| consecuencia
| valor
| INT
| FLOAT
;
condicional
:SI
|SINO
;
consecuencia
:ENTONCES
|OTROMODO
;
preposicion
:CONTIENE
|YADEMAS
|O
;
espacio :
(' '
| '\\t'
| '\\r' '\\n' { newline(); }
| '\\n' { newline(); }
)
;
palabra :('a'..'z'|'A'..'Z'|'0'..'9')+
;
NEWLINE
: '\u000d'? '\u000a' // DOS/Windows(\r\n) + Unix(\n)
| '\u000d' // Mac OS 9 and earlier(\r)
;
INT : '0'..'9'+
;
FLOAT
: ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
| '.' ('0'..'9')+ EXPONENT?
| ('0'..'9')+ EXPONENT
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
ID : ('_'|'a'..'z'|'A'..'Z') ('_'|'a'..'z'|'A'..'Z'|'0'..'9')* ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment