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

Use the calculator from the website as default project.

parent 5e2597ed
......@@ -2,7 +2,7 @@ package cup.example;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.Location;
import java_cup.runtime.Symbol;
import java.lang.Error;
import java.lang.*;
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}
Number = [0-9]
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment}
......@@ -76,9 +77,14 @@ ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
<YYINITIAL> {
{Whitespace} { }
"my" { return symbol("MYTERMINAL",MYTERMINAL); }
"other" { return symbol("OTHERTERMINAL",OTHERTERMINAL); }
{Whitespace} { }
";" { return symbolFactory.newSymbol("SEMI", SEMI); }
"+" { 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;
import java_cup.runtime.*;
import cup.example.Lexer;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
parser code {:
/* override error routines */
protected Lexer lexer;
:};
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);
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(); :};
terminal MYTERMINAL, OTHERTERMINAL;
scan with {: return lexer.next_token(); :};
/* 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;
expr ::= MYTERMINAL {:
// action code here
System.out.println("hello myterminal!");
/* Precedences */
precedence left PLUS, MINUS;
precedence left TIMES;
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