Skip to content

Commit

Permalink
Merge pull request #3 from romildo/maven
Browse files Browse the repository at this point in the history
Integrates CUP and JFlex
  • Loading branch information
romildo authored Oct 6, 2016
2 parents 38d152f + 4b47091 commit f199d82
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# dependency reduced pom.xml
dependency-reduced-pom.xml

# target
target/*

# backup files
*~

85 changes: 84 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,88 @@
<packaging>jar</packaging>

<name>EPlan compiler</name>
<url>http://maven.apache.org</url>
<url>https://github.com/romildo/eplan</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.github.vbmacher</groupId>
<artifactId>java-cup-runtime</artifactId>
<version>11b-20160615</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>de.jflex</groupId>
<artifactId>jflex-maven-plugin</artifactId>
<version>1.6.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.github.vbmacher</groupId>
<artifactId>cup-maven-plugin</artifactId>
<version>11b-20160615</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>main.LexerDriver</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>main.LexerDriver</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<configuration>
<finalName>uber-${project.artifactId}-${project.version}</finalName>
</configuration>
</plugin>
</plugins>
</build>

</project>
12 changes: 12 additions & 0 deletions src/main/cup/parser.cup
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package parse;

terminal LITINT;
terminal PLUS, MINUS, TIMES, DIV;
terminal LPAREN, RPAREN;

non terminal exp;

start with exp;

exp ::=
;
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package main;

import java.io.InputStreamReader;
import java.io.Reader;
import java_cup.runtime.Symbol;

import parse.Yylex;
import parse.sym;

public class LexerDriver {

public static void main(String[] args) throws Exception {
Reader input = new InputStreamReader(System.in);
Yylex lexer = new Yylex(input);
int tok;
Symbol tok;
do {
tok = lexer.yylex();
tok = lexer.next_token();
System.out.println(tok);
} while (tok != lexer.YYEOF);
} while (tok.sym != sym.EOF );
}

}
20 changes: 11 additions & 9 deletions src/main/jflex/lexer.jflex
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package parse;

import java_cup.runtime.Symbol;

%%

%public
%integer
%cup

%%

[ \t\f\n\r]+ { /* skip */ }

[0-9]+ ("." [0-9]+)? { return 10; }

"+" { return 21; }
"-" { return 22; }
"*" { return 23; }
"/" { return 24; }
[0-9]+ ("." [0-9]+)? { return new Symbol(sym.LITINT, yytext()); }

"(" { return 31; }
")" { return 32; }
"+" { return new Symbol(sym.PLUS); }
"-" { return new Symbol(sym.MINUS); }
"*" { return new Symbol(sym.TIMES); }
"/" { return new Symbol(sym.DIV); }
"(" { return new Symbol(sym.LPAREN); }
")" { return new Symbol(sym.RPAREN); }

. { System.out.printf("unexpected char |%s|\n", yytext()); }
1 change: 1 addition & 0 deletions tests/test-lexer-01.eplan
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12 * (453.2 - 140.3) / 2

0 comments on commit f199d82

Please sign in to comment.