ANTLR - How to determine which parse tree "best matches" some code

I am creating a program with ANTLR where I ask the user to enter some Java code and it splashes out the equivalent C # code. In my program, I ask the user to enter Java code and then parse it. So far, I assumed that they would introduce something that would be parsed as a valid compilation unit by itself, for example. sort of

package foo;

class A { ... }
class B { ... }
class C { ... }

However, this is not always the case. They can simply enter the code from within the class:

public void method1() {
    ...
}

public void method2() {
    ...
}

Or inside the method:

System.out.print("hello ");
System.out.println("world!");

Or even just an expression:

context.getSystemService(Context.ACTIVITY_SERVICE)

, parser.compilationUnit(), , . , parser.expression() parser.blockStatements(). . , ?

+4
2

, , , .

, , .

, ​​ , . node, , .

, ; BailErrorStrategy, .

+1

Swiftify . - Objective-C Swift, .

open-source ObjectiveC. :

  • Objective-C :
    • translationUnit
    • implementationDefinitionList
    • interfaceDeclarationList
    • expression
    • compoundStatement
  • , .
  • .
  • location, .

Demo

, :

:

+1

Source: https://habr.com/ru/post/1691394/


All Articles