sirmsym80
02-04-2013, 20:47
Ciao a tutti!
Devo creare un analizzatore sintattico a discesa ricorsiva che parsifichi delle semplici espressioni aritmetiche come per esempio: 5+4*(6/2) o cose di questo tipo.
Deve quindi riconoscere le espressioni generate dalla grammatica:
<start> = <expr>
<expr> = <term> <exprp>
<exprp> = + <term> <exprp> | - <term> <exprp> | epsilon
<term> = <fact> <termp>
<termp> = * <fact> <termp> | / <fact> <termp> | epsilon
<fact> = ( <expr> ) | NUM
Per fare ciò il parsificatore utilizza il Lexer che ho fatto in precedenza il quale data una espressione tipo 5+4*(6/2) mi restituisce una serie di token del tipo:
<NUM, 5>
<PIU, +>
<NUM, 4>
<PER, *> ecc
Ho scritto solo parte del codice perchè poi non so come continuare:
public class Parsificatore {
private Lexer l;
private Token lookhaead;
public Parserificatore(Lexer lx) {
l = lx;
read(); //legge il prossimo carattere in input
}
void read() {
lookhaead = l.scan(); //read() richiama il metodo scan() della classe Lexer che legge il prossimo carattere
}
void match(Tag t) {
if(lookhaead.tag == t)
read();
else
error("Errore");
}
public void comincia() {
expr();
match(Tag.EOF);
}
private void expr() {
System.out.println("<Expr>");
term();
exprp();
}
private void exprp() {
System.out.println("<Exprp>");
switch(lookhaead.tag) {
case PLUS:
match(Tag.PLUS);
term();
exprp();
break;
case MINUS:
match(Tag.MINUS);
term();
exprp();
break;
default:
/* *** */
}
}
private void term() {
System.out.println("<Term>");
fact();
termp();
}
private void termp() {
System.out.println("<Termp>");
switch(lookhaead.tag) {
case TIMES:
match(Tag.TIMES);
fact();
termp();
break;
case DIVIDE:
match(Tag.DIVIDE);
fact();
termp();
break;
default:
/* *** */
}
}
private void fact() {
System.out.println("<Factor>");
switch(lookhaead.tag) {
case NUM:
match(Tag.NUM);
default:
expr();
}
}
/**
Main
*/
public static void main(String[] args) {
Lexer l = new Lexer();
Parserificatore p = new Parserificatore(l);
p.comincia();
}
}
Come posso scrivere il caso Epsilon?
Grazie
Devo creare un analizzatore sintattico a discesa ricorsiva che parsifichi delle semplici espressioni aritmetiche come per esempio: 5+4*(6/2) o cose di questo tipo.
Deve quindi riconoscere le espressioni generate dalla grammatica:
<start> = <expr>
<expr> = <term> <exprp>
<exprp> = + <term> <exprp> | - <term> <exprp> | epsilon
<term> = <fact> <termp>
<termp> = * <fact> <termp> | / <fact> <termp> | epsilon
<fact> = ( <expr> ) | NUM
Per fare ciò il parsificatore utilizza il Lexer che ho fatto in precedenza il quale data una espressione tipo 5+4*(6/2) mi restituisce una serie di token del tipo:
<NUM, 5>
<PIU, +>
<NUM, 4>
<PER, *> ecc
Ho scritto solo parte del codice perchè poi non so come continuare:
public class Parsificatore {
private Lexer l;
private Token lookhaead;
public Parserificatore(Lexer lx) {
l = lx;
read(); //legge il prossimo carattere in input
}
void read() {
lookhaead = l.scan(); //read() richiama il metodo scan() della classe Lexer che legge il prossimo carattere
}
void match(Tag t) {
if(lookhaead.tag == t)
read();
else
error("Errore");
}
public void comincia() {
expr();
match(Tag.EOF);
}
private void expr() {
System.out.println("<Expr>");
term();
exprp();
}
private void exprp() {
System.out.println("<Exprp>");
switch(lookhaead.tag) {
case PLUS:
match(Tag.PLUS);
term();
exprp();
break;
case MINUS:
match(Tag.MINUS);
term();
exprp();
break;
default:
/* *** */
}
}
private void term() {
System.out.println("<Term>");
fact();
termp();
}
private void termp() {
System.out.println("<Termp>");
switch(lookhaead.tag) {
case TIMES:
match(Tag.TIMES);
fact();
termp();
break;
case DIVIDE:
match(Tag.DIVIDE);
fact();
termp();
break;
default:
/* *** */
}
}
private void fact() {
System.out.println("<Factor>");
switch(lookhaead.tag) {
case NUM:
match(Tag.NUM);
default:
expr();
}
}
/**
Main
*/
public static void main(String[] args) {
Lexer l = new Lexer();
Parserificatore p = new Parserificatore(l);
p.comincia();
}
}
Come posso scrivere il caso Epsilon?
Grazie