//Dans le cas ou il n'y a que deux parenthèses public boolean testSimple(String invocation){ return invocation.matches("[A-Za-z0-9]+\\.[A-Za-z0-9]+\\([A-Za-z0-9 ]*\\)"); } //Ensuite, ca se complique, si il y a plusieurs méthodes appelées par exemple : m.afficher(f.calculer()); public boolean testCompose(String invocation){ boolean res; //Si il y a plusieurs parentheses if(invocation.matches("[^\\(]*\\([^\\(]*\\("){ res=testCompose(recupererSousGroupe(invocation)) && testSimple(supprimerSousGroupe(invocation)); } else { res=testSimple(invocation); } return res; } public String recupererSousGroupe(String ligne){ String res=""; String patternStr = "[^\\(]*\\([^\\(]*\\((.*).*"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(ligne); boolean matchFound = matcher.find(); if (matchFound && matcher.groupCount()>=1) { res = matcher.group(1); } return res; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpackage analyseur; import java.util.List; public class Lexicale { public String chaine="(a+b)"; private int pos=0; public Lexicale(String chaine){this.chaine=chaine;} public boolean haveLexeme() {return(chaine.length()>pos);} public String getNextLexeme(){ ignoreSpaceTabulationRetourLigne(); while (haveLexeme()) { switch(chaine.charAt(pos)){ case '+':pos++; return("+"); case '-':pos++; return("-"); case '*':pos++; return("*"); case '/':pos++; return("/"); case '^':pos++; return("^"); case '(':pos++; return("("); case ')':pos++; return(")"); case'0': case'1': case'2': case'3': case'4':case'5':case'6':case'7':case'8':case'9': pos++; while (haveLexeme()&& chaine.charAt(pos)<='9'&& chaine.charAt(pos)>='0') pos++; return("nb"); case'a': case'b': case'c': case'd': case'e': case'f': case'g': case'h': case'i': case'j': case'k': case'l': case'm': case'n': case'o': case'p': case'q': case'r': case's': case't': case'u': case'v': case'w': case'x': case'y': case'z': case'A': case'B': case'C': case'D': case'E': case'F': case'G': case'H': case'I': case'J': case'K': case'L':case'M': case'N': case'O': case'P': case'Q': case'R': case'S':case'T': case'U': case'V': case'W': case'X': case'Y': case'Z': {pos++;} while (haveLexeme()&& (chaine.charAt(pos)<='9')&& chaine.charAt(pos)>='0'||chaine.charAt(pos)>='a'&& chaine.charAt(pos)<='z'||chaine.charAt(pos)>='A'&& chaine.charAt(pos)<='Z') {pos++;} return("id"); case'\n': case'\t': case' ': ignoreSpaceTabulationRetourLigne(); default: pos++; return (chaine.charAt(pos-1)+"n'est pas élément du langage"); } } return ("EOF"); } private void ignoreSpaceTabulationRetourLigne(){ if (haveLexeme()&& ((chaine.charAt(pos))=='\n')||((chaine.charAt(pos))=='\t')||(chaine.charAt(pos))==' ') {pos++; while (haveLexeme()&& ((chaine.charAt(pos))=='\n')|| ((chaine.charAt(pos))=='\t')||(chaine.charAt(pos))==' ') {pos++;} } } public int getPos(){return pos;} public void setPos(int pos){this.pos=pos;} public void synchro(List <String>l) {String lex=""; if (haveLexeme()) lex= getNextLexeme(); while (haveLexeme()&& (!l.contains(lex))) {lex= getNextLexeme();} if (l.contains(lex)) {int gh=getPos()-1; setPos(gh); } } }
package analyseur; import java.*; import java.util.List; import analyseur.Lexicale.*; public class Syntaxe { private static String symbole; static List<String> L; static String chaine = null; static Lexicale x = new Lexicale(chaine); public grammaire (String chaine ){ this.chaine= chaine; } static void E() { T(); R1(); } static void T() { F(); R2(); } static void R1() { //int i=0; while (i< chaine.length()) //symbole = chaine.charAt(0); if (symbole== "+") { accepter ("+"); T(); R1(); }else if( symbole=="-"){ accepter ("-"); T(); R1();}else {erreur(1);} // on ne met pas else erreur car on a epsilon } static void R2() { if (symbole=="*") { accepter ("*"); T(); R1(); }else if( symbole=="/"){ accepter ("/"); T(); R1();}else{erreur(2);} // on ne met pas else erreur car on a epsilon } static void F() { H(); R3(); } static void R3() { if (symbole =="^") {accepter("^"); F();} else {erreur(3);} } static void H(){ if(symbole =="(") { accepter("("); E(); } else if (symbole =="id") { accepter ("id"); }else{erreur(4);} } private static void accepter(String cc) { if (symbole cc){symbole x.getNextLexeme();} } public static void m(String xx){ symbole = x.getNextLexeme(); E(); if (symbole=="§") { System.out.println("Succès");} else {System.out.println("Echec"); } } private static void erreur(int n){ switch (n){ case 1:{System.out.println("le symbole + ou - est manquant"); L.add("§"); L.add("("); L.add(")"); L.add("id"); L.add("nb"); x.synchro(L); List L1 = null;// L1 contient les premiers de R1 //List L2 = null;// contient les suivants de R1 L1.add('+'); L1.add('-'); // L2.add(')'); //L2.add('§'); if (L1.contains(symbole)) {T();} /* else if (L2.contains(symbole)){}*/ } case 2:{System.out.println("le symbole * ou / est manquant"); L.add("*"); L.add("/"); L.add("+"); L.add("-"); L.add("§"); L.add(")"); x.synchro(L); List L3 = null; L3.add("*"); L3.add("/");// if ( L3.contains(symbole)){F();}} case 3:{System.out.println("le symbole ^ est manquant"); L.add("*"); L.add("/"); L.add("+"); L.add("-"); L.add("§"); L.add( ")"); L.add("^"); x.synchro(L); List L4 = null; L4.add("^"); if ( L4.contains(symbole)){F();}} case 4:{System.out.println("un id est manquant");} L.add("*"); L.add("/"); L.add("+"); L.add("-"); L.add("§"); L.add(" )");L.add("(");L.add("^"); L.add("nb");L.add("id"); x.synchro(L); if (symbole == "("){ E();} } } }
public class PatternSyntaxeException extends Exception { /** * */ public PatternSyntaxeException() { super(); // TODO Auto-generated constructor stub } /** * @param message * @param cause */ public PatternSyntaxeException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } /** * @param message */ public PatternSyntaxeException(String message) { super(message); // TODO Auto-generated constructor stub } /** * @param cause */ public PatternSyntaxeException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }
String chaine="(3+2)*(4+5"; int nb=0; for(int i=0; i<chaine.length; i++){ if(chaine.charAt(i)=="(".charAt(0)){ nb++; } else if(chaine.charAt(i)==")".charAt(0)){ nb--; } } if(nb!=0){ System.out.println("Syntaxe incorrecte"); }
for(int i=0; i<regex.length; i++){ if(regex.charAt(i)=="(".charAt(0)){ determinerSousExpression(regex, i); } } public void determinerSousExpression(String regex, int pos){ int niveau=0; boolean match=false; for(int i=pos; i<regex.length; i++){ if(regex.charAt(i)=="(".charAt(0)){ niveau++; } if(regex.charAt(i)==")".charAt(0)){ if(niveau==0){ System.out.prinln(regex.substring(pos, pos+i)); match=true; } niveau--; } } if(!match){ // Et là, il font une petite mise en forme sur la sortie pour te montrer ton erreur throw new PatternSyntaxException("Erreur en position "+pos); } }