Antlr - java.g

Programming/Java 2010.12.06 13:41 Posted by 파란크리스마스

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


신고


 

티스토리 툴바