我正在使用来自ANTLR v4 repo 这里的Lexer和Parser来解析java中的mysql。但是,我在以下几行的MySqlLexer.g4可用这里文件中遇到了一个错误:
lexer grammar MySqlLexer;
channels { MYSQLCOMMENT, ERRORCHANNEL }
// SKIP
SPACE: [ \t\r\n]+ -> channel(HIDDEN);
SPEC_MYSQL_COMMENT: '/*!' .+? '*/' -> channel(MYSQLCOMMENT);
COMMENT_INPUT: '/*' .*? '*/' -> channel(HIDDEN);
LINE_COMMENT: (
('-- ' | '#') ~[\r\n]* ('\r'? '\n' | EOF)
| '--' ('\r'? '\n' | EOF)
) -> channel(HIDDEN); 第一个错误:
语法错误:“{ MYSQLCOMMENT,ERRORCHANNEL }”在匹配规则序言时给我一个完全的惊喜
第二个错误:(在“空格:”行)
语法错误:“(”在匹配规则序言时,我完全感到惊讶“) 语法错误:外来输入‘)在匹配规则时期望半个
下面是我的pom.xml,用于检查我正在使用的版本:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>autoGem</groupId>
<artifactId>autoGem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>autoGem</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<antlr4.visitor>true</antlr4.visitor>
<antlr4.listener>true</antlr4.listener>
<target.jvm>1.6</target.jvm>
<antlr.version>4.7.1</antlr.version>
<antlr4test-maven-plugin.version>1.10</antlr4test-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${target.jvm}</source>
<target>${target.jvm}</target>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<compilerArguments>
<Xlint />
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/antlr4</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.3</version>
<configuration>
<includes>
<include>MySqlLexer.g4</include>
<include>MySqlParser.g4</include>
</includes>
<visitor>true</visitor>
<listener>true</listener>
</configuration>
<executions>
<execution>
<id>antlr4</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<versionRange>[4.0,)</versionRange>
<goals>
<goal>antlr4</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>我对ANTLR语法非常陌生。请帮帮忙。
谢谢:)
发布于 2018-04-08 08:46:37
我从终端下载了ANTLR4 JAR:
wget http://www.antlr.org/download/antlr-4.7.1-complete.jar并下载了语法:
wget https://raw.githubusercontent.com/antlr/grammars-v4/master/mysql/MySqlLexer.g4
wget https://raw.githubusercontent.com/antlr/grammars-v4/master/mysql/MySqlParser.g4然后从这些语法中生成lexer和解析器:
java -cp antlr-4.7.1-complete.jar org.antlr.v4.Tool MySqlLexer.g4
java -cp antlr-4.7.1-complete.jar org.antlr.v4.Tool MySqlParser.g4换句话说,它工作得很好。你一定做错什么了。我猜您正在使用ANTLR3 Tool来生成lexer和解析器。
编辑
把这个从你的pom.xml中删除
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.3</version>
<configuration>
<includes>
<include>MySqlLexer.g4</include>
<include>MySqlParser.g4</include>
</includes>
<visitor>true</visitor>
<listener>true</listener>
</configuration>
<executions>
<execution>
<id>antlr4</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>并将其替换为:
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<configuration>
<arguments>
<argument>-visitor</argument>
</arguments>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>我已经测试过了,而且效果很好。
https://stackoverflow.com/questions/49715171
复制相似问题