[antlr-interest] (no subject)

Molka Tounsi tounsimolka at yahoo.fr
Thu Sep 23 13:06:26 PDT 2010


Hello,
I need your help please
I tried this example which translates of C in  SQL and he has worked before, but 
now he does not work  he shows me: "Warning : the grammar used by the remote 
parser is not the same ( CSQL.g )"

grammar CSQL;
options {
  language = Java;
  output = AST;
  ASTLabelType = CommonTree;
}
tokens {
 CLASSDEF;
 VARDEF;
}
// parser
program : (declaration { System.out.println($declaration.tree.toStringTree()); } 
) + ;

declaration : class_statement '{' (variable_statement)* '}'
                -> ^(class_statement variable_statement*) ;

class_statement : scope_modifier 'class' ID
                   -> ^(CLASSDEF ID) ;

variable_statement : scope_modifier type ID  ';'
                      -> ^(VARDEF type ID) ;

scope_modifier : 'public' ;

// more can be added
type : 'string'
     | 'int'
     | 'decimal'
     | 'DateTime' ;

// lexer
ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_') * ;

WS  :   ( ' ' | '\t' | '\r' | '\n' )+ { $channel = HIDDEN; } ;


tree grammar translator;

options {
  language = Java;
  tokenVocab = CSQL;
  ASTLabelType = CommonTree;
}

@members {
 String className;
 List<String> columns = new ArrayList<String>();
}

program : (declaration
          {
           String table = "CREATE TABLE " + className + '\n' + "(" + '\n';
           String seperator = ",";
           Object[] arrayColumns = columns.toArray();
           for(int i = 0; i < arrayColumns.length; i++) {
            if(i == arrayColumns.length - 1) seperator = "";
            table += " " + arrayColumns[i].toString() + seperator + '\n';
           }
           table += ")";
           System.out.println(table);
           columns.clear();
          } ) + ;

declaration : ^(CLASSDEF ID variable_statement*) { className = $ID.text; } ;

variable_statement : ^(VARDEF type ID)
                     {
                      columns.add($ID.text + " " + $type.value + " NOT NULL");
                     } ;

type returns [String value]
    : 'string' { $value = "nvarchar(255)"; }
    | 'int'   { $value = "integer"; }
    | 'decimal' { $value = "number(21,6)"; }
    | 'DateTime'  { $value = "date"; };

 what  is the problem?

Another problem, I need to use this type of rule:

expression: expression ' land ' expression;

He shows me: " expression is left-recursive rule"

Thanks  



      


More information about the antlr-interest mailing list