import java_cup.runtime.*;
terminal READ, PRINT, IF, WHILE, ASSIGN, SEMI, LPAR, RPAR, LBRA, RBRA;
terminal GT, LT, PLUS, MINUS, ASTER, SLASH, UMINUS;
terminal ICONST, IDENT;
non terminal prog, statlist, stat, expr;
precedence nonassoc LT, GT;
precedence left PLUS, MINUS;
precedence left ASTER, SLASH;
precedence left UMINUS;
prog ::= statlist
;
statlist ::= stat statlist
|
;
stat ::= IDENT ASSIGN expr SEMI
| READ IDENT SEMI
| PRINT expr SEMI
| IF LPAR expr RPAR stat
| WHILE LPAR expr RPAR stat
| LBRA statlist RBRA
;
expr ::= expr PLUS expr
| expr MINUS expr
| expr ASTER expr
| expr SLASH expr
| expr GT expr
| expr LT expr
| MINUS expr %prec UMINUS
| IDENT
| ICONST
| LPAR expr RPAR
;
import java_cup.runtime.*;
%%
%class Lexer
%cup
%{
SymbolFactory sf = new DefaultSymbolFactory();
%}
%eofval{
return sf.newSymbol("EOF", sym.EOF);
%eofval}
L = [A-Za-z]
D = [0-9]
Ident = {L}({L}|{D})*
Iconst = [-+]?{D}+
Blank = [ \t\n]+
%%
\+ { return sf.newSymbol("PLUS", sym.PLUS); }
\- { return sf.newSymbol("MINUS", sym.MINUS); }
\* { return sf.newSymbol("ASTER", sym.ASTER); }
\/ { return sf.newSymbol("SLASH", sym.SLASH); }
\< { return sf.newSymbol("LPAR", sym.LT); }
\> { return sf.newSymbol("RPAR", sym.GT); }
\( { return sf.newSymbol("LPAR", sym.LPAR); }
\) { return sf.newSymbol("RPAR", sym.RPAR); }
\{ { return sf.newSymbol("LPAR", sym.LBRA); }
\} { return sf.newSymbol("RPAR", sym.RBRA); }
\; { return sf.newSymbol("SEMI", sym.SEMI); }
\= { return sf.newSymbol("ASSIGN", sym.ASSIGN); }
if { return sf.newSymbol("IF", sym.IF); }
while { return sf.newSymbol("WHILE", sym.WHILE); }
read { return sf.newSymbol("READ", sym.READ); }
print { return sf.newSymbol("PRINT", sym.PRINT); }
{Ident} { return sf.newSymbol("IDENT", sym.IDENT, yytext()); }
{Iconst} { return sf.newSymbol("ICONST", sym.ICONST, yytext()); }
{Blank} { /* ignore */ }
import java.util.*;
import java.io.*;
import java_cup.runtime.*;
public class Sam81 {
public static void main(String[] args) throws Exception {
parser p1 = new parser(new Lexer(new FileReader(args[0])));
p1.parse();
}
}
import java_cup.runtime.*;
terminal READ, PRINT, IF, WHILE, ASSIGN, SEMI, LPAR, RPAR, LBRA, RBRA;
terminal GT, LT, PLUS, MINUS, ASTER, SLASH, UMINUS;
terminal String ICONST, IDENT;
non terminal Tree.Node prog, statlist, stat, expr;
precedence nonassoc LT, GT;
precedence left PLUS, MINUS;
precedence left ASTER, SLASH;
precedence left UMINUS;
prog ::= statlist:x {: RESULT = x; :}
;
statlist ::= statlist:x stat:y {: RESULT = x; x.add(y); :}
| {: RESULT = new Tree.Seq(); :}
;
stat ::= IDENT:x ASSIGN expr:y SEMI
{: RESULT = new Tree.Assign(new Tree.Var(x), y); :}
| READ IDENT:x SEMI {: RESULT = new Tree.Read(new Tree.Var(x)); :}
| PRINT expr:x SEMI {: RESULT = new Tree.Print(x); :}
| IF LPAR expr:x RPAR stat:y {: RESULT = new Tree.If1(x, y); :}
| WHILE LPAR expr:x RPAR stat:y {: RESULT = new Tree.While(x, y); :}
| LBRA statlist:x RBRA {: RESULT = x; :}
;
expr ::= expr:x PLUS expr:y {: RESULT = new Tree.Add(x, y); :}
| expr:x MINUS expr:y {: RESULT = new Tree.Sub(x, y); :}
| expr:x ASTER expr:y {: RESULT = new Tree.Mul(x, y); :}
| expr:x SLASH expr:y {: RESULT = new Tree.Div(x, y); :}
| expr:x GT expr:y {: RESULT = new Tree.Gt(x, y); :}
| expr:x LT expr:y {: RESULT = new Tree.Lt(x, y); :}
| MINUS expr:x {: RESULT = new Tree.Neg(x); :} %prec UMINUS
| IDENT:x {: RESULT = new Tree.Var(x); :}
| ICONST:x {: RESULT = new Tree.Lit(Integer.parseInt(x)); :}
| LPAR expr:x RPAR {: RESULT = x; :}
;
import java.util.*;
import java.io.*;
import java_cup.runtime.*;
public class Sam82 {
public static void main(String[] args) throws Exception {
parser p1 = new parser(new Lexer(new FileReader(args[0])));
Symbol s1 = p1.parse();
((Tree.Node)s1.value).eval();
}
}
read x; read y;
while(x - y) {
if(x > y) { x = x - y; }
if(x < y) { y = y - x; }
}
print x;
import java.util.*;
public class Tree {
public static Map<String,Integer> vars = new TreeMap<String,Integer>();
public abstract static class Node {
List<Node> child = new ArrayList<Node>();
public void add(Node n) { child.add(n); }
public abstract int eval();
}
public static class Lit extends Node {
int val;
public Lit(int v) { val = v; }
public int eval() { return val; }
public String toString() { return ""+val; }
}
public static class Var extends Node {
String name;
public Var(String n) { name = n; }
public int eval() { return vars.get(name); }
public String toString() { return name; }
}
public abstract static class BinOp extends Node {
String op;
public BinOp(String o, Node n1, Node n2) { op = o; add(n1); add(n2); }
public String toString() { return "("+child.get(0)+op+child.get(1)+")"; }
}
public static class Add extends BinOp {
public Add(Node n1, Node n2) { super("+", n1, n2); }
public int eval() { return child.get(0).eval() + child.get(1).eval(); }
}
public static class Sub extends BinOp {
public Sub(Node n1, Node n2) { super("-", n1, n2); }
public int eval() { return child.get(0).eval() - child.get(1).eval(); }
}
public static class Mul extends BinOp {
public Mul(Node n1, Node n2) { super("+", n1, n2); }
public int eval() { return child.get(0).eval() * child.get(1).eval(); }
}
public static class Div extends BinOp {
public Div(Node n1, Node n2) { super("/", n1, n2); }
public int eval() { return child.get(0).eval() / child.get(1).eval(); }
}
public static class Mod extends BinOp {
public Mod(Node n1, Node n2) { super("%", n1, n2); }
public int eval() { return child.get(0).eval() % child.get(1).eval(); }
}
public static class Eq extends BinOp {
public Eq(Node n1, Node n2) { super("==", n1, n2); }
public int eval() { return child.get(0).eval()==child.get(1).eval()?1:0; }
}
public static class Ne extends BinOp {
public Ne(Node n1, Node n2) { super("!=", n1, n2); }
public int eval() { return child.get(0).eval()!=child.get(1).eval()?1:0; }
}
public static class Gt extends BinOp {
public Gt(Node n1, Node n2) { super(">", n1, n2); }
public int eval() { return child.get(0).eval()>child.get(1).eval()?1:0; }
}
public static class Ge extends BinOp {
public Ge(Node n1, Node n2) { super(">=", n1, n2); }
public int eval() { return child.get(0).eval()>=child.get(1).eval()?1:0; }
}
public static class Lt extends BinOp {
public Lt(Node n1, Node n2) { super("<", n1, n2); }
public int eval() { return child.get(0).eval()<child.get(1).eval()?1:0; }
}
public static class Le extends BinOp {
public Le(Node n1, Node n2) { super("<=", n1, n2); }
public int eval() { return child.get(0).eval()<=child.get(1).eval()?1:0; }
}
public static class And extends BinOp {
public And(Node n1, Node n2) { super("&&", n1, n2); }
public int eval() {
int v = child.get(0).eval();
if(v == 0) { return 0; } else { return child.get(1).eval(); }
}
}
public static class Or extends BinOp {
public Or(Node n1, Node n2) { super("||", n1, n2); }
public int eval() {
int v = child.get(0).eval();
if(v != 0) { return v; } else { return child.get(1).eval(); }
}
}
public abstract static class UniOp extends Node {
String op;
public UniOp(String o, Node n1) { op = o; add(n1); }
public String toString() { return "("+op+child.get(0)+")"; }
}
public static class Not extends UniOp {
public Not(Node n1) { super("!", n1); }
public int eval() { return child.get(0).eval()==0?1:0; }
}
public static class Neg extends UniOp {
public Neg(Node n1) { super("-", n1); }
public int eval() { return -child.get(0).eval(); }
}
public static class Assign extends Node {
Var v1; Node n1;
public Assign(Var v, Node n) { v1 = v; n1 = n; }
public int eval() {
int v = n1.eval(); vars.put(v1.toString(), v); return v;
}
public String toString() { return v1+"="+n1; }
}
public static class Seq extends Node {
public Seq(Node... a) { for(Node n:a) { child.add(n); } }
public int eval() {
int v = 0;
for(Node n:child) { v = n.eval(); }
return v;
}
public String toString() {
String s = "{\n";
for(Node n:child) { s += n.toString() + ";\n"; }
return s + "}";
}
}
public static class Read extends Node {
Var v1;
public Read(Var v) { v1 = v; }
public int eval() {
System.out.print(v1+"? ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int i = Integer.parseInt(str);
vars.put(v1.toString(), i); return i;
}
public String toString() { return "read "+v1; }
}
public static class Print extends Node {
public Print(Node n1) { child.add(n1); }
public int eval() {
int v = child.get(0).eval(); System.out.println(v); return v;
}
public String toString() { return "print "+child.get(0); }
}
public static class While extends Node {
public While(Node n1, Node n2) { child.add(n1); child.add(n2); }
public int eval() {
int v = 0;
while(child.get(0).eval() != 0) { v = child.get(1).eval(); }
return v;
}
public String toString() {
return "while("+child.get(0)+")"+child.get(1);
}
}
public static class If1 extends Node {
public If1(Node n1, Node n2) { child.add(n1); child.add(n2); }
public int eval() {
int v = 0;
if(child.get(0).eval() != 0) { v = child.get(1).eval(); }
return v;
}
public String toString() { return "if("+child.get(0)+")"+child.get(1); }
}
}