[antlr-interest] How to debug tree grammar??

sunao furukawa sadie at kind.ocn.ne.jp
Sun Jul 19 22:37:26 PDT 2009


Hello! My name is Sunao Furukawa.
I am Japanese.Sorry,I can speak English a little.
I use ANTLRWorks1.2.3.but I cannot debug tree grammar file T1.g.

1.I generated E3.tokens and E3Lexer.java and E3Parser.java from E3.g in 
ANTLRWorks.
2.I generated and compiled T1.g(T1.java,T1.tokens),and E3.tokens and 
E3Lexer.java and E3Parser.java
  by debug.
3.I compiled TestE.java.
4.I started ANTLRWorks debug remote in localhost port 49153.
5.I executed [java TestE],and standard input [a=1 (CRLF) a+2*3 (CRLF) 
(Ctrl-C)].

But I could not debug T1.g and below, error occur.
(ASSIGN a 1)
line 0:-1 mismatched input '<EOF>' expecting NEWLINE
<mismatched token: [@-1,0:0='<no text>',<-1>,0:-1], resync=a+2*3>
java.net.BindException: Address already in use: JVM_Bind
java.net.BindException: Address already in use: JVM_Bind
 at java.net.PlainSocketImpl.socketBind(Native Method)
 at java.net.PlainSocketImpl.bind(Unknown Source)
 at java.net.ServerSocket.bind(Unknown Source)
 at java.net.ServerSocket.<init>(Unknown Source)
 at java.net.ServerSocket.<init>(Unknown Source)
 at 
org.antlr.runtime.debug.DebugEventSocketProxy.handshake(DebugEventSocketProxy.java:75)
 at T1.<init>(T1.java:55)
 at T1.<init>(T1.java:47)
 at __Test__.main(__Test__.java:17)
Exception in thread "main" java.lang.NullPointerException
 at 
org.antlr.runtime.debug.DebugEventSocketProxy.transmit(DebugEventSocketProxy.java:116)
 at 
org.antlr.runtime.debug.DebugEventSocketProxy.exitRule(DebugEventSocketProxy.java:130)
 at T1.prog(T1.java:154)
 at __Test__.main(__Test__.java:19)

I use Windows Vista Home Basic sp1.IP adress is private(dhcp).
Please tell me how to correct it and debug tree grammar.
below E3.g and T1.g and TestE.java.

--------------------------------------------------------------------------------------------------
<E3.g>
grammar E3;

options{
 output = AST;
 ASTLabelType = CommonTree;
}

tokens{
 ASSIGN; ALU_ADD; ALU_SUB; ALU_MUL; ALU_DIV;
}

prog
 : (
   statement
  { if ($statement.tree != null) 
System.out.println($statement.tree.toStringTree());}
   )+

 ;
statement
 : expression NEWLINE!
 | IDENTIFIER '=' expression NEWLINE
  -> ^(ASSIGN IDENTIFIER expression)
 | NEWLINE!
 ;
expression
 : product (aop^ product)*
 ;
aop
 : '+'
  -> ALU_ADD
 | '-'
  -> ALU_SUB
 ;
product
 : factor (pop^ factor)*
 ;
pop
 : '*'
  -> ALU_MUL
 | '/'
  -> ALU_DIV
 ;
factor
 : IDENTIFIER
 | CONSTANT
 | '('! expression ')'!
 ;

IDENTIFIER :   ('a'..'z'|'A'..'Z')+ ;
CONSTANT :   '0'..'9'+ ;
NEWLINE  :   '\r'? '\n' ;
WS  :   (' '|'\t')+ {skip();} ;
-------------------------------------------------------------------------------------
<T1.g>
tree grammar T1;

options{
 tokenVocab=E3;
 ASTLabelType = CommonTree;
}

@header {
import java.util.HashMap;
}

@members {
HashMap memory = new HashMap();
}

prog : statement+ ;

statement
 : e=expression
  { System.out.println($e.value); }
 | ^(ASSIGN id=IDENTIFIER e=expression)
  { memory.put($id.text, new Integer($e.value)); }
 ;
expression returns [int value]
 : ^(ALU_ADD a=expression b=expression)
  {$value = $a.value + $b.value;}
 | ^(ALU_SUB a=expression b=expression)
  {$value = $a.value - $b.value;}
 | ^(ALU_MUL a=expression b=expression)
  {$value = $a.value * $b.value;}
 | ^(ALU_DIV a=expression b=expression)
  {$value = $a.value / $b.value;}
 | IDENTIFIER
  {
  Integer v = (Integer)memory.get($IDENTIFIER.text);
  if (v != null) $value = v.intValue();
  else System.err.println("undefined variable " + $IDENTIFIER.text);
  }
 | CONSTANT
  { $value = Integer.parseInt($CONSTANT.text); }
 ;
------------------------------------------------------------------------------------
<TestE.java>
import java.io.*;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;

public class TestE {
    public static void main(String[] args) throws Exception {
        ANTLRInputStream input = new ANTLRInputStream(System.in);
        E3Lexer lexer = new E3Lexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        E3Parser parser = new E3Parser(tokens);
        E3Parser.prog_return r = parser.prog();

        CommonTree t = (CommonTree)r.getTree();
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        T1 walker = new T1(nodes);
        walker.prog();
    }
}
--------------------------------------------------------------------------------------




More information about the antlr-interest mailing list