简体   繁体   中英

SableCC not hitting interpreter methods

I am new to SableCC. Just ran the calculator example at http://sablecc.sourceforge.net/thesis/thesis.html#PAGE26 . I used the grammar file and interpreter file as they are, and tried to parse simple arithmetic expression like "45 * 5 + 2". The problem is, the interpreter method caseAMultFactor does not seem to be hit. I see it hit caseAPlusExpr, or caseAMinusExpr if I change the "+" to "-". So does the Start.apply(DepthFirstAdapter) method only go through the top mode node? How can I iterate through all nodes like that sample codes seem to do? I am using Java 1.7 and hope that's not a problem.

For your convenience I have pasted the grammar and interpreter codes here. Thanks for your help.

### Grammar:

Package postfix;

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;

### Interpreter:

package postfix.interpret;
import postfix.analysis.DepthFirstAdapter;
import postfix.node.ADivFactor;
import postfix.node.AMinusExpr;
import postfix.node.AModFactor;
import postfix.node.AMultFactor;
import postfix.node.APlusExpr;
import postfix.node.TNumber;

public class Interpreter extends DepthFirstAdapter
{

     public void caseTNumber(TNumber node)
     {// When we see a number, we print it.
         System.out.print(node);
     }

     public void caseAPlusExpr(APlusExpr node)
     {
         System.out.println(node);
     }

     public void caseAMinusExpr(AMinusExpr node)
     {
         System.out.println(node);
     }

     public void caseAMultFactor(AMultFactor node)
     {// out of alternative {mult} in Factor, we print the mult.
         System.out.print(node.getMult());
     }

     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());
     }
}

What you posted looks fine. You did not post any of the output, nor did you post the code to run the interpreter.

Here's my code (I'm omitting the code for Interpreter as it's the same as yours):

package postfix;

import postfix.parser.*;
import postfix.lexer.*;
import postfix.node.*;
import java.io.*;

public class Compiler {
    public static void main(String[] arguments) {
        try {
            Parser p = new Parser(new Lexer(new PushbackReader(
                    new StringReader("(45 + 36/2) * 3 + 5 * 2"), 1024)));
            Start tree = p.parse();
            tree.apply(new Interpreter());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

and when run, it produces this:

45 36 2 / + 3 * 5 2 * +

Note the * is displayed, as expected.

UPDATE 2015-03-09

First, please copy/paste this grammar into a file named postfix.grammar. It should be the same as the one you have, but just copy/paste anyway:

Package postfix;

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;

Next, run this from a command line (make any necessary directory changes, of course):

java -jar "C:\Program Files\Java\sablecc-3.2\lib\sablecc.jar" src\postfix.grammar

Please ensure that you only have the Java classes from this invocation of SableCC (ie make sure any previously generated Java classes are deleted). Then using the Compiler class that I previously posted, try again. I cannot think of any problem with the grammar or problem with version 3.2 of SableCC that would cause the problem you're having. I'm hoping a fresh start will fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM