Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Simple +/-/* expression language;
- parser evaluates constant expressions on the fly
- */
- package cup.example;
- import java_cup.runtime.*;
- import cup.example.Lexer;
- import java.io.IOException;
- import java.io.File;
- import java.io.FileInputStream;
- parser code {:
- protected Lexer lexer;
- :}
- /* define how to connect to the scanner! */
- init with {:
- ComplexSymbolFactory f = new ComplexSymbolFactory();
- symbolFactory = f;
- File file = new File("input.txt");
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- lexer = new Lexer(f,fis);
- :};
- scan with {: return lexer.next_token(); :};
- /* Terminals (tokens returned by the scanner). */
- terminal LCB, RCB, LSQB, RSQB, COMMA, COLON, IDENTIFIER;
- terminal Integer NUMBER;
- terminal Boolean BOOLEAN;
- terminal String STRING;
- /* Non terminals */
- // non terminal expr_list;
- non terminal Integer value; // used to store evaluated subexpressions
- /* Precedences */
- precedence left LCB, RCB, LSQB, RSQB, COMMA, COLON;
- precedence left NUMBER, BOOLEAN, STRING;
- obj ::= LCB keyValList:k RCB {: RESULT = new Map :}
- | LCB RCB {: RESULT = {} :}
- ;
- keyValList ::= ListElem:l {: RESULT = l :}
- | ListElem COMMA {: :}
- ;
- listElem ::= ListElem COMMA keyValue {: :}
- | keyValue {: :}
- ;
- keyValue ::= IDENTIFIER COLON value {: :}
- | STRING COLON value {: :}
- ;
- value ::= NUMBER:n {: System.out.println(n); RESULT = n; :}
- | obj:n {: System.out.println(n); RESULT = n; :}
- | BOOLEAN:n {: System.out.println(n); RESULT = n; :}
- | STRING:n {: System.out.println(n); RESULT = n; :}
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement