options { STATIC = false; } PARSER_BEGIN(FJParser) package tmp.generated_fj; import java.io.*; import java.util.*; import cide.gast.*; import cide.gparser.*; /** * Grammar to parse Java version 1.5 * @author Sreenivasa Viswanadha - Simplified and enhanced for 1.5 */ public class FJParser { public FJParser(String fileName) { this(System.in); try { ReInit(new FileInputStream(new File(fileName))); } catch(Exception e) { e.printStackTrace(); } } public static void main(String args[]) { FJParser parser; if (args.length == 0) { System.out.println("Featherweigth Java Parser Version 1.1: Reading from standard input . . ."); parser = new FJParser(System.in); } else if (args.length == 1) { System.out.println("Featherweigth Java Parser Version 1.1: Reading from file " + args[0] + " . . ."); try { parser = new FJParser(new java.io.FileInputStream(args[0])); } catch (java.io.FileNotFoundException e) { System.out.println("Featherweigth Java Parser Version 1.1: File " + args[0] + " not found."); return; } } else { System.out.println("Featherweigth Java Parser Version 1.1: Usage is one of:"); System.out.println(" java JavaParser < inputfile"); System.out.println("OR"); System.out.println(" java JavaParser inputfile"); return; } try { parser.Goal(); System.out.println("Featherweigth Java Parser Version 1.1: Java program parsed successfully."); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println("Featherweigth Java Parser Version 1.1: Encountered errors during parse."); e.printStackTrace(System.out); } } public FJParser(InputStream inputStream) { this(new OffsetCharStream(inputStream)); } private void ReInit(InputStream inputStream) { ReInit(new OffsetCharStream(inputStream)); } public ISourceFile getRoot() throws ParseException { return Goal(); } } PARSER_END(FJParser) /* WHITE SPACE */ SKIP : { " " | "\t" | "\n" | "\r" | "\f" } /* COMMENTS */ MORE : { "//" : IN_SINGLE_LINE_COMMENT | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } MORE : { < ~[] > } /* RESERVED WORDS AND LITERALS */ TOKEN : { < THIS: "this" > | < OBJECT: "Object" > | < CLASS: "class" > | < EXTENDS: "extends" > | < INT: "int" > | < NEW: "new" > | < RETURN: "return" > | < SUPER: "super" > } /* LITERALS */ TOKEN : { < INTEGER_LITERAL: (["l","L"])? | (["l","L"])? | (["l","L"])? > | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | < #OCTAL_LITERAL: "0" (["0"-"7"])* > } /* IDENTIFIERS */ TOKEN : { < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > } /* SEPARATORS */ TOKEN : { < LPAREN: "(" > | < RPAREN: ")" > | < LBRACE: "{" > | < RBRACE: "}" > | < COMMA: "," > | < DOT: "." > } /* OPERATORS */ TOKEN : { < PLUS: "+" > | < MINUS: "-" > | < STAR: "*" > | < SLASH: "/" > } GRAMMARSTART Goal : TypeDeclaration ; TypeDeclaration : "class" "extends" ExtendedType "{" ( LL(2) VarDeclaration )* ClassConstructor ( MethodDeclaration )* "}"; ExtendedType : | "Object"; VarDeclaration : Type ";"; ClassConstructor : Type "(" ( FormalParameterList )? ")" "{" "super" "(" ( ExpressionList )? ")" ";" ( FieldAssign )* "}"; FieldAssign : "this" "." "=" ";"; MethodDeclaration : Type "(" ( FormalParameterList )? ")" "{" "return" Expression ";" "}"; BinaryOperator : "+" | "-" | "*" | "/"; FormalParameterList : FormalParameter ( FormalParameterRest )*; FormalParameter : Type ; FormalParameterRest : "," FormalParameter; Type : "int" | | "Object"; Expression : Term ( PlusOrMinus )*; PlusOrMinus: PlusExpressionRest | MinusExpressionRest; PlusExpressionRest : "+" Term; MinusExpressionRest : "-" Term; Term : PrimaryExpression ( TimesOrDivide )*; TimesOrDivide: TimesExpressionRest | DivideExpressionRest; TimesExpressionRest : "*" PrimaryExpression; DivideExpressionRest : "/" PrimaryExpression; PrimaryExpression : | LL(2147483647) MethodInvoke | LL(2147483647) FieldInvoke | | LL(2147483647) AllocationExpression | LL(2147483647) CastExpression | NestedExpression; MethodInvoke : InvokeTarget "." "(" ( ExpressionList )? ")"; FieldInvoke : InvokeTarget "." ; InvokeTarget: AllocationExpression | NestedExpression | | "this"; AllocationExpression : "new" "(" ( ExpressionList )? ")"; CastExpression : "(" Type ")" PrimaryExpression; NestedExpression : "(" Expression ")"; ExpressionList : Expression ( ExpressionRest )*; ExpressionRest : "," Expression;