You are on page 1of 24

SATURN IDE

( NBA Programming Language )

The NBA Programming Language was designed based on C


Programming Language, created in Java with the help of NETBEANS IDE, JFlex
and CUP for the parsing.
LEXICAL ANALYZER

import java_cup.runtime.*;
%%
%public
%class Lexical
%cup
%line
%column
%full
%{
StringBuffer string = new StringBuffer();

private Symbol symbol(int type) {


return new CustomSymbol(type, yyline+1, yycolumn+1);
}

private Symbol symbol(int type, Object value) {


return new CustomSymbol(type, yyline+1, yycolumn+1, value);
}

%}

/*main character types*/


LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]

WhiteSpace = {LineTerminator} | [ \t\f]

/* comments */
CommentText = ([^*/]|[^*]"/"[^*]|[^/]"*"[^/]|"*"[^/]|"/"[^*])*
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?

/* identifiers */
Identifier = [:jletter:][:jletterdigit:]*

/* integer literals */
Integer = [0-9]+

/* floating point literals */


Float = {FLit1}|{FLit2}
FLit1 = [0-9]+ \. [0-9]*
FLit2 = [0-9]* \. [0-9]+

/* string and character literals */


StringCharacter = [^\r\n\"\\]
SingleCharacter = [^\r\n\'\\ ]

%state STRINGLITERAL CHARLITERAL COMMENT

%%

<YYINITIAL>{

/* comments */
{EndOfLineComment} { /* ignore */ }
"/*" {yybegin(COMMENT);}
"*/" {
System.out.println("Unmatched end-of-comment: <" + yytext() + ">");
Utility.error(Utility.E_ENDCOMMENT);
}

/* whitespace */
{WhiteSpace} { /* ignore */ }

/*keywords*/
"break" {return symbol(sym.BREAK);}
"char" {return symbol(sym.CHAR);}
"rose" {return symbol(sym.CONST);}
"continue" {return symbol(sym.CONTINUE);}
"bosh" {return symbol(sym.DO);}
"float" {return symbol(sym.FLOAT);}
"wade" {return symbol(sym.FOR);}
"to" {return symbol(sym.TO);}
"kobe" {return symbol(sym.IF);}
"int" {return symbol(sym.INT);}
"bool" {return symbol(sym.INT);}
"fisher" {return symbol(sym.ELSE);}
"inbound" {return symbol(sym.READ);}
"rebound" {return symbol(sym.RETURN);}
"string" {return symbol(sym.STRING);}
"void" {return symbol(sym.VOID);}
"lebron" {return symbol(sym.WHILE);}
"outbound" {return symbol(sym.WRITE);}

/*separators*/
"[" {return symbol(sym.LBRACE);}
"]" {return symbol(sym.RBRACE);}
"(" {return symbol(sym.LPAREN);}
")" {return symbol(sym.RPAREN);}
"{" {return symbol(sym.OPENBRACKET);}
"}" {return symbol(sym.CLOSEBRACKET);}
"," {return symbol(sym.COMMA);}
";" {return symbol(sym.DELIMITER);}

/*operator*/
"=" {return symbol(sym.EQ);}
"+" {return symbol(sym.PLUS);}
"-" {return symbol(sym.MINUS);}
"*" {return symbol(sym.MULT);}
"/" {return symbol(sym.DIV);}
"%" {return symbol(sym.MOD);}
"!" {return symbol(sym.NOT);}
"+=" {return symbol(sym.PLUSEQ);}
"-=" {return symbol(sym.MINUSEQ);}
"/=" {return symbol(sym.DIVEQ);}
"*=" {return symbol(sym.MULTEQ);}
"%=" {return symbol(sym.MODEQ);}
"++" {return symbol(sym.PLUSPLUS);}
"--" {return symbol(sym.MINUSMINUS);}

/*logic*/
"||" {return symbol(sym.OROR);}
"&&" {return symbol(sym.ANDAND);}

/*comparator*/
"==" {return symbol(sym.EQEQ);}
"<=" {return symbol(sym.LTEQ);}
">=" {return symbol(sym.GTEQ);}
">" {return symbol(sym.GT);}
"<" {return symbol(sym.LT);}
"!=" {return symbol(sym.NOTEQ);}

/*identifier*/
{Identifier} {return symbol(sym.IDENTIFIER, yytext());}

/*integer*/
{Integer} {return symbol(sym.INTEGER_LITERAL, new Integer(yytext()));}

/*float*/
{Float} {return symbol(sym.FLOAT_LITERAL, new Float(yytext()));}

/* string literal */
\" { yybegin(STRINGLITERAL); string.setLength(0); }

/* character literal */
\' { yybegin(CHARLITERAL); }
}
<STRINGLITERAL>{
\" {yybegin(YYINITIAL); return symbol(sym.STRING_LITERAL,
string.toString());}

{StringCharacter}+ {string.append(yytext());}
\n {
System.out.println("Unclosed String: <" + string.toString() + ">");
Utility.error(Utility.E_UNCLOSEDSTR);
yybegin(YYINITIAL);
}

/*escape sequences*/
"\\b" {string.append('\b');}
"\\t" {string.append('\t');}
"\\n" {string.append('\n');}
"\\f" { string.append('\f');}
"\\r" {string.append('\r');}
"\\\"" {string.append('\"');}
"\\\\" {string.append('\\');}

<CHARLITERAL>{
{SingleCharacter}\' {yybegin(YYINITIAL); return
symbol(sym.CHARACTER_LITERAL, yytext());}

{SingleCharacter} {
System.out.println("Unclosed Character: <" + yytext() + ">");
Utility.error(Utility.E_UNCLOSEDSTR);
yybegin(YYINITIAL);
}

/*escape sequences*/
"\\b"\' {yybegin(YYINITIAL); return symbol(sym.CHARACTER_LITERAL,
yytext());}
"\\t"\' {yybegin(YYINITIAL); return symbol(sym.CHARACTER_LITERAL,
yytext());}
"\\n"\' {yybegin(YYINITIAL); return symbol(sym.CHARACTER_LITERAL,
yytext());}
"\\f"\' {yybegin(YYINITIAL); return symbol(sym.CHARACTER_LITERAL,
yytext());}
"\\r"\' {yybegin(YYINITIAL); return symbol(sym.CHARACTER_LITERAL,
yytext());}
"\\'"\' {yybegin(YYINITIAL); return symbol(sym.CHARACTER_LITERAL,
yytext());}
"\\\\"\' {yybegin(YYINITIAL); return symbol(sym.CHARACTER_LITERAL,
yytext());}

