SBSPSS/Utils/Scripter/parser.y

175 lines
4.5 KiB
Plaintext
Raw Normal View History

2000-12-08 21:42:35 +01:00
%{
/****************************************************************************
parser.y
ParserWizard generated YACC file.
Date: 07 December 2000
****************************************************************************/
2000-12-15 21:29:33 +01:00
#include "function.h"
2000-12-08 21:42:35 +01:00
#include "lexer.h"
#include "codegen.h"
%}
/////////////////////////////////////////////////////////////////////////////
// declarations section
// parser name
%name myparser
// class definition
{
2000-12-11 21:28:41 +01:00
public:
void setCurrentLexer(class mylexer *_lexer);
void setBaseNode(class CTreeNode *_baseNode);
// Overridden parser functions
void yyerror(const char *_text);
int yyparse();
private:
class mylexer *m_currentLexer;
class CTreeNode *m_baseNode;
2000-12-15 21:29:33 +01:00
// Ugh! (pkg)
class CTreeNode *m_functionArgs;
int m_functionNumber;
2000-12-08 21:42:35 +01:00
}
// constructor
{
// place any extra initialisation code here
}
//// attribute type
//%include {
//#ifndef YYSTYPE
//#define YYSTYPE int
//#endif
//}
%union {
2000-12-15 21:29:33 +01:00
int variableIdx;
signed short value;
int functionNumber;
class CTreeNode *treenode;
2000-12-08 21:42:35 +01:00
}
// place any declarations here
2000-12-11 21:28:41 +01:00
2000-12-08 21:42:35 +01:00
%token STOP
%token IF
%token ELSE
2000-12-20 21:15:24 +01:00
%token WHILE
%token DO
2000-12-08 21:42:35 +01:00
%token PAUSE
%token PRINT
%token ASSIGN
%token EQUAL
%token NOTEQUAL
2000-12-20 21:15:24 +01:00
%token LESSTHAN
%token GREATERTHAN
2000-12-08 21:42:35 +01:00
%token PLUS
2000-12-20 21:15:24 +01:00
%token SUBTRACT
2000-12-22 18:10:46 +01:00
%token MULTIPLY
%token DIVIDE
2000-12-08 21:42:35 +01:00
%token END_STMT
%token OPEN_PAR
%token CLOSE_PAR
%token BEGIN_CS
%token END_CS
2000-12-15 21:29:33 +01:00
%token COMMA
%token <variableIdx> VARIABLE
%token <value> VALUE
%token <functionNumber> FUNCTION
2000-12-08 21:42:35 +01:00
%type <treenode> program statement_list statement
%type <treenode> assign_expression
2001-01-02 17:02:35 +01:00
%type <treenode> conditional
2000-12-08 21:42:35 +01:00
%type <treenode> variable
2001-01-02 17:02:35 +01:00
%type <treenode> expression
2000-12-15 21:29:33 +01:00
%type <treenode> function
2000-12-08 21:42:35 +01:00
%%
/////////////////////////////////////////////////////////////////////////////
// rules section
// place your YACC rules here (there must be at least one)
program
2001-01-02 17:02:35 +01:00
:statement_list {s_baseTreeNode=$1;}
2000-12-08 21:42:35 +01:00
;
2000-12-11 21:28:41 +01:00
2000-12-08 21:42:35 +01:00
statement_list
2001-01-02 17:02:35 +01:00
:statement_list statement {$$=new CTreeNode(STMT_LIST,$1,$2);}
| {$$=new CTreeNode(EMPTY_STMT);}
2000-12-08 21:42:35 +01:00
;
statement
2001-01-02 17:02:35 +01:00
:END_STMT {$$=new CTreeNode(EMPTY_STMT);}
|STOP END_STMT {$$=new CTreeNode(STOP_STMT);}
|PAUSE END_STMT {$$=new CTreeNode(PAUSE_STMT);}
|PRINT OPEN_PAR expression CLOSE_PAR END_STMT {$$=new CTreeNode(PRINT_STMT,$3);}
|assign_expression END_STMT {$$=$1;}
|IF OPEN_PAR conditional CLOSE_PAR statement {$$=new CTreeNode(IF_STMT,$3,$5);}
|IF OPEN_PAR conditional CLOSE_PAR statement ELSE statement {$$=new CTreeNode(IFELSE_STMT,$3,$5,$7);}
|WHILE OPEN_PAR conditional CLOSE_PAR statement {$$=new CTreeNode(WHILE_STMT,$3,$5);}
|DO statement WHILE OPEN_PAR conditional CLOSE_PAR END_STMT {$$=new CTreeNode(DOWHILE_STMT,$2,$5);}
|BEGIN_CS statement_list END_CS {$$=new CTreeNode(STMT_LIST,$2);}
|function END_STMT {$$=new CTreeNode(STMT_LIST,$1,new CTreeNode(POP_STMT));}
2000-12-08 21:42:35 +01:00
;
assign_expression
2001-01-02 17:02:35 +01:00
:variable ASSIGN expression {$$=new CTreeNode(ASSIGN_EXPR,$1,$3);}
2000-12-08 21:42:35 +01:00
;
2001-01-02 17:02:35 +01:00
conditional
:expression EQUAL expression {$$=new CTreeNode(EQUAL_EXPR,$1,$3);}
|expression NOTEQUAL expression {$$=new CTreeNode(NOTEQUAL_EXPR,$1,$3);}
|expression LESSTHAN expression {$$=new CTreeNode(LESSTHAN_EXPR,$1,$3);}
|expression GREATERTHAN expression {$$=new CTreeNode(GREATERTHAN_EXPR,$1,$3);}
2000-12-20 21:15:24 +01:00
;
2000-12-08 21:42:35 +01:00
variable
2001-01-02 17:02:35 +01:00
:VARIABLE {$$=new CTreeNode(VARIABLE_EXPR,$1);} // variable id
2000-12-08 21:42:35 +01:00
;
2001-01-02 17:02:35 +01:00
expression
:VALUE {$$=new CTreeNode(VALUE_EXPR,$1);}
|OPEN_PAR expression CLOSE_PAR {$$=$2;}
|VARIABLE {$$=new CTreeNode(VARIABLE_EXPR,$1);} // variable value
// |PLUS expression {$$=$2;}
// |SUBTRACT expression {$$=new CTreeNode(STMT_LIST,CTreeNodeCTreeNode(VALUE_EXPR,$2);}
|expression PLUS expression {$$=new CTreeNode(PLUS_EXPR,$1,$3);}
|expression SUBTRACT expression {$$=new CTreeNode(SUBTRACT_EXPR,$1,$3);}
|expression MULTIPLY expression {$$=new CTreeNode(MULTIPLY_EXPR,$1,$3);}
|expression DIVIDE expression {$$=new CTreeNode(DIVIDE_EXPR,$1,$3);}
|function {$$=$1;}
2000-12-08 21:42:35 +01:00
;
2000-12-11 21:28:41 +01:00
2000-12-15 21:29:33 +01:00
function
2001-01-02 17:02:35 +01:00
:FUNCTION OPEN_PAR {if($1!=-1){m_functionNumber=$1;m_functionArgs=getFunctionArgs($1);}}
CLOSE_PAR {$$=new CTreeNode(STMT_LIST,m_functionArgs,new CTreeNode(FUNCTION_EXPR,m_functionNumber));}
2000-12-15 21:29:33 +01:00
;
2000-12-11 21:28:41 +01:00
2000-12-08 21:42:35 +01:00
%%
/////////////////////////////////////////////////////////////////////////////
// programs section