[antlr-interest] Type-checking and compiling a language with import constructs

Jeroen van Schagen jeroen.v.schagen at gmail.com
Fri Jul 10 04:53:44 PDT 2009


My problem is nearly solved, I just got one little problem left with tree
construction. External file can now be parsed based on the import
directive(s).

*modules:        module*
                -> ^( module );

module:         'module' moduleId ( imprt | site | function )* 'end'
                -> ^( 'module' moduleId imprt* site* function* 'end' ) ;

moduleId returns [String path = ""] @after { $path += ".wae"; } :
            e=IDCON { $path += e.getText(); }
**            **( '.' e=IDCON { $path += "/" + e.getText(); } )*
                -> ^( IDCON ( '.' IDCON )* ) ;

imprt:            'import' id=moduleId ';'
                -> ^( 'import' moduleId ';' ^({ parseImport($id.path) }) ) ;

@parser::members {
    private CommonTree parseImport(String path) throws RecognitionException
{
        try {
            CharStream is = new ANTLRFileStream(path);
            WaebricLexer lexer = new WaebricLexer(is);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
                  WaebricParser parser = new WaebricParser(tokens);
                  return (CommonTree) parser.module().getTree();
              } catch(java.io.IOException e) { return new CommonTree(); } //
Invalid directives are ignored
    }
}*

Resulting in an example output of: (module imprt (import moddef ; *(module
moddef end)*) end)
However my desired output is: (module imprt (import moddef ;) end) *(module
moddef end)*

Are there any features in ANTLR allowing me to lift a tree node in the
hierarchy?

On Thu, Jul 9, 2009 at 3:16 PM, <jeroen.v.schagen at gmail.com> wrote:

> After some digging I've found a tutorial in v2.7:
> http://www.ibm.com/developerworks/aix/library/au-c_plusplus_antlr/index.html.
> However, while implementing the preprocessor I noticed that the
> antlr.TokenStreamSelector add input stream requires a antlr.TokenStream, but
> the org.antlr.runtime.Lexer inherits from antlr.runtime.TokenStream. Now I'm
> a bit puzzled why there are two different TokenStream classes. Is this
> purely a migration issue that is waiting to be resolved, or should I use
> some other facility for multiple token streams?
>
>
> On Jul 8, 2009 12:33pm, Jeroen van Schagen <jeroen.v.schagen at gmail.com>
> wrote:
> > Hello,
> >
> > Currently I'm working on the implementation of a mark-up DSL using ANTLR.
> This language makes use of module definitions and import constructs. What
> would be the optimal approach to loading contents of dependent modules in my
> AST?
> >
> >
> > The parser grammar is as follows:
> > module:         'module' moduleId ( imprt | site | function )* 'end' EOF
> ;
> > moduleId:      IDENTIFIER ( '.' IDENTIFIER )* ;
> > imprt:             'import' moduleId ';' ;
> >
> >
> > Thanks in advance!
> > Jeroen
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090710/d7f75f08/attachment.html 


More information about the antlr-interest mailing list