Commit 8083baf1 authored by Johannes Roith's avatar Johannes Roith
Browse files

Use the calculator from the website as default project.

parent 5e2597ed
...@@ -2,7 +2,7 @@ package cup.example; ...@@ -2,7 +2,7 @@ package cup.example;
import java_cup.runtime.ComplexSymbolFactory; import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.Location; import java_cup.runtime.ComplexSymbolFactory.Location;
import java_cup.runtime.Symbol; import java_cup.runtime.Symbol;
import java.lang.Error; import java.lang.*;
import java.io.InputStreamReader; import java.io.InputStreamReader;
%% %%
...@@ -54,8 +54,9 @@ import java.io.InputStreamReader; ...@@ -54,8 +54,9 @@ import java.io.InputStreamReader;
} }
%} %}
Newline = \r | \n | \r\n Newline = \r | \n | \r\n
Whitespace = [ \t\f] | {Newline} Whitespace = [ \t\f] | {Newline}
Number = [0-9]
/* comments */ /* comments */
Comment = {TraditionalComment} | {EndOfLineComment} Comment = {TraditionalComment} | {EndOfLineComment}
...@@ -76,9 +77,14 @@ ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )* ...@@ -76,9 +77,14 @@ ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
<YYINITIAL> { <YYINITIAL> {
{Whitespace} { } {Whitespace} { }
"my" { return symbol("MYTERMINAL",MYTERMINAL); } ";" { return symbolFactory.newSymbol("SEMI", SEMI); }
"other" { return symbol("OTHERTERMINAL",OTHERTERMINAL); } "+" { return symbolFactory.newSymbol("PLUS", PLUS); }
"-" { return symbolFactory.newSymbol("MINUS", MINUS); }
"n" { return symbolFactory.newSymbol("UMINUS", UMINUS); }
"(" { return symbolFactory.newSymbol("LPAREN", LPAREN); }
")" { return symbolFactory.newSymbol("RPAREN", RPAREN); }
{Number} { return symbolFactory.newSymbol("NUMBER", NUMBER, Integer.parseInt(yytext())); }
} }
......
/*
Simple +/-/* expression language;
parser evaluates constant expressions on the fly
*/
package cup.example; package cup.example;
import java_cup.runtime.*; import java_cup.runtime.*;
import cup.example.Lexer;
import java.io.IOException;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
parser code {: parser code {:
/* override error routines */ protected Lexer lexer;
protected Lexer lexer; :}
:};
init with {: /* define how to connect to the scanner! */
ComplexSymbolFactory f = new ComplexSymbolFactory(); init with {:
symbolFactory = f; ComplexSymbolFactory f = new ComplexSymbolFactory();
File file = new File("input.txt"); symbolFactory = f;
FileInputStream fis = null; File file = new File("input.txt");
FileInputStream fis = null;
try { try {
fis = new FileInputStream(file); fis = new FileInputStream(file);
}catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
lexer = new Lexer(f,fis); lexer = new Lexer(f,fis);
:}; :};
scan with {: return lexer.next_token(); :}; scan with {: return lexer.next_token(); :};
terminal MYTERMINAL, OTHERTERMINAL;
/* Terminals (tokens returned by the scanner). */
/*terminal SEMI, PLUS, MINUS, TIMES, UMINUS, LPAREN, RPAREN;*/
terminal SEMI, PLUS, MINUS, UMINUS, TIMES, LPAREN, RPAREN;
terminal Integer NUMBER; // our scanner provides numbers as integers
/* Non terminals */
non terminal expr_list;
non terminal Integer expr; // used to store evaluated subexpressions
non terminal Integer expr; /* Precedences */
expr ::= MYTERMINAL {: precedence left PLUS, MINUS;
// action code here precedence left TIMES;
System.out.println("hello myterminal!"); precedence left UMINUS;
:} | OTHERTERMINAL; /* The grammar rules */
expr_list ::= expr_list expr:e SEMI {: System.out.println(e); :}
| expr:e SEMI {: System.out.println(e); :}
;
expr ::= expr:e1 PLUS expr:e2 {: RESULT = e1+e2; :}
| expr:e1 MINUS expr:e2 {: RESULT = e1-e2; :}
| expr:e1 TIMES expr:e2 {: RESULT = e1*e2; :}
| MINUS expr:e {: RESULT = -e; :}
// %prec UMINUS
| LPAREN expr:e RPAREN {: RESULT = e; :}
| NUMBER:n {: RESULT = n; :}
;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment