Subversion Repositories programming

Rev

Rev 229 | Rev 232 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * File: jminus.y
 * 
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
 * License: GNU General Public License v2
 *
 * This file contains a definition of the J-Minus language, and will
 * create a parser for the language when compiled using BYACC/J, which
 * is available from http://byaccj.sourceforge.net.
 ******************************************************************************/

%{
    import java.lang.*;
    import java.io.*;
    import java.util.StringTokenizer;
%}

/* Token Declarations */
%token CLASS
%token ID
%token PUBLIC
%token PRIVATE
%token INT
%token FLOAT
%token VOID
%token IF
%token ELSE
%token WHILE
%token RETURN
%token ADDOP
%token MULOP
%token NUM
%token LPAREN
%token RPAREN
%token LBRACE
%token RBRACE
%token SEMI
%token ASSIGNOP

/* J-Minus Grammar */
%%
program : modifier CLASS ID LBRACE method_declaration RBRACE
        { System.out.println ("program -> modifier class id { method_declaration }");
          System.out.println ("\nPARSING SUCCESSFUL: ACCEPT"); };

modifier : PUBLIC   { System.out.println ("modifier -> public"); }
    | PRIVATE       { System.out.println ("modifier -> private"); };

method_declaration : modifier type_specifier ID LPAREN param RPAREN
    LBRACE local_declaration stmt_sequence RBRACE
        { System.out.println ("method_declaration -> modifier type_specifier id (param) "
                             +"{ local_declaration stmt_sequence }"); };

type_specifier : INT { System.out.println ("type_specifier -> int"); }
    | FLOAT { System.out.println ("type_specifier -> float"); }
    | VOID  { System.out.println ("type_specifier -> void"); };

param : type_specifier ID { System.out.println ("param -> type_specifier id"); }
    | VOID  { System.out.println ("param -> void"); };

local_declaration : type_specifier ID SEMI
            { System.out.println ("local_declaration -> id;"); }
    |       { System.out.println ("local_declaration -> epsilon"); };

stmt_sequence : stmt_sequence statement
                { System.out.println ("stmt_sequence -> stmt_sequence statement"); }
    | statement { System.out.println ("stmt_sequence -> statement"); };

statement : if_stmt { System.out.println ("statement -> if_stmt"); }
    | while_stmt    { System.out.println ("statement -> while_stmt"); }
    | assign_stmt   { System.out.println ("statement -> assign_stmt"); }
    | return_stmt   { System.out.println ("statement -> return_stmt"); }
    | expr_stmt     { System.out.println ("statement -> expr_stmt"); };

if_stmt : IF LPAREN exp RPAREN statement ELSE statement
        { System.out.println ("if_stmt -> if (exp) statement else statement"); };

while_stmt : WHILE LPAREN exp RPAREN statement
        { System.out.println ("while_stmt -> (exp) statement"); };

assign_stmt : ID ASSIGNOP exp SEMI
        { System.out.println ("assign_stmt -> id = exp;"); };

return_stmt : RETURN exp SEMI   { System.out.println ("return_stmt -> return exp;"); }
    | RETURN SEMI               { System.out.println ("return_stmt -> return;"); };

expr_stmt : exp SEMI            { System.out.println ("expr_stmt -> exp;"); };

exp : exp ADDOP term            { System.out.println ("exp -> exp + term"); }
    | term                      { System.out.println ("exp -> term"); };

term : term MULOP factor        { System.out.println ("term -> term * factor"); }
    | factor                    { System.out.println ("term -> factor"); };

factor : LPAREN exp RPAREN      { System.out.println ("factor -> (exp)"); }
    | ADDOP factor              { System.out.println ("factor -> + factor"); }
    | NUM                       { System.out.println ("factor -> NUM"); }
    | ID                        { System.out.println ("factor -> ID"); };

%%

private Yylex lexer;

public void yyerror (String error)
{
    System.out.println ("Parse Error: " + error);
}

int yylex ()
{
    int lex_return = -1;

    try
    {
        lex_return = lexer.yylex();
    }
    catch (IOException e)
    {
        System.out.println ("IO Error: " + e);
    }

    return lex_return;
}

public Parser (Reader r)
{
    lexer = new Yylex (r, this);
}

public static void main (String[] args) throws Exception
{
    Parser yyparser = new Parser (new FileReader (args[0]));
    yyparser.yyparse();
}