Showing posts with label create an antlr project. Show all posts
Showing posts with label create an antlr project. Show all posts

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;
   }
  ;


CREATING ANTLR PROJECT


  1.  Run Eclipse.
  2. File – New – Java Project. (Figure 1)



    Figure 1
  3. Write Project Name – Click Finish button. (Figure 2)



    Figure 2

  4. Right Click over Project – Configure – Convert to ANTLR Project. (Figure 3)



    Figure 3

  5. Right click over src director under your Project. New – Other. (Figure 4)



    Figure 4

  6. Select ANTLR – Combined Grammar – Next. (Figure 5)



    Figure 5

  7. Write grammar Name (for ex. T). Click Finish button. (Figure 6)



    Figure 6

  8. Write your grammar. Then when you save your grammer, TLexer.java, TParser.java files are generated automatically under antlr-generated directory. (Figure 7, 8)


    Figure 7



    Figure 8

  9. Right click over antlr-generated directory. Build Path – Use as Source Folder. (Figure 9)



    Figure 9

  10. After Step 9, Project will give errors. (Figure 10)



    Figure 10

  11. Right click over Project – Build Path – Configure Build Path. (Figure 11)



    Figure 11

  12. Click Add External JARs… button. Find antlr-3.3 jar which is located in advance. (Figure 12)



    Figure 12

  13. Write your Test. Java file under antlr-genereated directory. (Figure 13)



    Figure 13

  14.  Run Test file.  (Figure 14)



    Figure 14

  15. The command is correct. So in the console there is no error message. (Figure 15)



    Figure 15

  16. If we don’t add ‘;’ at the end of the command, it will give error in the console. (Figure 16)



    Figure 16

TESTING AND GETTING PARSER TREES

  1. Open grammar file. Then Click interpreter tab. (Figure 17)



    Figure 17

  2. In opened screen, select rule, then write test command. Then click play button. Parse tree will be displayed. (Figure 18)



    Figure 18

  3. Example of wrong command. (Figure 19)



    Figure 19

  4. Click Railroad View tab. (Figure 20)



    Figure 20

  5. You can save this diagrams as jpeg or png or html pages. (Figure 21)


    Figure 21

1REFERENCES

[1] T. Parr, The Definitive ANTLR Reference, Building Domain-Specific Languges, Dallas, Texas : The Pragmatic Bookshelf , 2007