PARSER_BEGIN(SimpleExp) package org.virbo.dsutil.dd; import java.io.*; import java.util.*; import java.nio.ByteBuffer; import org.das2.util.ByteBufferInputStream; class SimpleExp { public static void main(String[] arg) throws ParseException, TokenMgrError, NumberFormatException, IOException { if (arg.length != 1) { System.out.println("Usage: Java SimpleExp file"); } InputStream is; if ( arg.length==0 ) { is = new ByteBufferInputStream( ByteBuffer.wrap( "3+4".getBytes() ) ); } else { is = new FileInputStream(arg[0]); } SimpleExp parser = new SimpleExp(is); List program = parser.Start(); boolean flag = Semantic.checkProgram(program); if (flag) System.out.println("Check succeeded"); if (!flag) System.out.println("Check failed"); } } PARSER_END(SimpleExp) SKIP : { " " | "\n" | "\r" } TOKEN : {} TOKEN : {} TOKEN : {} TOKEN : {} TOKEN : {} TOKEN : {} List Start () : { List l = new ArrayList(); Node n; } { ( n = Statement() { l.add(n); } )* { return l; } } Node Statement() : { Node ident, expr; } { ident = Identifier() expr = Expression() { Node n = new Node(Id.ASSIGN); n.add(ident); n.add(expr); return n; } } Node Identifier () : { Token t; } { t = { return new IdentifierNode(t.image); } } Node Expression() : { Node expr, term, temp; } { expr = Term() ( term = Term() { temp = new Node(Id.PLUS); temp.add(expr); temp.add(term); expr = temp; } )* { return expr; } } Node Term () throws NumberFormatException : { Node expr; Token t; } { t = { return new DoubleNode(Double.parseDouble(t.image)); } | expr = Expression() { return expr; } | expr = Identifier() { return expr; } }