antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.12k stars 3.69k forks source link

In Python3 Parser I am not able to get values of variable , function and class #2555

Closed DivyaSrivastava45 closed 2 years ago

DivyaSrivastava45 commented 2 years ago
  1. for Variables I am using atom_expr and atom but it is giving the values which are not considered as variable in python
  2. for Functions which are not declared using def keyword , or e.g call functions no grammar for it ? please help me regarding this
kaby76 commented 2 years ago

Which Python3 grammar? There are many, https://github.com/antlr/grammars-v4/tree/master/python

DivyaSrivastava45 commented 2 years ago

I have used grammar folder --> grammars-v4/python/python3 generated grammar using there two files

  1. python3lexer.g4
  2. python3parser.g4 and used
  3. python3lexerbase.java file inside java folder
kaby76 commented 2 years ago

Do you have a Python3 input file that demonstrates the problem so as I can then generate the parse tree and discuss it?

DivyaSrivastava45 commented 2 years ago

yeah we can discuss it on public repo https://github.com/rahulsain/Hackerrank_30daysOFcode.git
using 30daysOFcode_Python

kaby76 commented 2 years ago
DivyaSrivastava45 commented 2 years ago

Thank you for your help i understand the process but I never used xpath could you give me any source from where I can undertand xpath concept.

kaby76 commented 2 years ago

XPath is a expression language used to navigate around a tree. See wikipedia.The W3Schools tutorial is okay. Also see my cheatsheet page, and Antlr's XPath implementation in the runtime. XPath has been around for decades, but it's a very specialized language. It came about because people were using XML, which is just another way to represent a tree, but needed a way to fetch data out of that tree.

The main point of my previous comment is that you need to consider the context of a particular node in the parse tree. You can't just pick off "atom" nodes and say that those are the identifiers.

If you are using an Antlr Visitor or Listener to find identifiers, then in the VisitAtom() method, you will need to navigate around the parse tree and verify the context of the "atom" node. You might be able to call XPath in the visitor method to do that, or just use the generated methods Antlr provides for your language.

So, for the expression '//atom_expr/atom[not(following-sibling::trailer/OPEN_PAREN)]/NAME/text()', this means "find an atom_expr node anywhere in the tree ('//'). At the 'atom_expr' node candidate, find a child labeled 'atom'. If there isn't one, then this 'atom_expr' doesn't contain an identifier. Then, get the sibling of the 'atom' node. Verify that it does not a child named 'trailer'. If it is, then this 'atom_expr' doesn't contain an identifier. ........"

DivyaSrivastava45 commented 2 years ago

Thanks you . After Parsing I am getting " extraneous input ' ' expecting" at the end of files if there is an whitespace and because of this it is throwing syntax error is there anyway to handle this ?