[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Doubts about running example with Sablecc
Hi!
My name is Geoflávia Guilarducci.
I am really new to Sablecc and I need help. I starting studying about
Sablecc by thesis ?SableCC: An Object-Oriented Compiler Framework by Étienne
Gagnon?. In this thesis, there is a example (section 3.6 page 25 ? To
translate arithmetic expressions into postfix form), but I can not to run
with success.
The gramar is:
Tokens
number = ['0' .. '9']+;
plus = '+';
minus = '-';
mult = '*';
div = '/';
mod = '%';
l_par = '(';
r_par = ')';
blank = (' ' | 13 | 10)+;
Ignored Tokens
blank;
Productions
expr =
{factor} factor |
{plus} expr plus factor |
{minus} expr minus factor;
factor =
{term} term |
{mult} factor mult term |
{div} factor div term |
{mod} factor mod term;
term =
{number} number |
{expr} l_par expr r_par;
I implemented two classes ? Translation and Compiler :
import postfix.analysis.*;
import postfix.node.*;
public class Translation extends DepthFirstAdapter{
public void caseTNumber(TNumber node)
{// When we see a number, we print it.
System.out.print(node);
}
public void outAPlusExpr(APlusExpr node)
{// out of alternative {plus} in Expr, we print the plus.
System.out.print(node.getPlus());
}
public void outAMinusExpr(AMinusExpr node)
{// out of alternative {minus} in Expr, we print the minus.
System.out.print(node.getMinus());
}
public void outAMultFactor(AMultFactor node)
{// out of alternative {mult} in Factor, we print the mult.
System.out.print(node.getMult());
}
public void outADivFactor(ADivFactor node)
{// out of alternative {div} in Factor, we print the div.
System.out.print(node.getDiv());
}
public void outAModFactor(AModFactor node)
{// out of alternative {mod} in Factor, we print the mod.
System.out.print(node.getMod());
}
}
import postfix.parser.*;
import postfix.lexer.*;
import postfix.node.*;
import java.io.*;
import java.util.*;
public class Compiler {
public static void main(String[] arguments) {
try
{
Calendar cal = new GregorianCalendar();
int hour24;
int min;
int sec;
System.out.println("Type an arithmetic expression:");
// Displaying Start Time
hour24 = cal.get(Calendar.HOUR_OF_DAY);
min = cal.get(Calendar.MINUTE);
sec = cal.get(Calendar.SECOND);
System.out.println("Start Time: " + hour24 + ":" + min + ":" +
sec);
// Create a Lexer instance.
Lexer lex = new Lexer (new PushbackReader(new
InputStreamReader(System.in), 1024));
// Create a Parser instance.
Parser p = new Parser(lex);
// Parse the input.
Start tree = p.parse();
// Apply the translation.
tree.apply( new Translation() );
// Displaying End Time
hour24 = cal.get(Calendar.HOUR_OF_DAY);
min = cal.get(Calendar.MINUTE);
sec = cal.get(Calendar.SECOND);
System.out.println("End Time: " + hour24 + ":" + min + ":" + sec);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
For running the posfix Translator, I typed: java Compiler, but, it does not
finish. I think that it is looping.
I debugged it, and it always stops when to make parsing.
It happens in line 287 ? Lexer.getchar()... int result = this.in.read();
Please, anybody could me help?
Thanks a lot,
Geoflávia