0

I made my programming language with Java and I chose ANTLR v4 to parse it. I don't have really any previous experience with ANTLR but I succeeded in implementing my grammar. what am'I supposed to enter on the console as expression ? how can test any arithmetic at least with ANTLR? For me, I need to parse programs with ANTLR but I begun with simple arithmetic expressions to know how it works this framework ANTLR. I have entered 6+9 for exemple and I got this error message : line 1:0 mismatched input '' expecting {IDENTIFIER, CONSTF}

How can I test an expression I have defined successfully ? Please could you help me because my goal is to parse full programs (StatementList in my case)

Could you please help me. Thanks :)

Here's my Grammar (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();
            }
        }
}
  • Do not connect ANTLRInputStream to an infinite stream (`System.in`). If you want to test your grammar test it with constant strings. If you want to read from `System.in` use a scanner or a readline-loop and pass the result of a scan/line to a new Parser. – CoronA Apr 19 '18 at 10:08
  • Thank You @CoronA I have tested with a String and even a file and I have the same message of error debut parsing : line 1:0 mismatched input '' expecting {IDENTIFIER, CONSTF} – Dorra Ben Khalifa Apr 19 '18 at 12:20

0 Answers0