Communauté Informatique NDFR.net : Analyseur syntaxique en JAVA - Programmation Web (HTML, PHP, ASP, Java, XML, etc.)
Reply
Analyseur syntaxique en JAVA
Thread Tools Display Modes
  #1  
Old 21-01-2005, 21:34
JamesPatageule's Avatar
JamesPatageule JamesPatageule is offline
Membre junior
 
Join Date: 21-01-2005
Location: Belfort
Age: 38
Posts: 7
Send a message via MSN to JamesPatageule
Question Analyseur syntaxique en JAVA

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> ")"
Que j'ai ensuite programmé en java ...

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" ) ;
 
}
 
}
[ADMIN] Les balises [ CODE ] servent à écrire du code... et aussi à garder l'indentation de celui-ci...
Reply With Quote
  #2  
Old 21-01-2005, 21:50
Matt Matt is offline
Super modérateur
1 Highscore
 
Join Date: 24-12-2002
Posts: 4,453
Ben ils sont sympa vos projets Là je suis en plein dans un projet similaire (aussi en école d'info), mais plus complet : analyse lexicale, syntaxique et contextuelle de langage Pascal puis génération de code ASM.
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).
Reply With Quote
  #3  
Old 21-01-2005, 22:33
Cougar's Avatar
Cougar Cougar is offline
Membre senior
 
Join Date: 16-09-2001
Location: Orléans
Age: 39
Posts: 3,850
Send a message via MSN to Cougar
le code de ton arbre est bon au moins ?
__________________
Reply With Quote
  #4  
Old 21-01-2005, 23:24
JamesPatageule's Avatar
JamesPatageule JamesPatageule is offline
Membre junior
 
Join Date: 21-01-2005
Location: Belfort
Age: 38
Posts: 7
Send a message via MSN to JamesPatageule
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
Reply With Quote
  #5  
Old 22-01-2005, 00:31
Matt Matt is offline
Super modérateur
1 Highscore
 
Join Date: 24-12-2002
Posts: 4,453
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:
Reply With Quote
  #6  
Old 22-01-2005, 12:24
Samva's Avatar
Samva Samva is offline
Niaisüre within
 
Join Date: 24-04-2003
Location: Tours
Age: 39
Posts: 2,320
Send a message via ICQ to Samva Send a message via MSN to Samva
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é...
__________________
For the End-of-the-World spell, press "Ctrl, Alt, Delete."



Last edited by Samva; 22-01-2005 at 12:38. Reason: Orthographe scandaleuse... :s
Reply With Quote
  #7  
Old 23-01-2005, 18:53
JamesPatageule's Avatar
JamesPatageule JamesPatageule is offline
Membre junior
 
Join Date: 21-01-2005
Location: Belfort
Age: 38
Posts: 7
Send a message via MSN to JamesPatageule
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 !!!
Reply With Quote
  #8  
Old 23-01-2005, 20:30
Daokwan's Avatar
Daokwan Daokwan is offline
Furet et fier de l'être
 
Join Date: 20-10-2002
Location: Antony
Age: 39
Posts: 621
Send a message via MSN to Daokwan
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
Reply With Quote
  #9  
Old 24-01-2005, 00:28
JamesPatageule's Avatar
JamesPatageule JamesPatageule is offline
Membre junior
 
Join Date: 21-01-2005
Location: Belfort
Age: 38
Posts: 7
Send a message via MSN to JamesPatageule
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 !!
Reply With Quote
  #10  
Old 24-01-2005, 11:00
Daokwan's Avatar
Daokwan Daokwan is offline
Furet et fier de l'être
 
Join Date: 20-10-2002
Location: Antony
Age: 39
Posts: 621
Send a message via MSN to Daokwan
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
Reply With Quote
  #11  
Old 24-01-2005, 12:22
JamesPatageule's Avatar
JamesPatageule JamesPatageule is offline
Membre junior
 
Join Date: 21-01-2005
Location: Belfort
Age: 38
Posts: 7
Send a message via MSN to JamesPatageule
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 ...
__________________
"Honni soit qui mal y pense !"
Reply With Quote
  #12  
Old 24-01-2005, 21:13
Cougar's Avatar
Cougar Cougar is offline
Membre senior
 
Join Date: 16-09-2001
Location: Orléans
Age: 39
Posts: 3,850
Send a message via MSN to Cougar
http://www.javafr.com/gma/tout/arbre

regarde si tu veux trouver ton bohneur ici ou encore là http://java.developpez.com/ .
__________________
Reply With Quote
  #13  
Old 24-01-2005, 22:48
JamesPatageule's Avatar
JamesPatageule JamesPatageule is offline
Membre junior
 
Join Date: 21-01-2005
Location: Belfort
Age: 38
Posts: 7
Send a message via MSN to JamesPatageule
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 !!!
__________________
"Honni soit qui mal y pense !"
Reply With Quote
  #14  
Old 25-01-2005, 21:13
Samva's Avatar
Samva Samva is offline
Niaisüre within
 
Join Date: 24-04-2003
Location: Tours
Age: 39
Posts: 2,320
Send a message via ICQ to Samva Send a message via MSN to Samva
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 !
__________________
For the End-of-the-World spell, press "Ctrl, Alt, Delete."


Reply With Quote
  #15  
Old 26-01-2005, 02:29
wuub's Avatar
wuub wuub is offline
Membre senior
 
Join Date: 13-11-2002
Location: Montpellier
Age: 48
Posts: 552
Send a message via MSN to wuub
Juste pour dire coucou parce que moi j'y entrave quedalle a tout ça
__________________
L'ignorance n'excuse pas la CONNERIE
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
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:12.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.