Hi, I want to parse some lisp-like formulas given by the user into my own Java Formulas. I already wrote a grammer in antlr which is so easy that is should be correct, but now I do not know how to continue. How would you go from this? Create an AST and write a TreeParser or introduce formula constructors in the parser grammar directly?
Let me give some examples.
(in x X) should result in new Inclusion(x, X), where x and X are
variables, which are either newly created or retrieved from a map if
they have occurred elsewhere in the formula.
(word x Einführung) should result in new Inclusion(x, new
Predicate("Einführung")), with the same rule for x as above. Maybe I
should define a separate WordFormula instead of reusing Inclusion, but
that is another matter.
(=> formula1 formula2) should result in new Implication(formula1,
formula2), where of course f1 and f2 are to be parsed recursively.
And so on. Some rules for atomic formulas, and the usual stuff for the
logical connectors.
My grammar I have now looks as follows:
grammar fsq;
options {
language = Java;
}
tokens {
// LISP
OPEN = '(';
CLOSE = ')';
// labels
FUNCTION = 'fct';
CATEGORY = 'cat';
WORD = 'word';
LEMMA = 'lemma';
MORPHOLOGY = 'morph';
SENTENCE = 'sent';
// atomic
CONTAINMENT = 'in';
EQUALITY = '=';
PROPER_DOMINANCE = '>+';
IMMEDIATE_DOMINANCE = '>';
DOMINANCE = '>>';
IMMEDIATE_PRECEDENCE = '.';
PRECEDENCE = '..';
// unary
NEGATION = '!';
// binary
IMPLICATION = '->';
EQUIVALENCE = '<->';
// n-ary
DISJUNCTION = '|';
CONJUNCTION = '&';
// quantification
SECOND_ORDER_UNIVERSAL = 'A2';
SECOND_ORDER_EXISTENTIAL = 'E2';
FIRST_ORDER_UNIVERSAL = 'A1';
FIRST_ORDER_EXISTENTIAL = 'E1';
}
@header { package de.uni_tuebingen.sfb.lichtenstein.formulas.parsing; }
@lexer::header { package
de.uni_tuebingen.sfb.lichtenstein.formulas.parsing; }
@members {
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
/**
* One formula on each line.
*/
formulaList : ( formula NEWLINE? )* EOF ;
/**
* A formula is enclosed by braces.
*/
formula : OPEN body CLOSE ;
body : label | atomic | unary | binary | n_ary | quantor ;
// label formulas
label : labelHead WHITESPACE VARIABLE WHITESPACE LABEL ;
labelHead : FUNCTION | CATEGORY | WORD | LEMMA | MORPHOLOGY ;
// atomic relations
atomic : atomicHead WHITESPACE VARIABLE WHITESPACE VARIABLE ;
atomicHead : CONTAINMENT | EQUALITY | PROPER_DOMINANCE |
IMMEDIATE_DOMINANCE | DOMINANCE | IMMEDIATE_PRECEDENCE | PRECEDENCE ;
// unary junctors
unary : unaryHead formula ;
unaryHead : NEGATION ;
// binary junctors
binary : binaryHead formula formula ;
binaryHead : IMPLICATION | EQUIVALENCE ;
// n-ary junctors
n_ary : n_aryHead formula+ ;
n_aryHead : CONJUNCTION | DISJUNCTION ;
// quantors
quantor : quantorHead VARIABLE formula ;
quantorHead : FIRST_ORDER_EXISTENTIAL | FIRST_ORDER_UNIVERSAL |
SECOND_ORDER_EXISTENTIAL | SECOND_ORDER_UNIVERSAL ;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
WHITESPACE : ( '\t' | ' ' )+ ;
NEWLINE: ('\r'? '\n')+;
VARIABLE : ( DIGIT | LETTER )+ ;
fragment DIGIT : '0'..'9' ;
fragment LETTER : LOWERCASE | UPPERCASE ;
fragment LOWERCASE : 'a'..'z' ;
fragment UPPERCASE : 'A'..'Z' ;
LABEL : ~(')')+ ;
Other remarks most welcome also, of course!
Cheers, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
signature.asc
Description: OpenPGP digital signature
List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-address
