COMPILER Program CHARACTERS letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz". digit = "0123456789". cr = '\r'. lf = '\n'. tab = '\t'. charCh = ANY - '\\' - cr - lf. TOKENS /* Names */ ident = letter { letter | digit }. /* Numbers */ number = digit { digit }. /* Character constants */ charConst = '\'' ( charCh | '\\' [ 'n' | 'r' ] ) '\''. /* Operators */ add = "+". sub = "-". mul = "*". div = "/". mod = "%". inc = "++". dec = "--". eq = "==". ne = "!=". gt = ">". ge = ">=". lt = "<". le = "<=". and = "&&". or = "||". lPar = "(". rPar = ")". lBrack = "[". rBrack = "]". lBrace = "{". rBrace = "}". assign = "=". semCol = ";". comma = ",". dot = ".". /* Keywords */ class = "class". if = "if". else = "else". while = "while". read = "read". write = "write". return = "return". void = "void". const = "const". new = "new". COMMENTS FROM "/*" TO "*/" NESTED IGNORE cr + lf + tab PRODUCTIONS Program = "class" ident { ConstDecl | VarDecl | ClassDecl } "{" { MethodDecl } "}" . ConstDecl = "const" Type ident "=" ( number | charConst ) ";" . VarDecl = Type ident { "," ident } ";" . ClassDecl = "class" ident "{" { VarDecl } "}" . MethodDecl = ( Type | "void" ) ident "(" [ FormPars ] ")" { VarDecl } Block . FormPars = FormPar { "," FormPar } . FormPar = Type ident . Type = ident [ "[" "]" ] . Statement = ( Designator ( "=" Expr | ActParList | "++" | "--" ) ";" | "if" "(" Condition ")" Block [ "else" Block ] | "while" "(" Condition ")" Block | "return" [ Expr ] ";" | "read" "(" Designator ")" ";" | "write" "(" Expr [ "," number ] ")" ";" | ";" ) . Designator = ident [ "[" Expr "]" ] { "." ident [ "[" Expr "]" ] } . Block = "{" { Statement } "}" . ActParList = "(" [ Expr { "," Expr } ] ")" . Condition = CondTerm { "||" CondTerm } . CondTerm = CondFact { "&&" CondFact } . CondFact = Expr Relop Expr . Expr = [ "-" ] Term { Addop Term } . Term = Factor { Mulop Factor } . Factor = ( Designator [ ActParList ] | number | charConst | "new" ident [ "[" Expr "]" ] | "(" Expr ")" ) . Relop = ( "==" | "!=" | ">" | ">=" | "<" | "<=" ) . Addop = ( "+" | "-" ) . Mulop = ( "*" | "/" | "%" ) . END Program.