<COMMENT> {
"*/" {yybegin(YYINITIAL); }
{CommentText} {/* ignore */ }
}

.{
System.out.println("Illegal character: <" + yytext() + ">");
Utility.error(Utility.E_UNMATCHED);
}

PARSER
package compile.lexicalanalyzer;
import java_cup.runtime.*;
import java.io.Reader;

parser code{:

public parser(Reader input){


super(new Lexer(input));
}

public void report_error(String message, Object info){


StringBuffer m = new StringBuffer("ERROR");
if(info instanceof Symbol){
Symbol s = (Symbol)info;
if(s.left >= 0){
m.append(" in line "+(s.left+1));
if(s.right >=0){
m.append(", column " + (s.right+1));
}
}
}
m.append(" : " + message);
System.out.println(m);
}

public void report_fatal_error(String message, Object info){


report_error(message,info);
System.exit(1);
}
:};

terminal INT, CHAR, FLOAT, STRING;


terminal OPENBRACKET, CLOSEBRACKET;
terminal DELIMITER, MULT, COMMA, LBRACE, RBRACE, EQ, LPAREN, RPAREN;
terminal VOID; // method_header
terminal IF, ELSE; // if_then_statement, if_then_else_statement
terminal DO, WHILE, UNTIL; // while_statement, do_statement
terminal FOR, TO; // for_statement
terminal BREAK; // break_statement
terminal CONTINUE; // continue_statement
terminal RETURN; // return_statement
terminal PLUSPLUS; // postincrement_expression
terminal MINUSMINUS; // postdecrement_expression
terminal PLUS, MINUS, NOT, DIV, MOD;
terminal LT, GT, LTEQ, GTEQ; // relational_expression
terminal EQEQ, NOTEQ; // equality_expression
terminal ANDAND; // conditional_and_expression
terminal OROR; // conditional_or_expression
terminal MULTEQ, DIVEQ, MODEQ, PLUSEQ, MINUSEQ; // assignment_operator
terminal CONST;
terminal READ, WRITE;

terminal java.lang.Number INTEGER_LITERAL;


terminal java.lang.Number FLOAT_LITERAL;
terminal java.lang.Character CHARACTER_LITERAL;
terminal java.lang.String STRING_LITERAL;
terminal java.lang.String IDENTIFIER; // name
non terminal goal;
non terminal literal;
non terminal type, primitive_type, returntype;
non terminal array_type, array_return;
non terminal name;
non terminal compilation_unit;

non terminal variable_declarators, variable_declarator;


non terminal variable_declarator_id, variable_initializer;

non terminal method_declarations, method_declaration, method_header,


method_declarator;
non terminal formal_parameter_list_opt, formal_parameter_list;
non terminal formal_parameter;
non terminal method_body;

non terminal array_initializer;

non terminal block;


