diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..20c4263 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# parse with antlr +`antlr4 -package parser.grammar -o main/java/parser -no-listener -visitor grammar/Loop.g4` + diff --git a/grammar/Loop.g4 b/grammar/Loop.g4 deleted file mode 100644 index afbd3eb..0000000 --- a/grammar/Loop.g4 +++ /dev/null @@ -1 +0,0 @@ -grammar Loop; diff --git a/src/grammar/Loop.g4 b/src/grammar/Loop.g4 new file mode 100644 index 0000000..b71ffb7 --- /dev/null +++ b/src/grammar/Loop.g4 @@ -0,0 +1,17 @@ +grammar Loop; +start: prog EOF; +INT: [0-9]+ ; +VAR: ('x'|'y'|'z') INT; +expr: var=VAR '+' num=INT #Plus + | var=VAR '-' num=INT #Minus + ; +asrt: var=VAR '=' ex=expr ';' #Ex + | var=VAR '=' var=VAR ';' #Va + | var=VAR '=' num=INT ';' #Num + ; +prog: asrt #Asert + | 'LOOP' var=VAR 'DO' p=prog #Loop + | p1=prog p2=prog #ProgProg + ; +NEWLINE : [\r\n]+ -> skip; +LEER: ' ' -> skip; \ No newline at end of file diff --git a/src/main/java/compiler/grammar/Main.java b/src/main/java/compiler/grammar/Main.java new file mode 100644 index 0000000..62ddce8 --- /dev/null +++ b/src/main/java/compiler/grammar/Main.java @@ -0,0 +1,22 @@ +package compiler.grammar; + +import org.antlr.v4.runtime.ANTLRFileStream; +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTree; +import parser.grammar.LoopLexer; +import parser.grammar.LoopParser; + +import java.io.IOException; + +public class Main { + public static void main(String[] args) throws IOException { + ANTLRInputStream input = new ANTLRFileStream("src/main/java/compiler/grammar/test.txt"); + LoopLexer lexer = new LoopLexer(input); + CommonTokenStream tokens = new CommonTokenStream(lexer); + LoopParser parser = new LoopParser(tokens); + + ParseTree tree = parser.start(); + new MyVisitor().visit(tree); + } +} diff --git a/src/main/java/compiler/grammar/MyVisitor.java b/src/main/java/compiler/grammar/MyVisitor.java new file mode 100644 index 0000000..ff64746 --- /dev/null +++ b/src/main/java/compiler/grammar/MyVisitor.java @@ -0,0 +1,14 @@ +package compiler.grammar; + +import parser.grammar.LoopBaseVisitor; +import parser.grammar.LoopParser; + +public class MyVisitor extends LoopBaseVisitor { + @Override + public String visitPlus(LoopParser.PlusContext ctx) { + String var = ctx.var.getText(); + int varVal = 0; //get real value in future; + int num = Integer.parseInt(ctx.num.getText()); + return Integer.valueOf(varVal + num).toString(); + } +} diff --git a/src/main/java/compiler/grammar/test.txt b/src/main/java/compiler/grammar/test.txt new file mode 100644 index 0000000..9040582 --- /dev/null +++ b/src/main/java/compiler/grammar/test.txt @@ -0,0 +1,4 @@ +x0 = 5; +y0 = 10; +LOOP y0 DO + x0 = x0 + 1; \ No newline at end of file