[antlr-interest] Why java heap error In the grammar

屈汉图 qujiakang at hotmail.com
Sat Dec 19 20:34:52 PST 2009


Hi all:
    I've write sql select statement grammar by antlr , after i make a copy of the grammar file and alter all the literal       with 'SELECT' instead of SELECT :'SELECT' etc .but i find a strange problem ,after do this ,when do debug ,antlr complain java heap error ,can some one tell me why !!! 

grammar SelectStat;

options {
  language = Java; 
  output=AST;
  ASTLabelType=Tree;
}
@header {
  import java.util.HashMap;
  import org.antlr.runtime.tree.*;
}
@members {
  StringBuilder sb = new StringBuilder();
  public void out(Object obj){
     sb.append(obj);
  }
  public void outToken(Object obj){
      Tree tree = (Tree)((ParserRuleReturnScope)obj).getTree();
      sb.append(tree.toStringTree());
  }
}

start_rule
  :
  (select_statement)
  ;
select_statement
  :
  select
  (
    'UNION'
    (
      'ALL'
      | 'DISTINCT'
    )?
    select_statement
  )?  
  {System.out.println(sb.toString());}
  ;
select
  :
  ('SELECT')
  (
    'ALL'
    | 'DISTINCT'
  )?
  select_list from_clause (where_clause)? (group_clause)? (order_clause)?
  
  ;

from_clause
  :
  'FROM' table_reference
  ;

where_clause
  :
  
  'WHERE'{out("where ");} (search_conditions)  
  ;

group_clause
  :
  'GROUP' 'BY' column_field (COMMA column_field)* (having_clause)?
  ;

having_clause
  :
  'HAVING' (search_conditions)
  ;

order_clause
  :
  'ORDER' 'BY' column_field (COMMA column_field)*
  (
    'DESC'
    | 'ASC'
  )?
  ;

table_reference
  :
  (table_name (COMMA table_name)*)
  |
  (
    table_name
    (
      'INNER'
      | 'LEFT'
      | 'RIGHT'
    )?
    'JOIN' table_name ('ON' search_conditions)?
  )
  ;

search_conditions
  :
 ('('? s=search_condition {outToken(s);}')'?)(a=('AND'|'OR'){out(a.getText());} '('? c=search_condition{outToken(c);} ')'?)*
  ;

search_condition
  :
  column_elemnent ROPR (column_value|(('@')*column_elemnent))  
  |column_elemnent 'IS'('NOT')?'NULL'
  
  ;

table_name
  :
  ID|ID ('AS')? ID
  ;

select_list
  :
  ASTERISK
  | column_elemnent (COMMA column_elemnent)*
  ;

column_elemnent
  :
  column_field
  | aggregate_function '(' column_field ')'
  ;


aggregate_function
  :
  'COUNT'
  | 'SUM'
  | 'MAX'
  | 'MIN'
  | 'AVG'
  ;

column_field
  :
  ID(DOT(ID|ASTERISK))?
  
  ;

column_value
  :
  STRING|NUMBER
  ;

/**
selectKey: 'SELECT'('ALL'|'DISTINCT')?;
whereKey:'WHERE';
nullKey:'IS'('NOT')?'NULL';
asKey:'AS';
havingKey:'HAVING';
groupbyKey:'GROUP BY';
orderbyKey:'ORDER BY';
orderFixKey:('DESC'|'ASC')?;
onKey:'ON';
andorKey:('AND'|'OR');
joinKey: ('INNER'|'LEFT'|'RIGHT')?'JOIN';
*/
ID
  :
  (
    'a'..'z'
    | 'A'..'Z'
    | '_'
  )
  (
    'a'..'z'
    | 'A'..'Z'
    | '0'..'9'
    | '_'
  )*
  ;
ROPR:('=' | '!=' | '<' | '<=' | '>' | '>=');
STRING
  :
  '\''
  (
    ESC_SEQ
    |
    ~(
      '\\'
      | '"'
     )
  )* '\''
  ;

CHAR
  :
  '\''
  (
    ESC_SEQ
    |
    ~(
      '\''
      | '\\'
     )
  )
  '\''
  ;



fragment
HEX_DIGIT
  :
  (
    '0'..'9'
    | 'a'..'f'
    | 'A'..'F'
  )
  ;

fragment
ESC_SEQ
  :
  '\\'
  (
    'b'
    | 't'
    | 'n'
    | 'f'
    | 'r'
    | '\"'
    | '\''
    | '\\'
  )
  | UNICODE_ESC
  | OCTAL_ESC
  ;

fragment
OCTAL_ESC
  :
  '\\' ('0'..'3') ('0'..'7') ('0'..'7')
  | '\\' ('0'..'7') ('0'..'7')
  | '\\' ('0'..'7')
  ;

fragment
UNICODE_ESC
  :
  '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
  ;

ASTERISK
  :
  '*'
  ;

COMMA
  :
  ','
  ;

DOT
  :
  '.'
  ;

NUMBER
  :
  ('0'..'9')+
  | ('0'..'9')+ ('.') ('0'..'9')+
  ; 
 


More information about the antlr-interest mailing list