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: 40
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
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 01:14.

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