non terminal block_statements_opt, block_statements, block_statement;
non terminal local_variable_declaration_statement,
local_variable_declaration;
non terminal statement, statement_no_short_if;
non terminal statement_without_trailing_substatement;
non terminal empty_statement;
non terminal expression_statement, statement_expression;
non terminal if_then_statement;
non terminal if_then_else_statement, if_then_else_statement_no_short_if;
non terminal while_statement, while_statement_no_short_if;
non terminal until_statement, until_statement_no_short_if;
non terminal do_statement, do_until_statement;
non terminal for_statement, for_statement_no_short_if;
non terminal for_init_opt, for_init;
non terminal for_update_opt, for_update;
non terminal statement_expression_list;
non terminal identifier_opt;
non terminal break_statement, continue_statement;
non terminal return_statement;
non terminal primary;
non terminal argument_list_opt, argument_list;
non terminal dim_exprs, dim_expr, dims;
non terminal method_invocation, array_access;
non terminal postfix_expression;
non terminal postincrement_expression, postdecrement_expression;
non terminal unary_expression, unary_expression_not_plus_minus;
non terminal preincrement_expression, predecrement_expression;
non terminal multiplicative_expression, additive_expression;
non terminal relational_expression, equality_expression;
non terminal conditional_and_expression, conditional_or_expression;
non terminal assignment_expression;
non terminal assignment;
non terminal left_hand_side;
non terminal assignment_operator;
non terminal expression_opt, expression;
non terminal constant_expression;
non terminal constant_declaration_opt, constant_declarations,
constant_declaration;
non terminal value_sets_opt, value_sets, value_set, value_list_opt, value_list;
non terminal input, output;

start with goal;

goal ::= compilation_unit:compUnit


;

literal ::= INTEGER_LITERAL


| FLOAT_LITERAL
| CHARACTER_LITERAL
| STRING_LITERAL
;

type ::= primitive_type


| array_type
;

returntype ::= primitive_type


| array_return
;
primitive_type ::= INT
| FLOAT
| CHAR
| STRING
;

array_type ::= primitive_type dim_exprs


;

array_return ::= primitive_type dims


;

name ::= IDENTIFIER;

compilation_unit ::=
constant_declaration_opt
method_declarations
;

constant_declaration_opt ::= constant_declarations | ;

constant_declarations ::= constant_declaration


| constant_declarations constant_declaration
;

constant_declaration ::= CONST primitive_type IDENTIFIER EQ


constant_expression
;

variable_declarators ::=
variable_declarator
| variable_declarators COMMA variable_declarator
;
variable_declarator ::=
variable_declarator_id
| variable_declarator_id EQ variable_initializer
;
variable_declarator_id ::=
IDENTIFIER
;
variable_initializer ::=
expression
| array_initializer
;

method_declarations ::= method_declaration


| method_declarations method_declaration
;

method_declaration ::=
method_header method_body
;
method_header ::=
returntype method_declarator
| VOID method_declarator
;
method_declarator ::=
IDENTIFIER LPAREN formal_parameter_list_opt RPAREN
;
formal_parameter_list_opt ::=
| formal_parameter_list
;
formal_parameter_list ::=
formal_parameter
| formal_parameter_list COMMA formal_parameter
;
formal_parameter ::=
returntype variable_declarator_id
;

method_body ::= block


| DELIMITER
;

array_initializer ::= OPENBRACKET value_sets_opt CLOSEBRACKET;

value_sets_opt ::= value_sets | ;

value_sets ::= value_set


| value_sets COMMA value_set
;

value_set ::= OPENBRACKET value_list_opt CLOSEBRACKET;

value_list_opt ::= value_list | ;

value_list ::= variable_initializer


| value_list COMMA variable_initializer
;

block ::= OPENBRACKET block_statements_opt CLOSEBRACKET


;
block_statements_opt ::=
| block_statements
;
block_statements ::=
block_statement
| block_statements block_statement
;
block_statement ::=
local_variable_declaration_statement
| statement
;
local_variable_declaration_statement ::=
local_variable_declaration DELIMITER
;
local_variable_declaration ::=
type variable_declarators
;
statement ::= statement_without_trailing_substatement
| if_then_statement
| if_then_else_statement
| while_statement
| for_statement
| until_statement
;
statement_no_short_if ::=
statement_without_trailing_substatement
| if_then_else_statement_no_short_if
| while_statement_no_short_if
| for_statement_no_short_if
| until_statement_no_short_if
;
statement_without_trailing_substatement ::=
block
| input
| output
| empty_statement
| expression_statement
| do_statement
| do_until_statement
| break_statement
| continue_statement
| return_statement
;
empty_statement ::=
DELIMITER
;

