Commit b3d2dc4e authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

Verzia4

parent b2e1667b
......@@ -7,6 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/antlr4" isTestSource="false" generated="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
......@@ -14,5 +15,6 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.7" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7" level="project" />
<orderEntry type="library" name="Maven: org.antlr:antlr4-runtime:4.7.1" level="project" />
</component>
</module>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: org.antlr:antlr4-runtime:4.7.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/antlr/antlr4-runtime/4.7.1/antlr4-runtime-4.7.1.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/antlr/antlr4-runtime/4.7.1/antlr4-runtime-4.7.1-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/antlr/antlr4-runtime/4.7.1/antlr4-runtime-4.7.1-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
......@@ -23,6 +23,12 @@
<artifactId>jackson-databind</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.1</version>
</dependency>
</dependencies>
<build>
......@@ -30,11 +36,28 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7.1</version>
<configuration>
<visitor>true</visitor>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
grammar Test1;
test : title question+;
title : STRING;
question : 'question' STRING '{' answer (',' answer)+ '}';
answer : '-' STRING # Incorrect
| '+' STRING # Correct
;
STRING : '"' .*? '"';
WS : [ \r\t\n]+ -> skip;
\ No newline at end of file
grammar Test2;
test : title question+;
title : STRING NL+;
question : STRING NL+ answer answer+ NL*;
answer : '-' STRING NL # Incorrect
| '+' STRING NL # Correct
;
STRING : [a-zA-Z0-9]~[-+\n]*;
NL : '\r'? '\n';
WS : [ \t]+ -> skip;
package sk.tuke.dsl.testlang.main;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.parser.Test1Lexer;
import sk.tuke.dsl.testlang.parser.Test1Parser;
import sk.tuke.dsl.testlang.parser.Test1ParserListener;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import java.io.IOException;
public class Main08ParserListener1 {
public static void main(String[] args) throws IOException {
Test1Lexer lexer = new Test1Lexer(CharStreams.fromFileName("test1.test"));
CommonTokenStream tokens = new CommonTokenStream(lexer);
Test1Parser parser = new Test1Parser(tokens);
ParseTree tree = parser.test();
ParseTreeWalker treeWalker = new ParseTreeWalker();
Test1ParserListener listener = new Test1ParserListener();
treeWalker.walk(listener, tree);
Test test = listener.getTest();
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
package sk.tuke.dsl.testlang.main;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.parser.Test2Lexer;
import sk.tuke.dsl.testlang.parser.Test2Parser;
import sk.tuke.dsl.testlang.parser.Test2ParserListener;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import java.io.IOException;
public class Main09ParserListener2 {
public static void main(String[] args) throws IOException {
Test2Lexer lexer = new Test2Lexer(CharStreams.fromFileName("test2.test"));
CommonTokenStream tokens = new CommonTokenStream(lexer);
Test2Parser parser = new Test2Parser(tokens);
ParseTree tree = parser.test();
ParseTreeWalker treeWalker = new ParseTreeWalker();
Test2ParserListener listener = new Test2ParserListener();
treeWalker.walk(listener, tree);
Test test = listener.getTest();
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
package sk.tuke.dsl.testlang.main;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import sk.tuke.dsl.testlang.parser.AnswerCountVisitor;
import sk.tuke.dsl.testlang.parser.Test2Lexer;
import sk.tuke.dsl.testlang.parser.Test2Parser;
import java.io.IOException;
public class Main10ParserVisitor {
public static void main(String[] args) throws IOException {
Test2Lexer lexer = new Test2Lexer(CharStreams.fromFileName("test2.test"));
CommonTokenStream tokens = new CommonTokenStream(lexer);
Test2Parser parser = new Test2Parser(tokens);
ParseTree tree = parser.test();
AnswerCountVisitor visitor = new AnswerCountVisitor();
visitor.visit(tree);
System.out.println(visitor.getAnswerCount());
}
}
package sk.tuke.dsl.testlang.parser;
public class AnswerCountVisitor extends Test2BaseVisitor<Void> {
private int answerCount;
@Override
public Void visitCorrect(Test2Parser.CorrectContext ctx) {
answerCount++;
return super.visitCorrect(ctx);
}
@Override
public Void visitIncorrect(Test2Parser.IncorrectContext ctx) {
answerCount++;
return super.visitIncorrect(ctx);
}
public int getAnswerCount() {
return answerCount;
}
}
package sk.tuke.dsl.testlang.parser;
import sk.tuke.dsl.testlang.model.Answer;
import sk.tuke.dsl.testlang.model.Question;
import sk.tuke.dsl.testlang.model.Test;
import java.util.ArrayList;
import java.util.List;
public class Test1ParserListener extends Test1BaseListener {
private List<Answer> answers;
private List<Question> questions;
private Test test;
@Override
public void enterTest(Test1Parser.TestContext ctx) {
questions = new ArrayList<>();
}
@Override
public void enterQuestion(Test1Parser.QuestionContext ctx) {
answers = new ArrayList<>();
}
@Override
public void exitCorrect(Test1Parser.CorrectContext ctx) {
Answer answer = new Answer(stripQuotes(ctx.STRING().getText()), true);
answers.add(answer);
}
@Override
public void exitIncorrect(Test1Parser.IncorrectContext ctx) {
Answer answer = new Answer(stripQuotes(ctx.STRING().getText()), false);
answers.add(answer);
}
@Override
public void exitQuestion(Test1Parser.QuestionContext ctx) {
Question question = new Question(stripQuotes(ctx.STRING().getText()),
answers.toArray(new Answer[answers.size()]));
questions.add(question);
}
@Override
public void exitTest(Test1Parser.TestContext ctx) {
test = new Test(stripQuotes(ctx.title().getText()),
questions.toArray(new Question[questions.size()]));
}
public Test getTest() {
return test;
}
private String stripQuotes(String quoted) {
return quoted.substring(1, quoted.length() - 1);
}
}
package sk.tuke.dsl.testlang.parser;
import sk.tuke.dsl.testlang.model.Answer;
import sk.tuke.dsl.testlang.model.Question;
import sk.tuke.dsl.testlang.model.Test;
import java.util.ArrayList;
import java.util.List;
public class Test2ParserListener extends Test2BaseListener {
private List<Answer> answers;
private List<Question> questions;
private Test test;
@Override
public void enterTest(Test2Parser.TestContext ctx) {
questions = new ArrayList<>();
}
@Override
public void enterQuestion(Test2Parser.QuestionContext ctx) {
answers = new ArrayList<>();
}
@Override
public void exitCorrect(Test2Parser.CorrectContext ctx) {
Answer answer = new Answer(ctx.STRING().getText().trim(), true);
answers.add(answer);
}
@Override
public void exitIncorrect(Test2Parser.IncorrectContext ctx) {
Answer answer = new Answer(ctx.STRING().getText().trim(), false);
answers.add(answer);
}
@Override
public void exitQuestion(Test2Parser.QuestionContext ctx) {
Question question = new Question(ctx.STRING().getText().trim(),
answers.toArray(new Answer[answers.size()]));
questions.add(question);
}
@Override
public void exitTest(Test2Parser.TestContext ctx) {
test = new Test(ctx.title().getText().trim(),
questions.toArray(new Question[questions.size()]));
}
public Test getTest() {
return test;
}
}
File added
"Test z astronomie 2"
question "Ktora planeta je obyvana?" {
- "Merkur",
- "Venusa",
+ "Zem",
- "Jupiter"
}
question "Kolko planet ma nasa sustava?" {
- "6",
- "7",
+ "8",
- "9"
}
question "Kolko planet nema nasa sustava?" {
- "osem",
- "eight",
- "8",
+ "9"
}
Test z astronomie
Ktora planeta je obyvana?
- Merkur
- Venusa
+ Zem
- Mars
Kolko planet ma nasa sustava?
- 6
- 7
+ 8
- 9
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment