Communauté Informatique NDFR.net : Analyseur syntaxique en JAVA - Programmation Web (HTML, PHP, ASP, Java, XML, etc.)
 
Analyseur syntaxique en JAVA
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 21-01-2005, 21:34
JamesPatageule's Avatar
JamesPatageule JamesPatageule is offline
Membre junior
 
Join Date: 21-01-2005
Location: Belfort
Age: 39
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
 

Bookmarks


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

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 16:16.

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