ic.doc.ipandora.parser.fol.AstGenerator.java Source code

Java tutorial

Introduction

Here is the source code for ic.doc.ipandora.parser.fol.AstGenerator.java

Source

/*******************************************************************************
 *  Copyright (C) 2014 Simon Apen-Sadler
 *  
 *  This file is part of iPandora.
 *
 *     iPandora is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     iPandora is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with iPandora.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package ic.doc.ipandora.parser.fol;

import java.util.ArrayList;
import java.util.List;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import ic.doc.ipandora.folformula.Conjunction;
import ic.doc.ipandora.folformula.Constant;
import ic.doc.ipandora.folformula.Disjunction;
import ic.doc.ipandora.folformula.Equality;
import ic.doc.ipandora.folformula.Exists;
import ic.doc.ipandora.folformula.False;
import ic.doc.ipandora.folformula.FirstOrderLogicFormula;
import ic.doc.ipandora.folformula.ForAll;
import ic.doc.ipandora.folformula.Function;
import ic.doc.ipandora.folformula.IfAndOnlyIf;
import ic.doc.ipandora.folformula.Implication;
import ic.doc.ipandora.folformula.Negation;
import ic.doc.ipandora.folformula.Predicate;
import ic.doc.ipandora.folformula.Proposition;
import ic.doc.ipandora.folformula.True;
import ic.doc.ipandora.parser.fol.FormulaParser.ValueContext;

public class AstGenerator extends FormulaBaseVisitor<FirstOrderLogicFormula> {

    public static FirstOrderLogicFormula generateAst(String line) {
        ANTLRInputStream input = new ANTLRInputStream(line);
        FormulaLexer lexer = new FormulaLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        FormulaParser parser = new FormulaParser(tokens);
        parser.setErrorHandler(new BailErrorStrategy());

        ParseTree tree;

        try {
            tree = parser.prog();
        } catch (Exception e) {
            return null;
        }

        return new AstGenerator().visit(tree);
    }

    @Override
    public FirstOrderLogicFormula visitParen(FormulaParser.ParenContext ctx) {
        return visit(ctx.subformula());
    }

    @Override
    public FirstOrderLogicFormula visitNegation(FormulaParser.NegationContext ctx) {
        return new Negation(visit(ctx.subformula()));
    }

    @Override
    public FirstOrderLogicFormula visitConjunction(FormulaParser.ConjunctionContext ctx) {
        return new Conjunction(visit(ctx.subformula(0)), visit(ctx.subformula(1)));
    }

    @Override
    public FirstOrderLogicFormula visitDisjunction(FormulaParser.DisjunctionContext ctx) {
        return new Disjunction(visit(ctx.subformula(0)), visit(ctx.subformula(1)));
    }

    @Override
    public FirstOrderLogicFormula visitQuantification(FormulaParser.QuantificationContext ctx) {
        if (ctx.quantifier.getText().equals("$") || ctx.quantifier.getText().equals("FORALL")) {
            return new ForAll((Constant) visit(ctx.value()), visit(ctx.subformula()));
        } else if (ctx.quantifier.getText().equals("E") || ctx.quantifier.getText().equals("EXISTS")) {
            return new Exists((Constant) visit(ctx.value()), visit(ctx.subformula()));
        } else {
            //TODO: error case
            return null;
        }
    }

    @Override
    public FirstOrderLogicFormula visitImplication(FormulaParser.ImplicationContext ctx) {
        return new Implication(visit(ctx.subformula(0)), visit(ctx.subformula(1)));
    }

    @Override
    public FirstOrderLogicFormula visitIfAndOnlyIf(FormulaParser.IfAndOnlyIfContext ctx) {
        return new IfAndOnlyIf(visit(ctx.subformula(0)), visit(ctx.subformula(1)));
    }

    @Override
    public FirstOrderLogicFormula visitTerm(FormulaParser.TermContext ctx) {
        if (ctx.proposition() != null) {
            return visit(ctx.proposition());
        } else if (ctx.predicate() != null) {
            return visit(ctx.predicate());
        } else {
            // TODO: error case
            return null;
        }
    }

    @Override
    public FirstOrderLogicFormula visitProp(FormulaParser.PropContext ctx) {
        return new Proposition(ctx.PREDICATE_ID().getText());
    }

    @Override
    public FirstOrderLogicFormula visitBool(FormulaParser.BoolContext ctx) {
        if (ctx.BOTTOM() != null) {
            return new False();
        } else if (ctx.TOP() != null) {
            return new True();
        } else {
            // TODO: error case
            return null;
        }
    }

    @Override
    public FirstOrderLogicFormula visitConst(FormulaParser.ConstContext ctx) {
        return new Constant(ctx.getText());
    }

    @Override
    public FirstOrderLogicFormula visitEquals(FormulaParser.EqualsContext ctx) {
        return new Equality(visit(ctx.value(0)), visit(ctx.value(1)));
    }

    @Override
    public FirstOrderLogicFormula visitFunct(FormulaParser.FunctContext ctx) {
        String id = ctx.PREDICATE_ID().getText();
        List<ValueContext> constantContexts = ctx.argumentList().value();
        List<FirstOrderLogicFormula> arguments = new ArrayList<FirstOrderLogicFormula>();

        for (ValueContext constantContext : constantContexts) {
            arguments.add(visit(constantContext));
        }

        return new Function(id, arguments);
    }

    @Override
    public FirstOrderLogicFormula visitPred(FormulaParser.PredContext ctx) {
        String id = ctx.PREDICATE_ID().getText();
        List<ValueContext> constantContexts = ctx.argumentList().value();
        List<FirstOrderLogicFormula> arguments = new ArrayList<FirstOrderLogicFormula>();

        for (ValueContext constantContext : constantContexts) {
            arguments.add(visit(constantContext));
        }

        return new Predicate(id, arguments);
    }

}