Autor Beitrag
Macoy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: So 04.12.05 14:20 
Hallo, mein Programm muss Code, der in einer bestimmten Skriptsprache geschrieben wurde, in programminterne Objekte umwandeln, um diese dann in einem Baum dazustellen.

Kennt jemand einen guten Algorithmus zum Entflechten des Codes ?

So sieht der Input aus:

ausblenden volle Höhe Quelltext
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:
# Data for our solar system.

"Mercury" "Sol"
{
  Texture "mercury.*"
  BumpMap "mercurybump.*"
  BumpHeight 2.5
  Radius 2440

  CustomOrbit "vsop87-mercury"
  EllipticalOrbit {
  Period            0.2408
  SemiMajorAxis     0.3871
  Eccentricity      0.2056
  Inclination       7.0049
  AscendingNode    48.33167
  LongOfPericenter 77.456
        MeanLongitude   252.251
  }

  RotationPeriod 1407.509405
  Obliquity         7.01
  EquatorAscendingNode 48.42
  RotationOffset 291.20

  Albedo            0.06
}

AltSurface "limit of knowledge" "Sol/Mercury"
{
  Texture "mercury.*"
  OverlayTexture "mercury-lok-mask.png"
  BumpMap "mercurybump.*"
  BumpHeight 2.5
}


Nach Entfernen alles Leerzeilen und Kommentaren tokenisiere ich den Code, wobei ich das Linebreak Zeichen als Divider nehme. Raus kommt das:

ausblenden Quelltext
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:
0. Block : "Mercury" "Sol"
1. Block : {
2. Block :   Texture "mercury.*"
3. Block :   BumpMap "mercurybump.*"
4. Block :   BumpHeight 2.5
5. Block :   Radius 2440
6. Block :   CustomOrbit "vsop87-mercury"
7. Block :   EllipticalOrbit {
8. Block :   Period            0.2408
9. Block :   SemiMajorAxis     0.3871
10. Block :   Eccentricity      0.2056
11. Block :   Inclination       7.0049
12. Block :   AscendingNode    48.33167
13. Block :   LongOfPericenter 77.456
14. Block :         MeanLongitude   252.251
15. Block :   }
16. Block :   RotationPeriod 1407.509405
17. Block :   Obliquity         7.01
18. Block :   EquatorAscendingNode 48.42
19. Block :   RotationOffset 291.20
20. Block :   Albedo            0.06
21. Block : }
22. Block : AltSurface "limit of knowledge" "Sol/Mercury"
23. Block : {
24. Block :   Texture "mercury.*"
25. Block :   OverlayTexture "mercury-lok-mask.png"
26. Block :   BumpMap "mercurybump.*"
27. Block :   BumpHeight 2.5
28. Block : }


Der Code zwischen den öffnenden und schliessenden Klammern stellt jeweils ein Objekt in meiner Datenstruktur dar, nur habe ich keine Idee, wie ich den strukturiert auflösen kann.

Hat jemand ideen ?
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 04.12.05 14:43 
Den Code würde ich in weitere Tokens unterteilen, sodass es du in folgender Form hast. Nach der Unterteilung kannst du auch gerade die "-Zeichen weglassen:

Text:Mercury | Text:Sol | Steuerzeichen:{ | Text:Texture | Text:mercury.* | Text:BumpMap | Text:mercurybump.* | Text:BumpHeight | Zahl:2.5 | Text:Radius | Zahl:2440 | ...

Solche Daten können von rekursiven Parsern relativ einfach ausgewertet werden.

Du muss zuerst eine Grammatik definieren, irgend so in der Art:
Objekt => Text [Text] Text "{" { ObjektDefinition } "}"
ObjektDefinition => ( Text ( Text | Zahl ) ) | UnterObjekt
UnterObjekt => Text "{" ObjektDefinition "}"

[] bedeutet optional
{} bedeutet beliebig viele Male

Natürlich kannst du es dir auch einfacher machen, dafür ist das ganze dann aber vermutlich weniger tolerant und es liegt keine klare Definition der Sprache zu Grunde.

Mein Mathe-Parser funktioniert auf diesem Prinzip, wobei der Code aber nicht zuerst in Tokens unterteilt werden sondern alles bis zum letzten Charakter in der Sprache definiert ist.
Macoy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: So 04.12.05 14:53 
Den SSC Scripts liegt auch eine klare Definition zugrunde. Leider werden nicht alle Statements mit ";" abgeschlossen, das wäre um einiges einfacher. Ein Linebreak kann ja sonstwas bedeuten.

Das mit Grammatik ist ne gute Idee. Ich hab das im Studium mal als "Becker Nauer Form" oder BNF Dictionary gelernt. Mal schauen. Vielen Dank jedenfalls für den Hinweis.