[关闭]
@Wayne-Z 2017-09-22T16:20:52.000000Z 字数 4715 阅读 1878

Report 1 for Compiler Construction

编译原理

Reading

Reviewed what has been learned last semester by reading the chapter 1&2 of book Compiler Construction.
Also read Pragmatic.The Definitive ANTLR 4 Reference book and then finished reading the short-course.
And this article has been published in Jianshu.

Prepare Experiment

I have configured the antlr work environment in two ways.

PowerShell Environment

Download the file from the website, and then move it to a directory for 3rd party java libraries, mine is

  1. C:\javalib

then write two scripts as following.
This is antlr4.bat

  1. @echo off
  2. java org.antlr.v4.Tool %*

And this is grun.bat

  1. @echo off
  2. java org.antlr.v4.gui.TestRig %*

After this two file were all put into system32,
Then we can test it by type command in PowerShell at any position

  1. antlr

and

  1. grun

It seems works fine, then test the example.

Example of Hello

In a test directory, put the following grammar inside file Hello.g4: Hello.g4

  1. // Define a grammar called Hello
  2. grammar Hello;
  3. r : 'hello' ID ; // match keyword hello followed by an identifier
  4. ID : [a-z]+ ; // match lower-case identifiers
  5. WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

type

  1. antlr4 Hello.g4

we can see 6 new files named:

  • Hello.tokens
  • HelloBaseListener.java
  • HelloListener.java
  • HelloLexer.java
  • HelloLexer.tokens
  • HelloParser.java

And then compile and test it, I meet thie bug

  1. PS D:\tution\解释器构造\test1> javac *.java
  2. PS D:\tution\解释器构造\test1> grun hello r -tree
  3. Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
  4. Can't load Hello as lexer or parser

It just seems I forgot to add'.' into the classpath. After add'.' to the class path, it just works well.

IDEA Environment

Just add plugins of antlr4 into this IDE and we can work with it.
create a new file named hello.g4, and then write the same grammer shown above, right click r, choose to test rule r. Then we can see the picture below.
image_1bqcmad1gvhtq7budl1pl21jc49.png-482.6kB


Calculator

thoughts

Change the thoughts used before, after reading blogs and materials, I found that what I need to do is just define a completed grammar, and then try to to search the abstract grammar tree, and then I can get the calculated outcome.

Grammar

Since the grammar used before is not well-defined, I tried several grammars, and I found a well-defined grammar like below. The discussion of grammars can be found in the foreum(Yeah, it is me :) ).

  1. grammar Calc;
  2. options {
  3. language=Java;
  4. output=AST;
  5. ASTLabelType=CommonTree;
  6. }
  7. // PARSER RULES
  8. prog : ( ( assign | out ) SEMICOLON)+
  9. ;
  10. assign : VAR EQUA expr # assignment
  11. ;
  12. out : (PRINT '('expr ')' )+ # printExpr
  13. ;
  14. expr : expr op=(MULT | DIV) expr # MulDiv
  15. | expr op=(MINUS |PLUS) expr # AddSub
  16. | NUMBER # number
  17. | VAR # varaible
  18. | '(' expr ')' # parens
  19. ;
  20. //LEXER RULES
  21. PRINT : 'print';
  22. NUMBER : INT | FLOAT ;
  23. VAR : [a-zA-Z][a-zA-Z0-9]*;
  24. WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ ->skip ;
  25. INT : [0-9] | [1-9][0-9]+ ;
  26. FLOAT : [0-9]+ POINT [0-9]+ ;
  27. PLUS : '+' ;
  28. MINUS : '-' ;
  29. MULT : '*' ;
  30. DIV : '/' ;
  31. SEMICOLON : ';' ;
  32. EQUA : '=' ;
  33. POINT : '.' ;

Parse-Tree Visitor

The symbol '#' is important, like '# assignment' in line 12, this lable let the antlr creater different visit methods for each rule, and we will override these methods to implement the calculator.
When generate java code using antlr, we should add -no-listener option like since we do not need the listener for now. In command line, the commond is like this.

  1. antlr4 -no-listener -visitor Calc.g

In idea, we should set the antlr generation configuration like the picture below
image_1bqk6ek60v73g224961l011acj9.png-57.2kB
choose to generate the parse tree vistor.
After generated, we can find that in the CalcBaseVisitor.java, there
calc-1.PNG-54kB
Then we create our own class file to execute the calculate. Like the function shown below:

  1. //Still need a hashmap to record all the variables to check if they are used before declared.
  2. Map<String, BigDecimal > memory = new HashMap<String,BigDecimal>();
  3. public BigDecimal visitAssignment(CalcParser.AssignmentContext ctx) {
  4. String var = ctx.VAR().getText();
  5. BigDecimal value = visit(ctx.expr());
  6. memory.put(var, value);
  7. return value;
  8. }

I tried to use the BigDecimal class to fix the precision problem of the float number, but for now I still can not fix it out. And we can see all I did in this function and others is just use the ctx created already , and visit it, do calculation. And we also need to add some codes to check bugs like divition of zero etc.

Main Function

The most important code in the main function is shown like below

  1. // This method seems to be deprecated, but still can be used for now
  2. ANTLRInputStream input = new ANTLRInputStream(is);
  3. // create a lexer
  4. CalcLexer lexer = new CalcLexer(input);
  5. // record all the tokens
  6. CommonTokenStream tokens = new CommonTokenStream(lexer);
  7. // do parse work to tokens
  8. CalcParser parser = new CalcParser(tokens);
  9. // create Parse-Tree
  10. ParseTree tree = parser.prog();
  11. // visit the Parse Tree
  12. EvalVistor eval = new EvalVistor();
  13. eval.visit(tree);

Test

After all of whork mentioned above, we finally create a calculator support decimal calculation. Then we can test use the test cases used in last semester
In test.in

  1. a=(10.44*356+1.28)/2+1024*1.6;
  2. b=a*2-a/2;
  3. c123=a+b*b/5-(a-a*2)/b;
  4. print(a);
  5. print(b);
  6. print(c123);
  7. print(1+2-3*4/5);

The output is like below
output for test.in
And error1.in, it miss a semicolon

  1. a=(10.44*356+1.28)/2+1024*1.6;
  2. b=a*2-a/2
  3. print(a);
  4. print(b);

The output is like below output for error1.in

error2.in divid zero

  1. a=(10.44*356+1.28)/2-1024*1.6;
  2. print(a);
  3. print(1/0);

The output is like below
outfor error2.in

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注