首页 文章

ANTLR v4的mysql语法文件中的语法错误

提问于
浏览
1

我正在使用来自ANTLR v4 repo here的Lexer和Parser来解析java中的mysql . 但是我在以下一行中的文件MySqlLexer.g4中出现错误here

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);

First error:

语法错误:'{MYSQLCOMMENT,ERRORCHANNEL}'在匹配规则前言时给我一个完全的惊喜

Second error: (in "SPACE:" line)

语法错误:'('匹配规则前导码语法错误时,我感到非常惊讶:无关输入')'在匹配规则时期望SEMI

这是我用于检查我正在使用的版本的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语法很新 . 请帮忙 .
谢谢 :)

1 回答

  • 3

    从终端,我下载了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>
    

    我测试了这个,它就像一个魅力 .

相关问题