#header << typedef ANTLRCommonToken ANTLRToken; >> << // scanner rules #lexclass START // Not really necessary, but // good commentary nonetheless // White Space #token "[\ \t\r]" <> #token "\n" <> // Comments #token "\' ~[\n@]* [\n@]" <> #token "REM [\ ] ~[\n@]* [\n@]" <> // Literals #token INTLIT "[0-9]+" #token HEXLIT "&H[0-9A-F]+" #token "\"" << // start STRINGLIT skip(); mode(STRINGLIT_CLASS); >> // Keywords #token AS "as" #token DIM "dim" #token ENDIF "end\ if" #token ELSE "else" #token FOR "for" #token GOSUB "gosub" #token GOTO "goto" #token IF "if" #token INTEGER "integer" #token LET "let" #token LONG "long" #token NEXT "next" #token PRINT "print" #token RETURN "return" #token STRING "string" #token STEP "step" #token THEN "then" #token TO "to" // Operators #token COMMA "," #token EQUALS "=" #token LPAREN "\(" #token RPAREN "\)" #token NOT_EQUALS "<>" #token LT "<" #token LTE "<=" #token GT ">" #token GTE ">=" #token PLUS "\+" #token MINUS "\-" #token TIMES "\*" #token DIV "/" #token DIVINT "\\" #token MOD "mod" #token EXP "\^" #token NOT "not" #token IMP "imp" #token EQV "eqv" #token XOR "xor" #token OR "or" #token AND "and" // Identifiers #token IDENT "[a-zA-Z] [a-zA-Z0-9]* {[!%&$#]}" #token LABEL "[a-zA-Z] [a-zA-Z0-9]* [:]" // String Literal Processing // Separate Scanner class! #lexclass STRINGLIT_CLASS #token "\"\"" << more(); replchar('\"'); >> #token BADSTRING "\n" << replchar('\0'); newline(); mode(START); /* error message */ >> #token STRINGLIT "\"" << replchar('\0'); mode(START); >> #token "~[]" <> #tokclass STRING_LITERAL {STRINGLIT BADSTRING} #tokclass ADD_OP {PLUS MINUS} #tokclass RELATIONAL_OP {EQUALS NOT_EQUALS GT GTE LT LTE} #tokclass BOOLEAN_OP {AND OR XOR NOT EQV IMP} #tokclass MULT_OP {TIMES DIV DIVINT MOD EXP} class Parser { program : ( basicDecl )* subprogramBody "@" ; subprogramBody : statementList ; basicDecl : varDecl ; varDecl : DIM identList AS typeName ; identList : IDENT ( COMMA IDENT )* ; constantValue : INTLIT | HEXLIT | STRING_LITERAL ; typeName : INTEGER | LONG | STRING ; statement : ifStatement | LABEL | goStatement | returnStatement | assignmentStatement | forStatement | PRINT expression ; statementList : statement statementList | ; assignmentStatement : { LET } assignmentLHS EQUALS expression ; assignmentLHS : IDENT ; returnStatement : RETURN ; goStatement : ( GOTO | GOSUB ) IDENT ; ifStatement : IF ifPart ENDIF ; ifPart : expression THEN statementList { ELSE statementList } ; forStatement : FOR variableReference EQUALS expression ( TO | DOWNTO ) expression forStep statementList NEXT variableReference ; forStep : STEP expression | ; variableReference : IDENT ; primitiveElement : variableReference | constantValue | LPAREN expression RPAREN ; booleanNegationExpression : ( NOT )* primitiveElement ; signExpression : ( ADD_OP )* booleanNegationExpression ; multiplyingExpression : signExpression ( MULT_OP signExpression )* ; addingExpression : multiplyingExpression ( ADD_OP multiplyingExpression )* ; relationalExpression : addingExpression ( RELATIONAL_OP addingExpression )* ; expression : relationalExpression ( BOOLEAN_OP relationalExpression )* ; }