Commit db7aab54 authored by petter's avatar petter

basic example

git-svn-id: https://www2.in.tum.de/repos/cup/develop@53 f8377d4f-c7f8-0310-b030-a31f13008fac
parent 0a4dc5a1
/* Simple +/-/* expression language; parser evaluates constant expressions on the fly*/
import java_cup.runtime.*;
parser code {:
scanner s;
Parser(scanner s){
this.s=s;
}
:}
init with {: s.init(); :};
scan with {: return s.next_token(); :};
/* Terminals (tokens returned by the scanner). */
terminal SEMI, PLUS, MINUS, TIMES, UMINUS, 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
/* Precedences */
precedence left PLUS, MINUS;
precedence left TIMES;
precedence left UMINUS;
/* 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; :}
| LPAREN expr:e RPAREN {: RESULT = e; :}
| NUMBER:n {: RESULT = n; :}
;
java -jar ../../dist/java-cup-11b.jar -interface -parser Parser calc.cup
javac -cp ../../dist/java-cup-11b-runtime.jar:. *.java
java -cp ../../dist/java-cup-11b-runtime.jar:. Main
import java_cup.runtime.*;
class Main {
public static void main(String[] argv) throws Exception{
System.out.println("Please type your arithmethic expression:");
Parser p = new Parser(new scanner());
p.parse();
}
}
public class scanner {
/* single lookahead character */
protected static int next_char;
/* we use a SymbolFactory to generate Symbols */
private SymbolFactory sf = new DefaultSymbolFactory();
/* advance input by one character */
protected void advance() throws java.io.IOException
{ next_char = System.in.read(); }
/* initialize the scanner */
public void init() throws java.io.IOException
{ advance(); }
/* recognize and return the next complete token */
public Symbol next_token() throws java.io.IOException
{
for (;;)
switch (next_char)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
/* parse a decimal integer */
int i_val = 0;
do {
i_val = i_val * 10 + (next_char - '0');
advance();
} while (next_char >= '0' && next_char <= '9');
return sf.newSymbol("NUMBER",sym.NUMBER, new Integer(i_val));
case ';': advance(); return sf.newSymbol("SEMI",sym.SEMI);
case '+': advance(); return sf.newSymbol("PLUS",sym.PLUS);
case '-': advance(); return sf.newSymbol("MINUS",sym.MINUS);
case '*': advance(); return sf.newSymbol("TIMES",sym.TIMES);
case '(': advance(); return sf.newSymbol("LPAREN",sym.LPAREN);
case ')': advance(); return sf.newSymbol("RPAREN",sym.RPAREN);
case -1: return sf.newSymbol("EOF",sym.EOF);
default:
/* in this simple scanner we just ignore everything else */
advance();
break;
}
}
};
int x, y;
y = 10;
x = read();
read();
x = write(x);
write(x);
if (x>=0)
write (y);
else
......
......@@ -3,7 +3,7 @@
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:include href="tree-view.xsl"/>
......
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