ANTLR4:如何在可能用于查找起始规则的语法中查找根规则
ANTLR4: How to find the root rules in a gramar which maybe used to find the start rule
我一直在查看位于:
的语法
我一直在使用 Antlrworks 2 查看它们,但是我发现很难找到整个语法的起始规则。
我认为开始规则的定义是一个没有其他指针指向它的节点,有没有人有找到这些语法的开始规则的有效解决方案?
为了找到 root/start 规则,我实现了一个 ANTLR 树监听器,它创建了一个 ANTLR 语法中所有规则的邻接列表,并检查是否没有其他语法规则引用它。这将提示开始规则可能是什么。
为了运行,你将需要取自Antlr 4 grammar
的antlr语法
监听器实现如下:
(RootFinder.java)
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class RootFinder extends ANTLRv4ParserBaseListener {
private Set<String> rules = new HashSet<String>( );
public Map<String, Set<String>> adjacencyList = new LinkedHashMap<String, Set<String>>( );
public void enterParserRuleSpec(ANTLRv4Parser.ParserRuleSpecContext ctx)
{
rules = new HashSet<String>( );
}
public void exitParserRuleSpec(ANTLRv4Parser.ParserRuleSpecContext ctx)
{
if(ctx != null)
{
adjacencyList.put(ctx.RULE_REF().getText(), rules);
}
}
public void exitRuleref(ANTLRv4Parser.RulerefContext ctx) {
if(ctx != null)
{
rules.add(ctx.getText());
}
}
public List<String> leafNodes( )
{
List<String> leafs = new ArrayList<String>( );
for(Entry<String, Set<String>> entry : adjacencyList.entrySet())
{
if( entry.getValue().size() == 0)
{
leafs.add(entry.getKey());
}
}
return leafs;
}
public Set<String> rootNodes( )
{
Set<String> roots = new HashSet<String>( );
Collection<Set<String>> values = adjacencyList.values();
for(Entry<String, Set<String>> entry : adjacencyList.entrySet())
{
boolean found = false;
for(Set<String> vals : values)
{
if(vals.contains(entry.getKey()))
{
found = true;
}
}
if(found == false)
{
roots.add(entry.getKey());
}
}
return roots;
}
}
这是 运行 的主程序
(Main.java)
import java.io.IOException;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
public class Main {
public static void main(String[] args) throws IOException {
String filename = args[0];
ANTLRInputStream reader = new ANTLRFileStream(filename);
ANTLRv4Lexer lexer = new ANTLRv4Lexer(reader);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ANTLRv4Parser parser = new ANTLRv4Parser(tokenStream);
RootFinder rootfinder = new RootFinder();
parser.addParseListener(rootfinder);
ParseTree tree = parser.grammarSpec();
System.out.println(rootfinder.leafNodes());
System.out.println(rootfinder.rootNodes());
}
}
我一直在查看位于:
的语法我一直在使用 Antlrworks 2 查看它们,但是我发现很难找到整个语法的起始规则。
我认为开始规则的定义是一个没有其他指针指向它的节点,有没有人有找到这些语法的开始规则的有效解决方案?
为了找到 root/start 规则,我实现了一个 ANTLR 树监听器,它创建了一个 ANTLR 语法中所有规则的邻接列表,并检查是否没有其他语法规则引用它。这将提示开始规则可能是什么。
为了运行,你将需要取自Antlr 4 grammar
的antlr语法监听器实现如下:
(RootFinder.java)
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class RootFinder extends ANTLRv4ParserBaseListener {
private Set<String> rules = new HashSet<String>( );
public Map<String, Set<String>> adjacencyList = new LinkedHashMap<String, Set<String>>( );
public void enterParserRuleSpec(ANTLRv4Parser.ParserRuleSpecContext ctx)
{
rules = new HashSet<String>( );
}
public void exitParserRuleSpec(ANTLRv4Parser.ParserRuleSpecContext ctx)
{
if(ctx != null)
{
adjacencyList.put(ctx.RULE_REF().getText(), rules);
}
}
public void exitRuleref(ANTLRv4Parser.RulerefContext ctx) {
if(ctx != null)
{
rules.add(ctx.getText());
}
}
public List<String> leafNodes( )
{
List<String> leafs = new ArrayList<String>( );
for(Entry<String, Set<String>> entry : adjacencyList.entrySet())
{
if( entry.getValue().size() == 0)
{
leafs.add(entry.getKey());
}
}
return leafs;
}
public Set<String> rootNodes( )
{
Set<String> roots = new HashSet<String>( );
Collection<Set<String>> values = adjacencyList.values();
for(Entry<String, Set<String>> entry : adjacencyList.entrySet())
{
boolean found = false;
for(Set<String> vals : values)
{
if(vals.contains(entry.getKey()))
{
found = true;
}
}
if(found == false)
{
roots.add(entry.getKey());
}
}
return roots;
}
}
这是 运行 的主程序
(Main.java)
import java.io.IOException;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
public class Main {
public static void main(String[] args) throws IOException {
String filename = args[0];
ANTLRInputStream reader = new ANTLRFileStream(filename);
ANTLRv4Lexer lexer = new ANTLRv4Lexer(reader);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
ANTLRv4Parser parser = new ANTLRv4Parser(tokenStream);
RootFinder rootfinder = new RootFinder();
parser.addParseListener(rootfinder);
ParseTree tree = parser.grammarSpec();
System.out.println(rootfinder.leafNodes());
System.out.println(rootfinder.rootNodes());
}
}