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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| #include <vector>
#include <string>
#include <iostream>
#include "lexical.hpp"
#include "syntaxique.hpp"
#include "symboles.hpp"
#include "uniLex.hpp"
bool absent(std::vector<std::string>const &l,std::string const &s);
bool present(std::vector<std::string>const &l,std::string const &s);
syntaxique::syntaxique(char *nomfichier):L(nomfichier)/*,ecriresortie(true),
recurs(false)*/{
//pilerecurs.clear();
a=L.anaLex();
grammaire=Grammaire();
//ax=true;
}
void syntaxique::consommer(terminal const &t){
if(t==a.getLex())
a=L.anaLex();
else{
std::cerr<<"Ligne "<<L.getLigne()<<": symbole "<<a.getLexeme()
<<" inattendu"<<std::endl;
//ecriresortie=false;
}
}
std::vector<production>syntaxique::Grammaire(){
std::vector<production>restegrammaire_h,grammaire_s,
restegrammaire_s;
production production_s;
production_s=Production();
restegrammaire_h.push_back(production_s);
restegrammaire_s=Restegrammaire(restegrammaire_h);
consommer(diese);
grammaire_s=restegrammaire_s;
for(auto s:symboles)
if(absent(nonterminaux,s))
terminaux.push_back(s);
return grammaire_s;
}
std::vector<production>syntaxique::
Restegrammaire(std::vector<production> restegrammaire_h){
std::vector<production>restegrammaire1_h,restegrammaire_s,
restegrammaire1_s;
production production_s;
if(a.getLex()==mot){
production_s=Production();
restegrammaire_h.push_back(production_s);
restegrammaire1_h=restegrammaire_h;
restegrammaire1_s=Restegrammaire(restegrammaire1_h);
restegrammaire_s=restegrammaire1_s;
}
else
restegrammaire_s=restegrammaire_h;
return restegrammaire_s;
}
production syntaxique::Production(){
production production_s;
std::vector<std::string> corps_s;
std::string tete_s;
tete_s=Tete();
production_s.settete(tete_s);
corps_s=Corps();
production_s.setcorps(corps_s);
consommer(antislash);
return production_s;
}
std::string syntaxique::Tete(){
std::string tete_s,mot_s;
mot_s=a.getLexeme();
consommer(mot);
if(absent(symboles,mot_s))
symboles.push_back(mot_s);
if(absent(nonterminaux,mot_s))
nonterminaux.push_back(mot_s);
tete_s=mot_s;
return tete_s;
}
std::vector<std::string> syntaxique::Corps(){
std::string mot_s;
std::vector<std::string> restecorps_h,corps_s,restecorps_s;
mot_s=a.getLexeme();
consommer(mot);
if(absent(symboles,mot_s))
symboles.push_back(mot_s);
restecorps_h.push_back(mot_s);
restecorps_s=Restecorps(restecorps_h);
corps_s=restecorps_s;
return corps_s;
}
std::vector<std::string> syntaxique::
Restecorps(std::vector<std::string>restecorps_h){
std::vector<std::string> restecorps1_h,restecorps1_s,restecorps_s;
std::string mot_s;
if(a.getLex()==mot){
mot_s=a.getLexeme();
if(absent(symboles,mot_s))
symboles.push_back(mot_s);
consommer(mot);
restecorps_h.push_back(mot_s);
restecorps1_h=restecorps_h;
restecorps1_s=Restecorps(restecorps1_h);
restecorps_s=restecorps1_s;
}
else
restecorps_s=restecorps_h;
return restecorps_s;
}
void syntaxique::deuxannulables(){
int n_annulables;
std::vector<production> listep;
for(auto nt:nonterminaux){
n_annulables=0;
listep=tetescommunes(nt);
for(auto p:listep)
if(annulable(p.getcorps())){
n_annulables++;
if(n_annulables>1){
std::cerr<<"plus d'un annulable avec "<<p.gettete()<<std::endl;
break;
}
}
}
}
std::vector<production>syntaxique::tetescommunes(std::string t){
std::vector<production>retour;
for(auto p:grammaire)
if(p.gettete()==t)
retour.push_back(p);
return retour;
}
bool syntaxique::annulable(std::vector<std::string> c){
std::vector<production>communes;
std::vector<std::string> pcorps;
if(c[0]=="epsilon")
return true;
else
for(auto s:c)
if(present(terminaux,s)&&s!="epsilon")
return false;
else{
communes=tetescommunes(s);
for(auto p:communes){
pcorps=p.getcorps();
if( ! annulable(pcorps))
return false;
}
}
return true;
}
void syntaxique::sortie(){
deuxannulables();
}
void production::settete(std::string const &t){
tete=t;
}
std::string production::gettete(){
return tete;
}
void production::setcorps(std::vector<std::string> const &c){
corps=c;
}
std::vector<std::string>production::getcorps(){
return corps;
} |
Partager