Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PARSER_BEGIN(ExpEval)
- import java.io.*;
- /** IPIPIPIPIPIPIP */
- public class ExpEval {
- public static void main(String args[]) {
- System.out.println("Reading from " + args[0]);
- ExpEval parser;
- try
- {
- parser = new ExpEval(new FileInputStream(args[0]));
- }
- catch(FileNotFoundException e)
- {
- System.out.println("File not found : " + args[0]);
- return;
- }
- try
- {
- SimpleNode n = parser.Start();
- n.dump("");
- System.out.println("Done.");
- }
- catch (Exception e) {
- System.out.println("Error : ");
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
- }
- PARSER_END(ExpEval)
- SKIP:
- {
- " " | "\t" | "\r" | "\n"
- }
- TOKEN: /* NUMBER */
- {
- <NUMBER: <NATURAL> <FLOATING> >
- | <#NATURAL: (["0"-"9"])+ >
- | <#FLOATING: ("." (["0"-"9"])+)? >
- }
- TOKEN: /* VARIABLE */
- {
- <VARIABLE: <LETTER> (<LETTER>|<DIGIT>)*>
- | <#LETTER: ["a"-"z", "A"-"Z"] >
- | <#DIGIT: ["0"-"9"] >
- }
- TOKEN: /* OPERATOR */
- {
- <PLUS: "+">
- | <MINUS: "-">
- | <TIMES: "*">
- | <DIVIDE: "/">
- | <MODULO: "%">
- }
- void Natural() : {Token t;}
- {
- t=<NUMBER> { jjtThis.val = t.image; }
- }
- void Number() : {}
- {
- (Minus())? Natural()
- }
- void Variable() : {Token t;}
- {
- t=<VARIABLE> { jjtThis.val = t.image; }
- }
- void Plus() : {Token t;}
- {
- t=<PLUS> {jjtThis.op = "+";}
- }
- void Minus() : {Token t;}
- {
- t=<MINUS> {jjtThis.op = "-";}
- }
- void Times() : {Token t;}
- {
- t=<TIMES> {jjtThis.op = "*";}
- }
- void Divide() : {Token t;}
- {
- t=<DIVIDE> {jjtThis.op = "/";}
- }
- void Modulo() : {Token t;}
- {
- t=<MODULO> {jjtThis.op = "%";}
- }
- void PlusOp(): {}
- {
- Plus() product()
- }
- void MinusOp(): {}
- {
- Minus() product()
- }
- SimpleNode Start(): {}
- {
- sum() <EOF>
- { return jjtThis; }
- }
- /* Somme = Produit, {(+|-), Produit} */
- void sum():
- {}
- {
- product() products()
- }
- void products():
- {}
- {
- (PlusOp() | MinusOp())*
- }
- void TimesOp(): {}
- {
- Times() term()
- }
- void DivideOp(): {}
- {
- Divide() term()
- }
- void ModuloOp(): {}
- {
- Modulo() term()
- }
- /* Produit = Terme, {(+|/|%), Terme} */
- void product():
- {}
- {
- term() terms()
- }
- void terms():
- {}
- {
- (TimesOp() | DivideOp() | ModuloOp())*
- }
- /* Terme = "(",Somme,")" | Nombre | Variable */
- void term():
- {}
- {
- "(" sum() ")" | Number() | Variable()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement