Sunday, May 18, 2014

Propositional Logic Grammar Implementation in ANTLR


Here we have Propositional Logic BNF Grammar. We will implement this grammar in ANTLR project.


BNF of propositional logic grammar

We want to enter one or more statement. So we declare that prog rule is stat+.

prog  :  stat+  ;
 We want to finish the sentence with newline and user can just enter just newline character. Newline is a non termina so we declare newline with upper cases. Rules are written wth lower cases.

stat  :   sentence NEWLINE  | NEWLINE  ;
 Sentence may be atomcsentence or complexsentence.
sentence  :  atomicsentence  | complexsentence  ; 
Atomicsentence may be true value or false value or Symbol.
atomicsentence  :  'true'  | 'false'  | SYMBOL  ;
Symbol may be q or r or p value. 
SYMBOL  :  'q'  | 'r'  | 'p'  ;
Complexsentence may be not (~)sentence or (sentence && sentence) or (sentence ||sentence) or (sentence -> sentence ) or (sentence <-> sentence).

complexsentence  :  '~' sentence  | '(' sentence  (    '&&'    | '||'    | '->'    | '<->'  )  sentence ')'  ;

NEWLINE
  :
  '\n'+
  ;

WS
  :
  (
    ' '
    | '\t'
  )+
 
   {
    $channel = HIDDEN;
   }
  ;