代码之家  ›  专栏  ›  技术社区  ›  KCK

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

  •  1
  • KCK  · 技术社区  · 6 年前

    我正在使用ANTLR v4 repo中的Lexer和解析器 here 用于在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);  
    

    第一个错误:

    语法错误:“{MYSQLCOMMENT,ERRORCHANNEL}”是完整的 匹配规则前言时令我惊讶

    第二个错误:(在“空格:”行中)

    语法错误:“(”在匹配时让我大吃一惊 规则前言

    语法错误:在匹配时,外部输入“)”应为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 回复  |  直到 6 年前
        1
  •  3
  •   Bart Kiers    6 年前

    我从终端下载了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和parser:

    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>
    

    我已经测试过了,效果很好。