如何成功测试我的语法antlr4?

时间:2018-04-19 10:01:53

标签: java-8 antlr4

我用Java编写了我的编程语言,我选择了ANTLR v4来解析它。我之前没有任何ANTLR经验,但我成功地实现了我的语法。  什么我应该作为表达式进入控制台?如何至少用ANTLR测试任何算术?对我来说,我需要用ANTLR解析程序,但我从简单的算术表达开始,知道它如何在这个框架ANTLR中工作。 我输入了6 + 9的例子,我收到了这条错误消息:第1行:0输入不匹配''期待{IDENTIFIER,CONSTF}

如何测试我已成功定义的表达式? 请帮助我,因为我的目标是解析完整的程序(在我的情况下是StatementList)

你能帮我吗?谢谢:))

这是我的语法(grammaire.g4)

grammar Grammaire;
options {


  language = Java;}

@header{
import Expression.*;
import Statement.*;
import Boolean.*;
import Util.*;
import Number.*;}

@members{
AbstractClass left;
AbstractClass right;
Numbers b;
AbstractClassB cond;
StatementList then;
StatementList other;
StatementList body;
}

    expression returns [AbstractClass result]


    :     (left=baseexpr PLUS right=expression END) { $result = new Add(left,right);}
        | (left=baseexpr MINUS right=expression ){ $result = new Subtract(left, right);}
        | (left=baseexpr MULT right=expression)  { $result = new Multiply(left, right);}
        | (left=baseexpr DIV right=expression)  { $result = new Divide(left, right);}
        | (left=baseexpr) { $result=left;}
    ;

    baseexpr returns [Expression result ]

    : id=IDENTIFIER  { $result = new Variable( $id.getText());} 
    |floatt=CONSTF { System.out.println("#"); $result = new Constante (b);}

        ;

statementList
    : statement NEWLINE+ statementList                                                      # multiStmntList
    | statement NEWLINE+                                                                    # singleStmntList
;

statement returns [AbstractClassS result]

   :    (id=IDENTIFIER EQ right=expression) { $result = new Assign($result,$id.getText());} 

      | (IF cond=condition  then=thenBlock (ELSE other=elseBlock)?){$result = new IfThenElse(cond, then,other);}

     | (WHILE condition body){$result = new While(cond, body);}

    ;

    condition :  baseexpr ;

thenBlock: statementList;

elseBlock :statementList;

body: statementList;

boolexp returns [AbstractClassB result]

  :     (left=baseexpr EQ right=expression)  { $result = new EQUAL(left, right);}
        | (left=baseexpr GT right=expression) { $result = new GREATER(left, right);}
        | (left=baseexpr GE right=expression)  { $result = new GREATEREQUAL(left, right);}
        | (left=baseexpr LT right=expression)   { $result =new LESS(left, right);}
        | (left=baseexpr LE right=expression)   { $result = new LESSEQUAL(left, right);}

    ;


//============================================================================//
// LEXER
//============================================================================//
// Keywords

IDENTIFIER: (['A'-'Z']|['a'-'z']|'_')(['A'-'Z']|['a'-'z']|'_'|['0'-'9'])*;
CONSTI: ['0'-'9']*; 
CONSTF: ['0'-'9']*'.'['0'-'9']+; 

DIV: '/';
MULT:'*' ;
MINUS: '-';
PLUS: '+';
END: 'EOF';

TRUE  : 'true' ;
FALSE : 'false' ;

GT : '>' ;
GE : '>=' ;
LT : '<' ;
LE : '<=' ;
EQ : '==' ;
NEQ : '!=' | '<>' ;
NOT : '!' ;

ASSIGN : '=';
IF : 'if' ;
ELSE : 'else';
WHILE :'while';

LPAREN : '(' ;
RPAREN : ')' ;
LBRACKET : '{';
RBRACKET : '}';

NEWLINE


    : ('\n' | '\r')+  -> skip
;


//
// Whitespace and comments
//

WS  :  [ '\t' | ' ']+ -> skip

    ;

COMMENT


    :   '/*' .*? '*/' -> skip
    ;

LINE_COMMENT



    :   '//' ~[\r\n]* -> skip
;

(test.java)

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import Expression.*;
import Util.Environnement;

public class GrammaireRunner  {


    public static void main(String[] args)throws Exception {
        try {      
        ANTLRInputStream input = new ANTLRInputStream(System.in);
        System.out.println("debut parsing");


        GrammaireLexer lexer= new GrammaireLexer(input) ;
        Environnement env= new Environnement();
        CommonTokenStream tokens=new CommonTokenStream(lexer);
        GrammaireParser parser = new GrammaireParser (tokens);

        parser.setBuildParseTree(true);
    Expression expr= (Expression)parser.expression();
    System.out.println("fin parsing");

    System.out.println(expr);
    System.out.println(">> " + expr.evaluate(env));
}
        catch (Exception e) {
            if (e.getMessage() != null) {
                System.err.println(e.getMessage());
            } else {
                e.printStackTrace();
            }
        }
}

0 个答案:

没有答案