Bonjour,
Je suis en train de construire un mini langage (ultra simple, c'est juste pour essayer), un truc qui pourra faire :
Code:
1
2
3
4
5
6 integer toto toto.set(3+2) toto.get() logical titi titi.set(TRUE) titi.get()
J'ai défini une grammaire en utilisant javacc (code si-dessous) et je vise au final un compilateur vers java. A priori, ca marche pas trop mal, les expressions sont reconnues :
Code:
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> "()" }
Par contre, je ne sais pas trop quoi faire ensuite... Une fonction de typage ? Un compilateur ? Ca prend quelle forme ? Bref, si vous avez des conseils ou des liens de doc, je suis preneur.
Merci !
Christophe
