| Refresh | Home EGTry.com

java implementation of parser rules


declare: TYPE ID ';'

public void declare() {
  Token type1=(Token)match(input,TYPE);
  Token id1=(Token)match(input, ID);
  Token semi=(Token)match(input,SEMI_ID);
}



repetition, kleene closure

block: '{' declare* '}'

 match '{'
 
 while the next token is the start token of declare()
    declare1=declare();
 end

 match '}'



alternation

classBody:
  fieldDef
  | methodDef
  ;

fieldDef:
   'int' ID ';' 
   ;

methodDef:
   'int' ID '{' '}'
   ;


void classBody() {
  if next tokens match: 'int' ID ';' 
  then fieldDef();

   if next tokens match: 'int' ID '{'
   then methodDef();

   otherwise, throw NoViableAltException
}