header { #include "ast.h" } options { language="Cpp"; } class P extends Parser; prog returns [ Program* p ] { vector dv; vector sv; Declaration* d; Statement* s; } : (d=dcl { dv.push_back(d); } )* (s=stmt { sv.push_back(s); } )* { p = new Program( dv, sv ); } ; dcl returns [ Declaration* d ] : FLOATDCL fid:ID { d = new Declaration( fid->getText(), T_FLOAT ); } | INTDCL iid:ID { d = new Declaration( iid->getText(), T_INT ); } ; stmt returns [ Statement* retval ] { Expression *exp, *v0, *v1, *v2; } : aid:ID ASSIGN v0=val { exp = v0; } (PLUS v1=val { exp = new MathOp( T_PLUS, exp, v1 ); } | MINUS v2=val { exp = new MathOp( T_MINUS, exp, v2 ); } )* { retval = new AssignStatement( new Identifier( aid->getText() ), exp ); } | PRINT pid:ID { retval = new PrintStatement( new Identifier( pid->getText() ) ); } ; val returns [ Expression* retval ] : id:ID { retval = new Identifier( id->getText() ); } | num:NUM { retval = make_num( num->getText() ); } ; class L extends Lexer; FLOATDCL : 'f' ; INTDCL : 'i' ; ID : 'a'..'e'|'g'|'h'|'j'..'o'|'q'..'z' ; ASSIGN : '=' ; PRINT : 'p' ; PLUS : '+' ; MINUS : '-' ; NUM : ( ( '0'..'9' )+ '.')=> ( '0'..'9')+ '.' ( '0'..'9' )+ | ( '0'..'9' )+ ; WS : (' ' | ('\r' '\n' | '\n') { newline(); } | '\t') {$setType(Token::SKIP);} //ignore this token ;