/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ grammar Gramatica; programa : instrs ; instrs : instr+ ; instr : ifInstruction | whileInstruction | forInstruction | atr ';' @nl | io ';' @nl ; io : 'print' ID | 'input' ID ; ifInstruction : 'if' '(' cond ')' '{' @nlt instrs '}' elseCondition? ; elseCondition : 'else' '{' instrs '}' ; whileInstruction : 'while' '(' cond ')' '{' instrs '}' ; forInstruction : 'for' '(' atr ';' cond ';' atr ')' '{' instrs '}' ; atr : ID '=' exp ; exp : termo | exp opA termo ; opA : '+' | '-' ; termo : fator | termo opM=('*'|'/') fator ; fator : ID | '-'? INT ; cond : comp | '(' cond ')' | cond opL=('&&'|'||') cond ; comp : exp op exp ; op : '>' | '<' | '<' '=' | '>' '=' | '=' '=' | '!' '=' ; /*--------------- Lexer ---------------------------------------*/ NOME: STRING ; ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-')* ; INT : '0'..'9'+ ; WS : [ \t\r\n] -> skip ; fragment STRING : '"' ( ESC_SEQ | ~('"') )* '"' ; fragment ESC_SEQ : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') | UNICODE_ESC | OCTAL_ESC ; fragment OCTAL_ESC : '\\' ('0'..'3') ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ; fragment UNICODE_ESC : '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT ; fragment HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;