antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.2k stars 3.29k forks source link

antlr4, python 3, Parser, ParseTreePatternMatcher, compile #2237

Closed jpicau closed 6 years ago

jpicau commented 6 years ago

Dear ANTLR team,

I used conda to install the antlr-python-runtime 4.7.1 package. I compiled the Lua grammar as such: antlr4 -o lua -Dlanguage=Python3 -visitor Lua.g4

I'm parsing the factorial.lua example and would like to use the tree pattern matching capabilities. The first steps were as the described in many simple examples:

from antlr4 import *
from lua import *

def main():
    input = FileStream("factorial.lua")         # antlr4
    lexer = LuaLexer(input)
    stream = CommonTokenStream(lexer)           # antlr4
    parser = LuaParser(stream)
    tree = parser.chunk()

However, the next line:

    p = parser.compileParseTreePattern("<ID>", LuaParser.RULE_exp)

gives me the following error:

AttributeError: 'ParseTreePatternMatcher' object has no attribute 'compile'

I looked into the ParseTreePatternMatcher class, but found only compileTreePattern.

Am I missing something?

Thanks, Jaroslaw

ericvergnaud commented 6 years ago

Hi Not sure tree pattern matching was ever ported to Python If you need it may I suggest that you look into implementing it and submit a PR if you get it working?

Envoyé de mon iPhone

Le 1 mars 2018 à 22:23, jpicau notifications@github.com a écrit :

Dear ANTLR team,

I used conda to install the antlr-python-runtime 4.7.1 package. I compiled the Lua grammar as such: antlr4 -o lua -Dlanguage=Python3 -visitor Lua.g4

I'm parsing the factorial.lua example and would like to use the tree pattern matching capabilities. The first steps were as the described in many simple examples:

from antlr4 import *
from lua import *

def main():
   input = FileStream("factorial.lua")         # antlr4
   lexer = LuaLexer(input)
   stream = CommonTokenStream(lexer)           # antlr4
   parser = LuaParser(stream)
   tree = parser.chunk()

However, the next line:

   p = parser.compileParseTreePattern("<ID>", LuaParser.RULE_exp)

gives me the following error:

AttributeError: 'ParseTreePatternMatcher' object has no attribute 'compile'

I looked into the ParseTreePatternMatcher class, but found only compileTreePattern.

Am I missing something?

Thanks, Jaroslaw

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/antlr/antlr4/issues/2237 ----==_mimepart_5a9STLS 80cd8c1857_1cf72ae23ed12ec81065ec Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Dear ANTLR team,

I used conda to install the antlr-python-runtime 4.7.1 package.
I compiled the Lua grammar as such:
antlr4 -o lua -Dlanguage=Python3 -visitor Lua.g4

I'm parsing the factorial.lua example and would like to use the tree pattern matching capabilities.
The first steps were as the described in many simple examples:

from antlr4 import *
from lua import *

def main():
   input = FileStream("factorial.lua")         # antlr4
   lexer = LuaLexer(input)
   stream = CommonTokenStream(lexer)           # antlr4
   parser = LuaParser(stream)
   tree = parser.chunk()

However, the next line:

    p = parser.compileParseTreePattern("<ID>", LuaParser.RULE_exp)

gives me the following error:

AttributeError: 'ParseTreePatternMatcher' object has no attribute 'compile'

I looked into the ParseTreePatternMatcher class, but found only compileTreePattern.

Am I missing something?

Thanks,
Jaroslaw


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

jpicau commented 6 years ago

:D Thanks. I'd love to. However, I'm still too new to ANTLR. I keep working/digging and come back later ... Thanks, again.

shuizuo commented 6 years ago

I also have the problem and find that the Method compile was not Implemented in the Class ParseTreePatternMatcher. And so far have you solved the problem?

jpicau commented 6 years ago

Hi, my intention was to find all function definitons. I used Trees.findAllRuleNodes(...) instead. I don't know if this helps:

from antlr4 import *
from antlr4.tree.Trees import Trees
from lua import *

def main():

    input = FileStream("factorial.lua")         # antlr4
    lexer = LuaLexer(input)
    stream = CommonTokenStream(lexer)           # antlr4
    parser = LuaParser(stream)
    tree = parser.chunk()

    # find all function definitions
    names = Trees.findAllRuleNodes(tree, LuaParser.RULE_funcname)
    for name in names:
        funcname = name.getPayload()
        ...

Bye, Jaroslaw

shuizuo commented 6 years ago

@jpicau Thanks a lot, your code has given me some good ideas.

jpicau commented 6 years ago

Cool! You're welcome.

80avin-sw commented 2 years ago

@jpicau Why was this closed ? It doesn't seem like the original issue of compileParseTreePattern was ever resolved.