![]() |
Analyseur syntaxique en JAVA
|
Thread Tools
![]() |
Display Modes
![]() |
#1
|
||||
|
||||
![]()
Voila etant etudiant en informatique, j'ai comme projet de faire un Analyseur Syntaxique d'expressions booleennes ...
J'ai donc fait une grammaire CORRECTE : Code:
<expr> ::= <binaire> <fin_expr> <fin_expr> ::= "=" <binaire> ::= ">" <binaire> ::= <binaire> ::= <terme> <fin_bin> <fin_bin> ::= "&" <fin_et> ::= "|" <fin_ou> ::= <fin_et> ::= <terme> "&" <fin_et> ::= <terme> <fin_ou> ::= <terme> "|" <fin_ou> ::= <terme> <terme> ::= "!" <terme> ::= <variable> <variable> ::= "A" ... "Z" ::= "(" <expr> ")" Sachant que le programme doit retourner "erreur" si l'expression booleenne est incorecte et un arbre binaire prefixé si l'expression est bonne ... Ce dernier me sort "erreur" pour l'expression rentree en dur "a|b" ... ![]() Les operateurs geres sont : et & ; ou | ; implique > ; equivaut = ; non ! De plus en testant de faire un affichage de mon arbre dans le cas de l'expression non correcte , une belle erreur est renvoyée ... Je manque de temps et d'experience en java ... C'est ce pourquoi je fais appel a des pros comme vous. Donc si vous avez le temps de jeter un oeil je vous en serais tres reconnaissant !!! Merci, je met quand meme le code java ... Code:
import java.lang.*; import java.io.*; class Projet{ static class Strind { private String str = "" ; private int ind = 0 ; Strind ( String s ) { str = s ; } public boolean encore ( ) { return ind < str.length ( ) ; } public char getchar ( ) { return str.charAt ( ind ) ; } public void incr ( ) { if ( ind != ( str.length ( ) - 1 ) ) ind ++ ; } public void desIncr ( ) { ind --; } } static class Arbre { private int sel ; private char op ; private Arbre fg ; private Arbre fd ; Arbre ( ) { sel = -1 ; op = '\0' ; fg = null ; fd = null ; } Arbre ( char var ) { sel = 0 ; op = var ; } Arbre ( char o , Arbre fs ) { sel = 1 ; op = o ; fg = fs ; } Arbre ( char o , Arbre g , Arbre d ) { sel = 2 ; op = o ; fg = g ; fd = d ; } public int getsel ( ) { return sel ; } public char getop ( ) { return op ; } public Arbre getfg ( ) { return fg ; } public Arbre getfd ( ) { return fd ; } public String toString ( ) { switch ( sel ) { case -1 : return "Arbre vide" ; case 0 : return op + "" ; case 1 : return op + "(" + fg.toString ( ) + ")" ; case 2 : return op + "(" + fg.toString ( ) + "," + fd.toString ( ) + ")" ; default : return "" ; } } } public static boolean estblanc ( char c ) { return c == ' ' || c == '\n' || c == '\t' ; } public static void sbl ( Strind s ) { while ( s.encore ( ) && estblanc ( s.getchar ( ) ) ) s.incr ( ) ; } public static Arbre expr ( Strind s ) { Arbre f ; if ( ( f = binaire ( s ) ) != null ) return fin_expr ( s , f ) ; return null ; } public static Arbre fin_expr ( Strind s , Arbre f ) { char c ; Arbre g ; if ( s.encore ( ) ) { c = s.getchar ( ) ; if ( c == '>' || c == '=' ) { s.incr ( ) ; sbl ( s ) ; if ( ( g = binaire ( s ) ) != null ) return new Arbre ( c , f , g ) ; return null ; } } return f ; } public static Arbre binaire ( Strind s ) { Arbre f ; if ( ( f = terme ( s ) ) != null ) return fin_bin ( s , f ) ; return null ; } public static Arbre fin_bin ( Strind s , Arbre f ) { char c ; Arbre g ; if ( s.encore ( ) ) { c = s.getchar ( ) ; if ( c == '&' ) { s.incr ( ) ; sbl ( s ) ; if ( ( g = fin_et ( s ) ) != null ) return new Arbre ( c , f , g ) ; return null ; }else{ if ( c == '|' ) { s.incr ( ) ; sbl ( s ) ; if ( ( g = fin_ou ( s ) ) != null ) return new Arbre ( c , f , g ) ; return null ; } } } return f ; } public static Arbre fin_et ( Strind s ) { char c ; Arbre f ; Arbre g ; if ( ( f = terme ( s ) ) != null ) { c = s.getchar ( ) ; if ( c == '&' ) { s.incr ( ) ; sbl ( s ) ; if ( ( g = fin_et ( s ) ) != null ) return new Arbre ( c , f , g ) ; return null ; } return f ; } return null ; } public static Arbre fin_ou ( Strind s ) { char c ; Arbre f ; Arbre g ; if ( ( f = terme ( s ) ) != null ) { c = s.getchar ( ) ; if ( c == '|' ) { s.incr ( ) ; sbl ( s ) ; if ( ( g = fin_ou ( s ) ) != null ) return new Arbre ( c , f , g ) ; return null ; } return f ; } return null ; } public static Arbre terme ( Strind s ) { char c ; Arbre f ; if ( s.encore ( ) ) { c = s.getchar ( ) ; if ( c == '!' ) { s.incr ( ) ; sbl ( s ) ; if ( ( f = terme ( s ) ) != null ) return new Arbre ( c , f ) ; return null ; }else{ if ( (f = variable ( s ) ) != null ) return f ; } } return null ; } public static Arbre variable ( Strind s ) { char c ; Arbre a ; if ( s.encore ( ) ) { c = s.getchar ( ) ; if ( c >= 'a' && c <= 'z' ) { s.incr ( ) ; if ( s.encore ( ) ) { sbl ( s ) ; }else{ s.desIncr ( ) ; } return new Arbre ( c ) ; } if ( c == '(' ) { s.incr ( ) ; sbl ( s ) ; if ( ( a = expr ( s ) ) != null ) if ( s.encore ( ) ) if ( s.getchar ( ) == ')' ) { s.incr ( ) ; sbl ( s ) ; return a ; } } } return null ; } public static Arbre analyse ( Strind s ) { Arbre a ; sbl ( s ) ; if ( ( a = expr ( s ) ) != null ){ if ( s.encore ( ) ) return null ; return a ; } return null; } public static void main ( String argv [] ) { Arbre a ; String s ; Strind si ; s = "a|b" ; Arbre b = new Arbre ( a ) ; System.out.println ( b.toString ( ) ); if ( argv.length >= 1 ) { System.out.println ( argv[0] ) ; si = new Strind ( argv[0] ) ; } else { System.out.println ( s ) ; si = new Strind ( s ) ; } if ( ( a = analyse( si ) ) != null ) { System.out.println ( a.toString ( ) ) ; } else //System.out.println ( a.toString ( ) ) ; System.out.println ( "erreur" ) ; } } |
#2
|
|||
|
|||
Ben ils sont sympa vos projets
![]() Joint à ton post une version en fichier de ton code (histoire d'avoir de l'identation) et j'y jeterais un coup d'oeil dès que mon projet sera terminé (vendredi prochain). |
#3
|
||||
|
||||
le code de ton arbre est bon au moins ?
|
#4
|
||||
|
||||
Je l'espere que mon code d'arbre est bon ... mais je tourne en rond dans ce projet depuis quelques heures deja et je voit vraiment pas ...
Pour moi tout est ok !!! Je doit rendre version finale dans la semaine prochaine ... Et g pas le net le week-end ... Donc je repasserai voir Dimanche soir si qqn a reussi a m'aider .. En tous cas MERCI a VOUS !!! http://www.lotc.fr/Perso/Projet.java http://www.lotc.fr/Perso/Grammaire.txt |
#5
|
|||
|
|||
Bon ben désolé, mais moi je doit rendre le code du mien mardi et la doc vendredi, donc j'vais pas avoir le temps de m'occuper de toi :confused:
|
#6
|
||||
|
||||
Je vais regarder ton code de plus près et voir a te le rendre en version utilisable, mais je ne garanti rien je ne dispose pas d'un temps illimité...
Last edited by Samva; 22-01-2005 at 12:38. Reason: Orthographe scandaleuse... :s |
#7
|
||||
|
||||
Je vous remerci bien ... Je suis revenu ...
Merci de votre aide etsachez que par la suite si moi je peux aider je n'hesiterais pas !!! Sinon je cherche un mouen gratuit si possible de programmer sous windows en java (je bosse sous linux) ... Merci a vous !!! |
#8
|
||||
|
||||
Sous windows, tu peux utiliser l'ide Eclipse qui est pas mal, c'est celui qu'on utilise en cours.
Il requiert bien sur le java sdk d'installé (il faut mettre le chemin du dossier Bin dans la variable PATH de windows)
__________________
Casser une branche, c'est facile, en casser 2, c'est plus dur |
#9
|
||||
|
||||
OK j'ai regardé ca ...
Mais j'aurais besoin d'aide car il y a bcp de choses et je sais pas lesquelles prendre ... J'ai pris : - eclipse-SDK-3.1M2-win32.zip Mais je sais pas quels NLPacks prendre ... Et pour machine Java je crois que c'est le SDK ou le JRE ou les 2 ? Merci bien !! |
#10
|
||||
|
||||
Concerant java, il te faut le JDK, qui est en fait le kit de dev, la jre est simplement l'environement d'execution.
Concernant Eclipse, je te join directement le lien pour le DL via un miroir, c'est pas génial, mais au moin tu te tromperas pas ![]() Tu peux le dl ici
__________________
Casser une branche, c'est facile, en casser 2, c'est plus dur |
#11
|
||||
|
||||
Merci bien ... j'ai testé ca marche nickel ...
Le probleme de mon prog viens de la creation de l'arbre ... Mais je sais pas le resoudre ... enfin j'y arrive pas ...
__________________
![]() ![]() |
#12
|
||||
|
||||
http://www.javafr.com/gma/tout/arbre
regarde si tu veux trouver ton bohneur ici ou encore là http://java.developpez.com/ . |
#13
|
||||
|
||||
Ben en fait j'ai corrigé ...
Probleme d'incrementation qui retournais toujours NULL ... Probleme reparé donc vous cassez pu le tete les gars et merci quand meme !!! Je repasserais vous questionner si j'ai un autre probleme !!! Pis sinon je me promenerais dans les forums !!! Encore merci !!!
__________________
![]() ![]() |
#14
|
||||
|
||||
Ah ben j'étais presque sur la réponse lundi matin en projet, si un con s'était pas amusé a appuyer sur la touche power de mon portable j'aurais eu le temps d'en être sur.... gniiiii
M'en fou je me suis vengé ... content que tu ais trouvé ton erreur ! |
#15
|
||||
|
||||
Juste pour dire coucou parce que moi j'y entrave quedalle a tout ça
![]()
__________________
L'ignorance n'excuse pas la CONNERIE |
![]() |
Bookmarks |
«
Previous Thread
|
Next Thread
»
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Probleme Java + Firewall ! | Kalisto | Internet, Réseaux et Sécurité | 4 | 04-09-2004 16:28 |
appli java qui te fait écouter la musique libre que tu aimes | fonji | Musique | 1 | 16-07-2004 00:08 |
WAP, jeux java et hosting | ThOMaZ2118 | Internet, Réseaux et Sécurité | 4 | 17-04-2004 21:55 |
Java 2 Standard Edition 1.5.0 Beta 1 | Benjy | Actualité | 1 | 06-02-2004 12:24 |
Java | nonoleptitmalin | Programmation Web (HTML, PHP, ASP, Java, XML, etc.) | 9 | 08-07-2003 14:39 |
All times are GMT +2. The time now is 21:38.
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.