Guest User

Untitled

a guest
Jul 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. (** TP2 : Analyse Syntaxique Descendante
  2. Objectif : Realiser un analyseur syntaxique pour un certain langage.
  3. Le code doit etre aisement modifiable et donc documente.
  4.  
  5. @author BASSAND Guillaume
  6. @author NGouye Ndiaye
  7. *)
  8.  
  9. (* Creation d'un analyseur syntaxique *)
  10.  
  11. open Analex;;
  12. #load "analex.cma";;
  13.  
  14. (** Définition des unités lexicales telles que définies dans l'analyseur lexical *)
  15. (*type ul = Ident of string | Operateur of string | ParentheseG | ParentheseD | ET | OU | EoF | ERREUR of string ;;*)
  16. UL_IDENT "ident";;
  17.  
  18. (** Definition des non-terminaux, cf la grammaire LL(1) du compte-rendu :
  19.  
  20. E -> T B
  21.  
  22. B -> "ou" E | epsilon
  23.  
  24. T -> F G
  25.  
  26. G -> "et" T | epsilon
  27.  
  28. F -> R | ( E ) | "si" E "alors" E "sinon" E "fsi"
  29.  
  30. R -> I O I
  31.  
  32. O -> = | <> | < | > | >= | <=
  33.  
  34. I -> Ident
  35. *)
  36. type vn = E | T | B | F | G | R | I | O ;;
  37.  
  38.  
  39. (** Definition des terminaux :
  40.  
  41. epsilon , = , <> , < , > , >= , <= , fsi , si , alors , sinon , et , ou
  42. *)
  43. type vt = Epsilon | Terminal of unite_lexicale;;
  44.  
  45. (** v correspond à l'union de Vt et Vn *)
  46. type v = NonTerminal of vn | Term of vt;;
  47.  
  48.  
  49. (** Construction d'un arbre concret *)
  50. type arbre_concret = Noeud of (vn*arbre_concret list) |
  51. Feuille of vt
  52. ;;
  53.  
  54. (* test de l'arbre concret *)
  55. Noeud(E,[Noeud(F,[Feuille(Terminal(UL_IDENT "et"));Feuille( Terminal(UL_IDENT "ou"))])]);;
  56.  
  57. (** Fonction de dérivation qui dérive un non terminal avec l'unite lexicale lue *)
  58. let derivation (nonT,ulNext) =
  59. match nonT with E -> begin
  60. match ulNext with UL_IDENT(_)|UL_PAROUV|UL_SI -> [NonTerminal(T);NonTerminal(B)]
  61. |_ -> [Term(Terminal(UL_IDENT("ERREUR dans E")))]
  62. end
  63. | B -> begin
  64. match ulNext with UL_OU -> [Term(Terminal(UL_OU));NonTerminal(E)]
  65. | _ -> [Term(Epsilon)]
  66. end
  67. |T -> begin
  68. match ulNext with UL_IDENT(_) |UL_PAROUV |UL_SI -> [NonTerminal(F);NonTerminal(G)]
  69. |_ -> [Term(Terminal(UL_IDENT("ERREUR dans T")))]
  70. end
  71. |G -> begin
  72. match ulNext with UL_ET -> [Term(Terminal(UL_ET));NonTerminal(T)]
  73. | _ -> [Term(Epsilon)]
  74. end
  75. |F -> begin
  76. match ulNext with UL_IDENT(_) -> [NonTerminal(R)]
  77. |UL_PAROUV -> [Term(Terminal(UL_PAROUV));NonTerminal(E);Term(Terminal(UL_PARFERM))]
  78. |UL_SI -> [Term(Terminal(UL_SI));NonTerminal(E);Term(Terminal(UL_ALORS));NonTerminal(E);Term(Terminal(UL_SINON));NonTerminal(E);Term(Terminal(UL_FSI))]
  79. |_ -> [Term(Terminal(UL_IDENT("ERREUR dans F")))]
  80. end
  81. |R ->begin
  82. match ulNext with UL_IDENT(_) -> [NonTerminal(I);NonTerminal(O);NonTerminal(I)]
  83. |_ -> [Term(Terminal(UL_IDENT("ERREUR dans R")))]
  84. end
  85. |O -> begin
  86. match ulNext with
  87. UL_EGAL -> [Term(Terminal(UL_EGAL))]
  88. | UL_DIFF -> [Term(Terminal(UL_DIFF))]
  89. | UL_INF -> [Term(Terminal(UL_INF))]
  90. | UL_SUP -> [Term(Terminal(UL_SUP))]
  91. | UL_INF_EG -> [Term(Terminal(UL_INF_EG))]
  92. | UL_SUP_EG -> [Term(Terminal(UL_SUP_EG))]
  93. |_ -> [Term(Terminal(UL_IDENT("ERREUR dans O")))]
  94. end
  95. |I -> begin
  96. match ulNext with
  97. UL_IDENT _ -> [Term(Terminal(ulNext))]
  98. |_ -> [Term(Terminal(UL_IDENT("ERREUR dans I")))]
  99. end
  100. ;;
  101.  
  102. (* test de derivation *)
  103. derivation(E,UL_IDENT("t"));;
  104. derivation(T,UL_IDENT("t"));;
  105. derivation(F,UL_IDENT("t"));;
  106. derivation(R,UL_IDENT("t"));;
  107. derivation(I,UL_IDENT("t"));;
  108.  
  109.  
  110. (** Fonctions mutuellement récursives analyse_caractere et analyse_mot *)
  111. (** analyse_caractere construit à partir d'une liste d'unités lexicales une feuille si elle rencontre
  112. un terminal, et appelle analyse_mot si elle rencontre un non terminal *)
  113. (** analyse_mot construit un noeud à partir d'un non terminal et d'une liste et de la meme liste d'unités lexicales
  114. pour construire une liste d'arbres concrets. La liste d'objets de type v appel analyse_caractere pour le premier élément
  115. et analyse_mot pour les suivants *)
  116. let rec analyse_caractere (carac,ulList) =
  117. match carac with Term(_) -> begin
  118. match ulList with x::r when carac = Term(Terminal(x)) -> (Feuille (Terminal x),r)
  119. |_ -> (Feuille Epsilon,ulList)
  120. end
  121. | NonTerminal(e) -> begin
  122. match ulList with x::r -> let (arbList,newUlList) = analyse_mot(derivation (e,x),ulList) in (Noeud(e,arbList),newUlList)
  123. |_ -> (Feuille (Terminal(UL_IDENT("Erreur 2"))),[UL_ERR "error2"])
  124. end
  125. and analyse_mot (caracList,ulList) =
  126. match caracList with x::r -> let (arb,newList) = analyse_caractere(x,ulList) in let (arbList,newUlList) = analyse_mot(r,newList) in (arb::arbList,newUlList)
  127. | []->([],ulList)
  128. ;;
Add Comment
Please, Sign In to add comment