Always Asking
Always Asking

Reputation: 243

Building a parse tree in ANTLR with python target

I have a grammar for parsing SQL scripts. The lexer for the grammar works fine with the following code:

    with open("/path/to/sql/script.sql") as f:
    query = f.read().upper()
    tokenStream = antlr3.StringStream(query)
    lexer = MyLexer(tokenStream)
    for token in lexer:
        # process the token

    pass

I don't know how to parser the I have a grammar for parsing SQL scripts. The lexer for the grammar works fine with the following code. There is not much documentation forthe Python runtime on the ANLTR's website.

Upvotes: 0

Views: 384

Answers (1)

mjv
mjv

Reputation: 75288

Typically what you want to do after/past the above, is to create a TokenStream from the output of the Lexer and feed these tokens to your Parser. BTW the StringStream that you supply as input to the lexer is not really a token stream despite the name you gave it.

Maybe try something like:

...
lexer = MyLexer(tokenStream)
// Get a token stream
tokens = CommonTokenSream(lexer)
// Feed it to the parser   (assumes you named the Grammar/Parser  "MyParser")
parser = MyParser(tokens)

// Invoke the topmost rule (or some other rule) of the grammar, to start
// the parsing process
parser.SomeRule()

Upvotes: 1

Related Questions