Created
November 9, 2017 16:52
-
-
Save ManuelBlanc/da631744d6407ca5a63811d89c326f99 to your computer and use it in GitHub Desktop.
Sintaxis de ALFA en formato ANTLR
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
grammar ALFA; | |
programa | |
: 'main' '{' declaraciones funciones sentencias '}' | |
; | |
declaraciones | |
: declaracion | |
| declaracion declaraciones | |
; | |
declaracion | |
: clase identificadores ';' | |
; | |
clase : clase_escalar | |
| clase_vector | |
; | |
clase_escalar : tipo | |
; | |
tipo : 'int' | 'boolean' | |
; | |
clase_vector : 'array' tipo constante_entera? | |
; | |
identificadores | |
: identificador | |
| identificador ',' identificadores | |
; | |
funciones : funcion funciones | |
| | |
; | |
funcion : 'function' tipo identificador '(' parametros_funcion ')' '{' declaraciones_funcion sentencias '}' | |
; | |
parametros_funcion | |
: parametro_funcion resto_parametros_funcion | |
| | |
; | |
resto_parametros_funcion | |
: ';' parametro_funcion resto_parametros_funcion | |
| | |
; | |
parametro_funcion | |
: tipo identificador | |
; | |
declaraciones_funcion | |
: declaraciones | |
| | |
; | |
sentencias | |
: sentencia | |
| sentencia sentencias | |
; | |
sentencia | |
: sentencia_simple> ';' | |
| bloque | |
; | |
sentencia_simple | |
: asignacion | |
| lectura | |
| escritura | |
| retorno_funcion | |
; | |
bloque : condicional | |
| bucle | |
; | |
asignacion | |
: identificador '=' exp | |
| elemento_vector '=' exp | |
; | |
elemento_vector | |
: identificador '[' exp ']' | |
; | |
condicional | |
: 'if' '(' exp ')' '{' sentencias '}' | |
| 'if' '(' exp ')' '{' sentencias '}' 'else' '{' sentencias '}' | |
; | |
bucle : 'while' '(' exp ')' '{' sentencias '}' | |
; | |
lectura : 'scanf' identificador | |
; | |
escritura | |
: 'printf' exp | |
; | |
retorno_funcion | |
: 'return' exp | |
; | |
exp : = exp '+' exp | |
| exp '-' exp | |
| exp '/' exp | |
| exp '*' exp | |
| '-' exp | |
| exp '&&' exp | |
| exp '||' exp | |
| '!' exp | |
| identificador | |
| constante | |
| '(' exp ')' | |
| '(' comparacion ')' | |
| elemento_vector | |
| identificador '(' lista_expresiones ')' | |
; | |
lista_expresiones | |
: exp resto_lista_expresiones | |
| | |
; | |
resto_lista_expresiones | |
: ',' exp resto_lista_expresiones | |
; | |
comparacion | |
: exp '==' exp | |
| exp '!=' exp | |
| exp '<=' exp | |
| exp '>=' exp | |
| exp '<' exp | |
| exp '>' exp | |
; | |
constante | |
: constante_logica | |
| constante_entera | |
; | |
constante_logica | |
: 'true' | |
| 'false' | |
; | |
constante_entera | |
: numero | |
; | |
numero : digito | |
| numero digito | |
; | |
identificador | |
: letra | |
| letra cola_identificador | |
; | |
cola_identificador | |
: alfanumerico | |
| alfanumerico cola_identificador | |
; | |
alfanumerico | |
: letra | |
| digito | |
; | |
letra : ('a'..'z'|'A'..'Z') | |
digito : '0'..'9' | |
COMMENT | |
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} | |
; | |
WS : ( ' ' | |
| '\t' | |
| '\r' | |
| '\n' | |
) {$channel=HIDDEN;} | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment