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

prednaska6

parent 8fa81a85
......@@ -10,7 +10,7 @@
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="testlang2020" target="1.11" />
<module name="testlang2020" target="13" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">
......
grammar Expr;
prog : expr;
expr : expr op=('*'|'/') expr #Mul
| expr op=('+'|'-') expr #Add
| NUMBER #Number
| '(' expr ')' #Paren
| IDENT #Ident
;
NUMBER : [0-9]+;
IDENT : [a-zA-Z]+;
WS : [ \r\t\n]+ -> skip;
\ No newline at end of file
grammar Expr;
prog: stat+;
stat: expr NL #print
| ID '=' expr NL #assign
| NL #blank
;
expr: expr op=('*'|'/') expr #mulDiv
| expr op=('+'|'-') expr #addSub
| INT #int
| ID #var
| '(' expr ')' #parens
;
MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
INT : [0-9]+ ;
ID : [a-zA-Z]+ ;
NL : '\r'? '\n' ;
WS : [ \t] -> skip ;
grammar Test1;
test : title question+;
title : STRING;
question : 'question' STRING '{' answer (',' answer)+ '}';
answer : '-' STRING # Incorrect
| '+' STRING # Correct
;
STRING : '"' .*? '"';
WS : [ \r\t\n]+ -> skip;
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.TestPrinter;
import java.io.IOException;
public class Main11ParserListener1 {
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();
TestPrinter printer = new TestPrinter();
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.TestPrinter;
import java.io.IOException;
public class Main12ParserListener2 {
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();
TestPrinter printer = new TestPrinter();
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 Main13ParserVisitor {
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;
}
}
"Test z astronomie iny"
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"
}
question "Je Zem pekna?" {
- "ano",
+ "nie"
}
Test z astronomie
Ktora planeta je obyvana?
- Merkur
- Venusa
+ Zem
- Mars
Kolko planet ma nasa sustava?
- 6
- 7
+ 8
- 9
Je Zem pekna?
+ ano
- nie
\ No newline at end of file
......@@ -15,7 +15,7 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_13">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
......
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