Communauté Informatique NDFR.net

Communauté Informatique NDFR.net (http://www.ndfr.net/forums/index.php)
-   Programmation Web (HTML, PHP, ASP, Java, XML, etc.) (http://www.ndfr.net/forums/forumdisplay.php?f=65)
-   -   Analyseur syntaxique en JAVA (http://www.ndfr.net/forums/showthread.php?t=5762)

JamesPatageule 21-01-2005 21:34

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

Matt 21-01-2005 21:50

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).

Cougar 21-01-2005 22:33

le code de ton arbre est bon au moins ?

JamesPatageule 21-01-2005 23:24

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

Matt 22-01-2005 00:31

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:

Samva 22-01-2005 12:24

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é...

JamesPatageule 23-01-2005 18:53

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 !!!

Daokwan 23-01-2005 20:30

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)

JamesPatageule 24-01-2005 00:28

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 !!

Daokwan 24-01-2005 11:00

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

JamesPatageule 24-01-2005 12:22

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

Cougar 24-01-2005 21:13

http://www.javafr.com/gma/tout/arbre

regarde si tu veux trouver ton bohneur ici ou encore là http://java.developpez.com/ .

JamesPatageule 24-01-2005 22:48

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 !!!

Samva 25-01-2005 21:13

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 !

wuub 26-01-2005 02:29

Juste pour dire coucou parce que moi j'y entrave quedalle a tout ça :p

JamesPatageule 29-01-2005 00:59

Merci bien ...

De plus il a fallut ameliorer pour repondre aux exigences du prof ...

Projet a rendre dimanche 29 au soir au plus tard ...

Si quelqu'un en a besoin ... qu'il me mail !!!

Encore merci a tous !!!!


All times are GMT +2. The time now is 10:54.

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