1

I am using Lexer and Parser from ANTLR v4 repo here for parsing mysql in java. But I am getting an error in file MySqlLexer.g4 available here at the following set of lines:

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:

syntax error: '{ MYSQLCOMMENT, ERRORCHANNEL }' came as a complete surprise to me while matching rule preamble

Second error: (in "SPACE:" line)

syntax error: '(' came as a complete surprise to me while matching rule preamble

syntax error: extraneous input ')' expecting SEMI while matching a rule

Here is my pom.xml for checking versions I am using:

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

I am very new to ANTLR grammar. Please Help.
Thanks :)

KCK
  • 2,015
  • 2
  • 17
  • 35
  • Looks like you’re using ANTLR v3 on a v4 grammar. – Bart Kiers Apr 08 '18 at 07:19
  • But the location I got this from is the repository of v4 grammars for antlr. I have given the link above. Know any fix for removing the above errors when using with v4? – KCK Apr 08 '18 at 07:31
  • Yes, that is what I said: you're using v4 grammars and are trying with ANTLR v3 to generate the lexer and parser from these v4 grammars. Check my answer. – Bart Kiers Apr 08 '18 at 08:49

1 Answers1

3

From the terminal, I downloaded the ANTLR4 JAR:

wget http://www.antlr.org/download/antlr-4.7.1-complete.jar

and downloaded the grammars:

wget https://raw.githubusercontent.com/antlr/grammars-v4/master/mysql/MySqlLexer.g4
wget https://raw.githubusercontent.com/antlr/grammars-v4/master/mysql/MySqlParser.g4

and then generated the lexer and parser from these grammars:

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

In other words: it works fine. You must be doing something wrong. My guess is you're using the ANTLR3 Tool to generate the lexer and parser.

EDIT

Remove this from you 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>

and replace it with this:

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

I've tested this, and it worked like a charm.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • I have added my pom.xml in the above question. requesting to hava a look in case anything is wrong in versions. – KCK Apr 08 '18 at 09:22