Sunday, May 18, 2014

English Sentence Example ANTLR Project


We will write a grammar that defines rule of English sentence. But we will have additional conditions. 
  • Sentence in English contains noun phrase and verb phrase.
  • Noun phrase contains adjective phrase and noun or just noun or e which shows empty.
  • Adjective phrase contains adjective phrase and adjective or e.
  • Verb phrase containd verb and noun phrase.
  • Verb : contains at least one  just lower case .
  • Adjective : contains at least one just upper case.
  • Noun : contains the first letter upper the others are lower cases.

When we write our grammar we will use White space terminal to specify White space should be used in where. We can say that ignore when White space is seen  but when we want to draw parser tree dynamically interpreter draws wrong but rule Works correct. So to see correct parser tree we will not ignore White  space. 


If a terminal goes to empty we don’t need to declare empty. We we use that terminal we specify it with question mark that means that terminal is used 0 or one time.

Now we declare s : will contain np and WS in paranthesis. And np can go  empty so we write a question mark outside of the paranthes. Then s contains verb phrase. If we want to use java code we can open curly bracet and between them we may write java codes. Here we display sentence. Use dolar sign to specify variables or terminal or non terminals.

s  :  (np WS)? vp              {               if ($np.text != null)                    System.out.print($np.text + " ");               System.out.println($vp.text);              }  ;

First we check noun phrase is null or not. If not we display it’s text. Then we display vp.

Noun phrase contains adjective phrase and noun. Adjective phrases can go empty. And it has left recursion. Antlr doesn’t support left recursion. So  we will use star that means we can use ap zero or more times.
Ap contains just adjective.
np  :  (ap WS)* N  ;
ap  :  A  ;
Verb phrase contains verb then noun phrase. Again noun phase can go empty so we will use again question mark.

vp  :  V (WS np)?  ;

Adjective contains at least one upper case so we will use +. It means at least 1 or more.
A  :  ('A'..'Z')+  ;
Noun will begin with  one beginning upper letter but the others if exist will be lower. So we use . to show one big character, for others we will use star.
N
  :
  ('A'..'Z') . ('a'..'z')*
  ;
Verb will be at least one lower case.
V  :  ('a'..'z')+  ;
White space will be space or tab.
WS  :  (    ' '    | '\t'  )+  ;

Now we write our Test class. First we take sentence from user through JOptionPane. It returns string. But we need to return it Input Stream. Because ANTLRInputStream accepts Input Stream. InputStream is abstract class so we cannot create an instance in this type. We will create ByteArrayInputStream. It takes byte. String class has a getbyte method. So we can use it.
String userin = JOptionPane.showInputDialog(null,                           "Please enter English sentence");              InputStream userinn = new ByteArrayInputStream(userin.getBytes());           
             ANTLRInputStream input = new ANTLRInputStream(userinn);

Then we will create a lexer instance by passing input as parameter.
// Create an ExprLexer that feeds from that stream             EnglishGrammarLexer lexer = new EnglishGrammarLexer(input);

Then we will create a tokens instance by passing lexer instance as parameter.
// Create a streams of tokens fed by the lexer             CommonTokenStream tokens = new CommonTokenStream(lexer);

Then we will create a parser instance by passing tokens instance as parameter.
// Create a parser that feeds off the token stream             EnglishGrammarParser parser = new EnglishGrammarParser(tokens);

Then using parser we will call our rule s.
// Begin parsing at rule s              parser.s();

No comments:

Post a Comment