***Intro*** Write a lexer and parser (using JavaCC) and simple semantics analyzer for simple Java-like language described in this file. - Each program starts with a keyword "program" and has static fields, static methods, inerfaces, and inner classes. - The main method in the language is always called main(). - There are: -- three types of constants: int, char, and bool. -- several primitive types: int, bool, char (ASCII only), enums. -- several kinds of variables: global (static), local, fields. -- reference types: arrays like in Java, interfaces and inner classes with fields and methods. -- static methods - There is also class inheritance and polymorphism. - Overloading is also available. - Three predefined procedure include: ord, chr, and len. - The "print" method prints values for all primitive types. - Control structures include "if" and "for". ***Syntax*** Program = "program" ident {ConstDecl | VarDecl | ClassDecl| EnumDecl | InterfaceDecl} "{" {MethodDecl} "}". ConstDecl = "const" Type ident "=" (numConst | charConst| boolConst) {, ident "=" (numConst | charConst | boolConst)} ";". EnumDecl = "enum" ident "{" ident ["=" numConst] {"," ident ["=" numConst]} "}". VarDecl = Type ident ["[" "]"] {"," ident ["[" "]"]} ";". ClassDecl = "class" ident ["extends" Type] ["implements" Type {"," Type}]"{" {VarDecl}["{"{MethodDecl} "}"]"}". InterfaceDecl = "interface" ident "{"{InterfaceMethodDecl} "}" InterfaceMethodDecl = (Type | "void") ident "(" [FormPars]")" ";" MethodDecl = (Type | "void") ident "(" [FormPars]")" {VarDecl} "{" {Statement} "}". FormPars = Type ident ["[" "]"] {"," Type ident ["[" "]"]}. Type = ident. Statement = DesignatorStatement ";" | "if" "(" Condition ")" Statement ["else" Statement] | "for" "(" [DesignatorStatement ] ";" [Condition] ";" [DesignatorStatement] ")" Statement | "break" ";" | "continue" ";" | "return" [Expr] ";" | "read" "(" Designator ")" ";" | "print" "(" Expr ["," numConst] ")" ";" | "{" {Statement} "}". DesignatorStatement = Designator (Assignop Expr | "(" [ActPars] ")" | "++" | "--"). ActPars = Expr {"," Expr}. Condition = CondTerm {"||" CondTerm}. CondTerm = CondFact {"&&" CondFact}. CondFact = Expr [Relop Expr]. Expr = ["-"] Term {Addop Term}. Term = Factor {Mulop Factor}. Factor = Designator ["(" [ActPars] ")"] | numConst | charConst | boolConst | "new" Type ["[" Expr "]"] | "(" Expr ")". Designator = ident {"." ident | "[" Expr "]"} Assignop = "=". Relop = "==" | "!=" | ">" | ">=" | "<" | "<=" Addop = "+" | "-". Mulop = "*" | "/" | "%" ***Other lexical structures*** keywords :program, break, class, interface, enum, else, const, if, new, print, read, return, void, for, extends, continue token types: ident = letter {letter | digit | "_"}. numConst = digit {digit}. charConst = "'" printableChar "'". boolConst = ("true" | "false") operators: +, -, *, /, %, ==, !=, >, >=, <, <=, &&, ||, =, ++, --, ;, comma, ., (, ), [, ], {, } comments: // until the end of the line ***Semantics*** - Each name must be declared before its fist use. - There cannot be two variables with the same name in one scope. - There must be a method "main"; the return type must be void and the method should have no arguments. These checks should be done on the AST. ***Submission Instructions*** See syllabus. ***Important*** Be creative. When something is unclear make a reasonable assumption. Also, do not try to cover everything at once; start with small examples and then expand from there. ***Example program*** program P const int size = 10; enum Num { ZERO, ONE, TEN = 10 } interface I { int getp(int i); int getn(int i); } class Table implements I { int pos[], neg[]; { void putp(int a, int idx) { this.pos[idx]=a; } void putn (int a, int idx) { this.neg[idx]=a; } int getp (int idx) { return pos[idx]; } int getn (int idx) { return neg[idx]; } } } Table val; { void f(char ch, int a, int arg) int x; { x = arg; } void main() int x, i; char c; { //---------- Initialize val val = new Table; val.pos = new int [size]; val.neg = new int [size]; for (i = 0; i 0;) { if (Num.ZERO<= x && x < size) { val.putp(val.getp(x)+Num.ONE); } else { if (-size < x && x < 0) { val.putn(val.getn(-x)+1); } } read(x); } } }