expression_statement ::=
statement_expression DELIMITER
;
statement_expression ::=
assignment
| preincrement_expression
| predecrement_expression
| postincrement_expression
| postdecrement_expression
| method_invocation
;
if_then_statement ::=
IF LPAREN expression RPAREN statement
;
if_then_else_statement ::=
IF LPAREN expression RPAREN statement_no_short_if
ELSE statement
;
if_then_else_statement_no_short_if ::=
IF LPAREN expression RPAREN statement_no_short_if
ELSE statement_no_short_if
;

while_statement ::=
WHILE LPAREN expression RPAREN statement
;
while_statement_no_short_if ::=
WHILE LPAREN expression RPAREN statement_no_short_if
;

until_statement ::=
UNTIL LPAREN expression RPAREN statement
;

until_statement_no_short_if ::=
UNTIL LPAREN expression RPAREN statement_no_short_if
;

do_statement ::=
DO statement WHILE LPAREN expression RPAREN DELIMITER
;

do_until_statement ::=
DO statement UNTIL LPAREN expression RPAREN DELIMITER
;

for_statement ::=
FOR LPAREN for_init_opt TO expression DELIMITER
for_update_opt RPAREN statement
;
for_statement_no_short_if ::=
FOR LPAREN for_init_opt TO expression DELIMITER
for_update_opt RPAREN statement_no_short_if
;
for_init_opt ::=
| for_init
;
for_init ::= statement_expression_list
| local_variable_declaration
;
for_update_opt ::=
| for_update
;
for_update ::= statement_expression_list
;
statement_expression_list ::=
statement_expression
| statement_expression_list COMMA statement_expression
;

identifier_opt ::=
| IDENTIFIER
;

break_statement ::=
BREAK identifier_opt DELIMITER
;

continue_statement ::=
CONTINUE identifier_opt DELIMITER
;
return_statement ::=
RETURN expression_opt DELIMITER
;

primary ::=
literal
| LPAREN expression RPAREN
| method_invocation
| array_access
;

argument_list_opt ::=
| argument_list
;
argument_list ::=
expression
| argument_list COMMA expression
;
dim_exprs ::= dim_expr
| dim_exprs dim_expr
;
dim_expr ::= LBRACE expression RBRACE
;

dims ::= LBRACE RBRACE


| dims LBRACE RBRACE
;

method_invocation ::=
name LPAREN argument_list_opt RPAREN
;
array_access ::=
name LBRACE expression RBRACE
| primary LBRACE expression RBRACE
;
postfix_expression ::=
primary
| name
| postincrement_expression
| postdecrement_expression
;
postincrement_expression ::=
postfix_expression PLUSPLUS
;
postdecrement_expression ::=
postfix_expression MINUSMINUS
;
unary_expression ::=
preincrement_expression
| predecrement_expression
| PLUS unary_expression
| MINUS unary_expression
| unary_expression_not_plus_minus
;
preincrement_expression ::=
PLUSPLUS unary_expression
;
predecrement_expression ::=
MINUSMINUS unary_expression
;
unary_expression_not_plus_minus ::=
postfix_expression
| NOT unary_expression
;

multiplicative_expression ::=
unary_expression
| multiplicative_expression MULT unary_expression
| multiplicative_expression DIV unary_expression
| multiplicative_expression MOD unary_expression
;
additive_expression ::=
multiplicative_expression
| additive_expression PLUS multiplicative_expression
| additive_expression MINUS multiplicative_expression
;

relational_expression ::=
additive_expression
| relational_expression LT additive_expression
| relational_expression GT additive_expression
| relational_expression LTEQ additive_expression
| relational_expression GTEQ additive_expression
;
equality_expression ::=
relational_expression
| equality_expression EQEQ relational_expression
| equality_expression NOTEQ relational_expression
;

conditional_and_expression ::=
equality_expression
| conditional_and_expression ANDAND equality_expression
;
conditional_or_expression ::=
conditional_and_expression
| conditional_or_expression OROR conditional_and_expression
;
assignment_expression ::=
conditional_or_expression
| assignment
;
assignment ::= left_hand_side assignment_operator
assignment_expression
;
left_hand_side ::=
name
| array_access
;
assignment_operator ::=
EQ
| MULTEQ
| DIVEQ
| MODEQ
| PLUSEQ
| MINUSEQ
;
expression_opt ::=
| expression
;
expression ::= assignment_expression
;
constant_expression ::=
expression
;

input ::= READ name DELIMITER;

output ::= WRITE LPAREN expression RPAREN DELIMITER;


THE DESIGN OF THE IDE
EXAMPLE CODES
EXAMPLE OUTPUT

WITH THE INPUT OF AN INTEGER 23 ..


The example program above is about Binary Conversion in Recursive Form ..
it converts an integer number to binary number .
THE EXTENSION FILE NAME

- .cnba

You might also like