1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| PARSER_BEGIN(SyntaxChecker)
public class SyntaxChecker {
public static void main(String[] args) {
try {
new SyntaxChecker(new java.io.StringReader(args[0])).programme();
System.out.println("Syntax is okay");
} catch (Throwable e) {
// Catching Throwable is ugly but JavaCC throws Error objects!
System.out.println("Syntax check failed: " + e.getMessage());
}
}
}
PARSER_END(SyntaxChecker)
SKIP: { "\t" | "\n" | "\r" }
TOKEN: { "+" |
<INT: (["0"-"9"])+> |
<LOG: ("TRUE"|"FALSE")> |
<TYPE_LOGICAL:"logical"> | <TYPE_INTEGER:"integer"> |
<SET:"set"> | <GET:"get"> |
<ID: (["a"-"z"])+>
}
/* Struture générale du programme */
void programme() : {} { (instruction())+ <EOF> }
void instruction(): {} { declaration() | appelMethod() }
/* Déclaration des variables */
void declaration(): {} { type() " " identifiant() }
void identifiant(): {} { <ID> }
void type() : {} { LOOKAHEAD(3) typeRepete() | typeBasic()}
void typeBasic() : {} { <TYPE_LOGICAL> | <TYPE_INTEGER> }
void typeRepete() : {} { typeBasic() "[" dimension() "]" }
void dimension() : {} { <INT> ("," <INT>)* } /* Integer only */
/* Methodes get et set, avec possibilité d'inclure des additions */
void appelMethod() :{} { identifiant() | methods() }
void methods() :{} {LOOKAHEAD(2) set() | get()}
void set() :{}{ "." <SET> "(" expression() ")" | "<-" expression()}
void expression() :{}{ <INT> ("+" <INT>)* | <LOG> }
void get() :{}{ "." <GET> "()" } |
Partager