티스토리 뷰

Programming/Java

Antlr - java.g

파란크리스마스 2010. 12. 6. 13:41
728x90

@header {
package xxx;
}

@members {
private AppBase appBase;

public void SetAppBase(AppBase appBase) {
  this.appBase = appBase;
}
}

@lexer::header {
package xxx;
}

packageDeclaration 
    :   'package' qualifiedName {
         System.out.println("--" + $qualifiedName.result.getLine() + "/" + $qualifiedName.result.getQualifiedName());
      }
        ';'
    ;

qualifiedName returns [AstQualifiedName result]
scope {
  AstQualifiedName current;
}
@init {
  $qualifiedName::current = new AstQualifiedName();
}

    :   IDENTIFIER {
          $qualifiedName::current.setLine($IDENTIFIER.getLine());
          $qualifiedName::current.addIdentifier($IDENTIFIER.getText());
      }
        ('.' IDENTIFIER {
          $qualifiedName::current.addIdentifier($IDENTIFIER.getText());
        }
        )* {
          $result = $qualifiedName::current;
        }
    ;

-----------------------------------------------------------------------------------

package xxx;

import xxx.AppBase;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.RuleReturnScope;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonErrorNode;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.TreeAdaptor;

public class JavaTestMain {
 
 public static void main(String[] args) throws Exception {
  
  AppBase appBase = new AppBase();
  
  ANTLRFileStream fileStream = new ANTLRFileStream("ASTMain.java");
  JavaLexer lexer = new JavaLexer(fileStream);
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  JavaParser parser = new JavaParser(tokens);
  parser.SetAppBase(appBase);
  parser.compilationUnit();
  }
}


댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함