mysql ANTLR v4 语法文件中的语法错误
Syntax Error in mysql grammar file for ANTLR v4
我在以下几行中使用来自 ANTLR v4 repo here for parsing mysql in java. But I am getting an error in file MySqlLexer.g4 available here 的 Lexer 和 Parser:
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);
第一个错误:
syntax error: '{ MYSQLCOMMENT, ERRORCHANNEL }' came as a complete
surprise to me while matching rule preamble
第二个错误:(在"SPACE:"行)
syntax error: '(' came as a complete surprise to me while matching
rule preamble
syntax error: extraneous input ')' expecting SEMI while matching a
rule
这是我的 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 语法很陌生。请帮助。
谢谢:)
我从终端下载了 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
然后从这些语法生成词法分析器和解析器:
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
来生成词法分析器和解析器。
编辑
从你这里删除这个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>
我已经测试过了,效果非常好。
我在以下几行中使用来自 ANTLR v4 repo here for parsing mysql in java. But I am getting an error in file MySqlLexer.g4 available here 的 Lexer 和 Parser:
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);
第一个错误:
syntax error: '{ MYSQLCOMMENT, ERRORCHANNEL }' came as a complete surprise to me while matching rule preamble
第二个错误:(在"SPACE:"行)
syntax error: '(' came as a complete surprise to me while matching rule preamble
syntax error: extraneous input ')' expecting SEMI while matching a rule
这是我的 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 语法很陌生。请帮助。
谢谢:)
我从终端下载了 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
然后从这些语法生成词法分析器和解析器:
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
来生成词法分析器和解析器。
编辑
从你这里删除这个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>
我已经测试过了,效果非常好。