daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest May 16th, 2018 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * JavaCC template file created by SF JavaCC plugin 1.5.17+ wizard for JavaCC 1.5.0+
  3.  */
  4. options
  5. {
  6.   JDK_VERSION = "1.5";
  7.   static = true;
  8. }
  9.  
  10. PARSER_BEGIN(eg1)
  11. import java.util.*;
  12. public class eg1
  13. {
  14.   static Stack st;
  15.   public static void main(String args []) throws ParseException
  16.   {
  17.     st=new Stack();
  18.     eg1 parser = new eg1(System.in);
  19.    
  20.     while (true)
  21.     {
  22.       try
  23.       {
  24.       //System.out.print("Write a valid variable: ");
  25.       //eg1.input();
  26.       //System.out.println("Variable is valid.");
  27.       System.out.print("Write an expression: ");
  28.       eg1.calc();
  29.       for(int i=0;i<st.size();i++){
  30.             System.out.println(st.get(i));
  31.         }
  32.     }catch(Exception e)
  33.     {
  34.       System.out.println("Variable not valid");
  35.     }
  36.      
  37.     }
  38.   }
  39. }
  40.  
  41. PARSER_END(eg1)
  42.  
  43. SKIP :
  44. {
  45.   " "
  46. | "\r"
  47. | "\t"
  48. | "\n"
  49. }
  50.  
  51. TOKEN : /* KEYWORDS */
  52. {
  53.   < PLUS : "+" >
  54. | < MINUS : "-" >
  55. | < MULTIPLY : "*" >
  56. | < DIVIDE : "/" >
  57. | < EQUALS : "=" >
  58. | < TYPE : "BYTE" | "SHORT" | "INT" | "LONG" | "FLOAT" | "DOUBLE" | "BOOLEAN" | "CHAR" >
  59. | < EOS : ";" >
  60.  
  61. }
  62. /* LITERALS */
  63.  
  64. TOKEN :
  65. {
  66.   < INTEGER_LITERAL:
  67.         <DECIMAL_LITERAL> (["l","L"])?
  68.       | <HEX_LITERAL> (["l","L"])?
  69.       | <OCTAL_LITERAL> (["l","L"])?
  70.   >
  71. |
  72.   < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
  73. |
  74.   < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
  75. |
  76.   < #OCTAL_LITERAL: "0" (["0"-"7"])* >
  77. |
  78.   < FLOATING_POINT_LITERAL:
  79.         <DECIMAL_FLOATING_POINT_LITERAL>
  80.       | <HEXADECIMAL_FLOATING_POINT_LITERAL>
  81.   >
  82. |
  83.   < #DECIMAL_FLOATING_POINT_LITERAL:
  84.         (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
  85.       | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
  86.       | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
  87.       | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]
  88.   >
  89. |
  90.   < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
  91. |
  92.   < #HEXADECIMAL_FLOATING_POINT_LITERAL:
  93.         "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
  94.       | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
  95.   >
  96. |
  97.   < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
  98. |
  99.   < CHARACTER_LITERAL:
  100.       "'"
  101.       (   (~["'","\\","\n","\r"])
  102.         | ("\\"
  103.             ( ["n","t","b","r","f","\\","'","\""]
  104.             | ["0"-"7"] ( ["0"-"7"] )?
  105.             | ["0"-"3"] ["0"-"7"] ["0"-"7"]
  106.             )
  107.           )
  108.       )
  109.       "'"
  110.   >
  111. |
  112.   < STRING_LITERAL:
  113.       "\""
  114.       (   (~["\"","\\","\n","\r"])
  115.         | ("\\"
  116.             ( ["n","t","b","r","f","\\","'","\""]
  117.             | ["0"-"7"] ( ["0"-"7"] )?
  118.             | ["0"-"3"] ["0"-"7"] ["0"-"7"]
  119.             )
  120.           )
  121.       )*
  122.       "\""
  123.   >
  124. }
  125.  
  126. TOKEN :
  127. {
  128.     < IDENTIFIER : ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","0"-"9","_"] )* >
  129. }
  130. /*FOR EXPRESSION HANDLING */
  131.  
  132.  
  133.  
  134. void input() :
  135. {
  136. Token t1; String name;
  137. Token t2; String type;
  138. }
  139. {
  140.  t1=< TYPE >
  141.  {type=t1.image.toString();}
  142.  {System.out.println("type="+type); }
  143.  t2=<IDENTIFIER>
  144.  < EOS >
  145.  {name=t2.image.toString();}
  146.  {System.out.println("variable name="+name); }
  147. }
  148.  
  149. int calc() :
  150. {}
  151. {
  152.  
  153.  
  154.   sum() ";"
  155.   {
  156.     return 0;
  157.   }
  158. |
  159.  
  160. ";"
  161.   {
  162.     return 1;
  163.   }
  164. }
  165.  
  166.  
  167. void sum() :
  168. {Token t;}
  169. {
  170.   term()
  171.   (
  172.     (
  173.       t=< PLUS >{st.add(t.image);}
  174.     | t=< MINUS > {st.add(t.image);}
  175.    
  176.     )
  177.     term()
  178.   )*
  179. }
  180.  
  181. void term() :
  182. {Token t;}
  183. {
  184.   unary()
  185.   (
  186.     (
  187.       t=< MULTIPLY > {st.add(t.image);}
  188.     | t=< DIVIDE > {st.add(t.image);}
  189.    
  190.     )
  191.     unary()
  192.   )*
  193. }
  194.  
  195. void unary() :
  196. {Token t;}
  197. {
  198.   t=< MINUS >
  199.   {st.add(t.image);  } element()
  200. | element()
  201. }
  202.  
  203. void element() :
  204. {Token t;}
  205. {
  206.   t=< INTEGER_LITERAL >
  207.   {
  208.     st.add(t.image);
  209.   }
  210. | < IDENTIFIER >
  211. | "(" sum() ")"
  212.  
  213. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top