create hsmc repo
This commit is contained in:
commit
0506ec019b
26 files changed
+5413
No files matched your search
@@ -0,0 +1,77 @@
|
||||
# flags and libs
|
||||
DEFS =
|
||||
LIBS =
|
||||
|
||||
# compiler and flags
|
||||
CC = cc
|
||||
CCFLAGS = -Wall -O3 -c
|
||||
CXX = c++
|
||||
CXXFLAGS = $(CCFLAGS)
|
||||
CPPFLAGS =
|
||||
|
||||
# linker
|
||||
LD = c++
|
||||
LDFLAGS =
|
||||
STRIP = strip
|
||||
|
||||
# misc programs
|
||||
RM = rm -f
|
||||
INSTALL = install
|
||||
MKDEPEND = mkdepend
|
||||
LEX = flex -f
|
||||
YACC = yacc
|
||||
|
||||
# our target and source code
|
||||
TARGET = hsmc
|
||||
SRCS = hsm-lexer.c hsm-parser.tab.c getopt.cpp main.cpp
|
||||
OBJS = hsm-lexer.o hsm-parser.tab.o getopt.o main.o
|
||||
|
||||
##
|
||||
##
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(LD) $(LDFLAGS) $(LIBS) -o $@ $(OBJS)
|
||||
|
||||
hsm-parser.tab.h: \
|
||||
hsm.y
|
||||
|
||||
hsm-parser.tab.c: hsm.y
|
||||
$(YACC) -d -bhsm-parser hsm.y
|
||||
|
||||
hsm-lexer.h: \
|
||||
hsm.l
|
||||
|
||||
hsm-lexer.c: hsm.l
|
||||
$(LEX) -ohsm-lexer.c hsm.l
|
||||
|
||||
depend:
|
||||
$(MKDEPEND) $(SRCS)
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS) $(TARGET)
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(DEFS) $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CCFLAGS) $(CPPFLAGS) $(DEFS) $< -o $@
|
||||
|
||||
|
||||
# DO NOT DELETE THIS LINE -- make depend depends on it.
|
||||
|
||||
hsm-lexer.o: \
|
||||
hsm-parser.tab.h
|
||||
|
||||
hsm-parser.o: \
|
||||
main.h
|
||||
|
||||
main.o: \
|
||||
autofile.h \
|
||||
config.h \
|
||||
getopt.h \
|
||||
machine.h \
|
||||
main.h \
|
||||
state.h
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef __autofile_h__
|
||||
#define __autofile_h__
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
class AutoFILE
|
||||
{
|
||||
public:
|
||||
AutoFILE(char* filename, char* mode = "r")
|
||||
{
|
||||
f = fopen(filename,mode);
|
||||
}
|
||||
~AutoFILE()
|
||||
{
|
||||
if ( Opened() )
|
||||
fclose(f);
|
||||
}
|
||||
operator FILE* ()
|
||||
{
|
||||
return f;
|
||||
}
|
||||
bool Opened()
|
||||
{
|
||||
return f != (FILE*)0;
|
||||
}
|
||||
|
||||
private:
|
||||
FILE* f;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // __autofile_h__
|
||||
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
#ifndef __config_h__
|
||||
#define __config_h__
|
||||
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include "getopt.h"
|
||||
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
|
||||
std::string InFileName;
|
||||
std::string OutCPPFileName;
|
||||
std::string OutHFileName;
|
||||
std::string IncludeName;
|
||||
bool Debug;
|
||||
bool Quiet;
|
||||
bool PureVirtuals;
|
||||
|
||||
void Usage()
|
||||
{
|
||||
printf(
|
||||
"\n"
|
||||
"\nHSMC - Hierarchical State Machine Compiler\n"
|
||||
"\n"
|
||||
"Usage: hsmc [-hdpq] [-o prefix] source_file\n"
|
||||
"\n"
|
||||
" -h this help\n"
|
||||
" -d include debugging information\n"
|
||||
" -p actions declared as pure virtual\n"
|
||||
" -q quiet mode (only output errors)\n"
|
||||
" -o prefix specify prefix for output files [prefix.cpp,prefix.h]\n"
|
||||
" source_file HSM source file\n"
|
||||
"\n"
|
||||
);
|
||||
}
|
||||
|
||||
bool ParseCommandLine(int argc, char* argv[])
|
||||
{
|
||||
// reset defaults
|
||||
InFileName = "";
|
||||
OutCPPFileName = "";
|
||||
OutHFileName = "";
|
||||
IncludeName = "";
|
||||
Debug = false;
|
||||
Quiet = false;
|
||||
PureVirtuals = false;
|
||||
|
||||
// temporary
|
||||
std::string prefix = "";
|
||||
|
||||
// parse command line
|
||||
int ch;
|
||||
while ( (ch = getopt(argc,argv,"hdpqo:")) != -1 )
|
||||
{
|
||||
switch ( ch )
|
||||
{
|
||||
case 'p':
|
||||
PureVirtuals = true;
|
||||
break;
|
||||
|
||||
case 'q':
|
||||
Quiet = true;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
Debug = true;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
prefix = optarg;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
default:
|
||||
Usage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// check for input file
|
||||
if ( (argc-optind) == 0 )
|
||||
{
|
||||
printf("ERROR: no input file specified\n");
|
||||
Usage();
|
||||
return false;
|
||||
}
|
||||
else if ( (argc-optind) > 1 )
|
||||
{
|
||||
printf("ERROR: multiple input files not allowed\n");
|
||||
Usage();
|
||||
return false;
|
||||
}
|
||||
|
||||
// got input file
|
||||
InFileName = argv[optind];
|
||||
|
||||
// determine output prefix if it wasn't specified
|
||||
if ( prefix == "" )
|
||||
{
|
||||
// prefix NOT specified, let's get it from the base
|
||||
prefix = InFileName.substr(0,InFileName.rfind('.'));
|
||||
}
|
||||
|
||||
// determine output files specified
|
||||
OutCPPFileName = prefix + ".cpp";
|
||||
OutHFileName = prefix + ".h";
|
||||
|
||||
// strip up to any '/' or '\'
|
||||
IncludeName = OutHFileName;
|
||||
if ( OutHFileName.rfind('/') >= 0 )
|
||||
{
|
||||
IncludeName = IncludeName.substr( IncludeName.rfind('/')+1 );
|
||||
}
|
||||
if ( OutHFileName.rfind('\\') >= 0 )
|
||||
{
|
||||
IncludeName = IncludeName.substr( IncludeName.rfind('\\')+1 );
|
||||
}
|
||||
|
||||
// success
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif // __config_h__
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright (c) 1987, 1993, 1994
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
|
||||
#endif
|
||||
static const char rcsid[] = "$FreeBSD: src/lib/libc/stdlib/getopt.c,v 1.2 1999/12/17 01:52:15 jkh Exp $";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int opterr = 1; /* if error message should be printed */
|
||||
int optind = 1; /* index into parent argv vector */
|
||||
int optopt; /* character checked for validity */
|
||||
int optreset; /* reset getopt */
|
||||
char *optarg; /* argument associated with option */
|
||||
|
||||
|
||||
#define BADCH (int)'?'
|
||||
#define BADARG (int)':'
|
||||
#define EMSG ""
|
||||
|
||||
/*
|
||||
* getopt --
|
||||
* Parse argc/argv argument vector.
|
||||
*/
|
||||
#if defined(__cplusplus)
|
||||
extern "C"
|
||||
#endif
|
||||
int getopt(int nargc, char* const* nargv, const char* ostr)
|
||||
{
|
||||
char *__progname = nargv[0];
|
||||
static char *place = EMSG; /* option letter processing */
|
||||
char *oli; /* option letter list index */
|
||||
int ret;
|
||||
|
||||
if ( optreset || !*place )
|
||||
{ /* update scanning pointer */
|
||||
optreset = 0;
|
||||
if ( optind >= nargc || *(place = nargv[optind]) != '-' )
|
||||
{
|
||||
place = EMSG;
|
||||
return(-1);
|
||||
}
|
||||
if ( place[1] && *++place == '-' )
|
||||
{ /* found "--" */
|
||||
++optind;
|
||||
place = EMSG;
|
||||
return(-1);
|
||||
}
|
||||
} /* option letter okay? */
|
||||
if ( (optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr, optopt)) )
|
||||
{
|
||||
/*
|
||||
* if the user didn't specify '-' as an option,
|
||||
* assume it means -1.
|
||||
*/
|
||||
if ( optopt == (int)'-' )
|
||||
return(-1);
|
||||
if ( !*place )
|
||||
++optind;
|
||||
if ( opterr && *ostr != ':' )
|
||||
(void)fprintf(stderr,"%s: illegal option -- %c\n", __progname, optopt);
|
||||
return(BADCH);
|
||||
}
|
||||
if ( *++oli != ':' )
|
||||
{
|
||||
/* don't need argument */
|
||||
optarg = NULL;
|
||||
if ( !*place )
|
||||
{
|
||||
++optind;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* need an argument */
|
||||
if ( *place )
|
||||
{
|
||||
/* no white space */
|
||||
optarg = place;
|
||||
}
|
||||
else if ( nargc <= ++optind )
|
||||
{
|
||||
/* no arg */
|
||||
place = EMSG;
|
||||
if ( *ostr == ':' )
|
||||
ret = BADARG;
|
||||
else
|
||||
ret = BADCH;
|
||||
if ( opterr )
|
||||
(void)fprintf(stderr,"%s: option requires an argument -- %c\n",__progname, optopt);
|
||||
return(ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* white space */
|
||||
optarg = nargv[optind];
|
||||
}
|
||||
place = EMSG;
|
||||
++optind;
|
||||
}
|
||||
return(optopt); /* dump back option letter */
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef __getopt_h__
|
||||
#define __getopt_h__
|
||||
|
||||
extern int opterr; /* if error message should be printed */
|
||||
extern int optind; /* index into parent argv vector */
|
||||
extern int optopt; /* character checked for validity */
|
||||
extern int optreset; /* reset getopt */
|
||||
extern char *optarg; /* argument associated with option */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int getopt(int nargc, char* const* nargv, const char* ostr);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // __getopt_h__
|
||||
+1684
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,469 @@
|
||||
#ifndef lint
|
||||
static char yysccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93";
|
||||
#endif
|
||||
#define YYBYACC 1
|
||||
#define YYMAJOR 1
|
||||
#define YYMINOR 9
|
||||
#define yyclearin (yychar=(-1))
|
||||
#define yyerrok (yyerrflag=0)
|
||||
#define YYRECOVERING (yyerrflag!=0)
|
||||
#define YYPREFIX "yy"
|
||||
#line 2 "hsm.y"
|
||||
|
||||
|
||||
extern int yylex();
|
||||
extern void yyerror(char* s);
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#line 11 "hsm.y"
|
||||
typedef union
|
||||
{
|
||||
char string[258];
|
||||
float constant;
|
||||
} YYSTYPE;
|
||||
#line 26 "hsm-parser.tab.c"
|
||||
#define MACHINE 257
|
||||
#define STATE 258
|
||||
#define DEFAULT 259
|
||||
#define ENTRY 260
|
||||
#define IDLE 261
|
||||
#define EXIT 262
|
||||
#define TRANSITION 263
|
||||
#define ACTION 264
|
||||
#define TERMINATE 265
|
||||
#define IDENTIFIER 266
|
||||
#define CONSTANT 267
|
||||
#define YYERRCODE 256
|
||||
short yylhs[] = { -1,
|
||||
0, 0, 1, 3, 3, 4, 4, 5, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 2, 7, 8,
|
||||
9, 10, 11, 12, 13, 14, 15, 16,
|
||||
};
|
||||
short yylen[] = { 2,
|
||||
1, 2, 4, 1, 2, 1, 1, 4, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 6, 4, 4,
|
||||
4, 4, 4, 6, 6, 6, 6, 4,
|
||||
};
|
||||
short yydefred[] = { 0,
|
||||
0, 0, 1, 0, 0, 2, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 4, 6, 7,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
|
||||
5, 0, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
17, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 18, 19, 20, 21, 22, 23, 0,
|
||||
0, 0, 0, 28, 8, 0, 0, 0, 0, 24,
|
||||
26, 25, 27,
|
||||
};
|
||||
short yydgoto[] = { 2,
|
||||
3, 4, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
25, 26, 27, 28, 29, 30,
|
||||
};
|
||||
short yysindex[] = { -251,
|
||||
-33, -251, 0, -106, -248, 0, -249, -25, -20, -19,
|
||||
-18, -17, -15, -14, -13, -12, -125, 0, 0, 0,
|
||||
-99, -30, -29, -28, -27, -26, -24, -23, -22, -21,
|
||||
-233, -227, -226, -225, -224, -223, -264, -262, -222, 0,
|
||||
0, -249, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 4, 5, 7, 8, 9, 10, 11, 12, 13,
|
||||
14, 18, -117, 0, 0, 0, 0, 0, 0, -214,
|
||||
-213, -212, -206, 0, 0, 20, 21, 22, 23, 0,
|
||||
0, 0, 0,
|
||||
};
|
||||
short yyrindex[] = { 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0,
|
||||
};
|
||||
short yygindex[] = { 0,
|
||||
63, 0, 24, -16, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
};
|
||||
#define YYTABLESIZE 148
|
||||
short yytable[] = { 40,
|
||||
41, 58, 59, 60, 61, 1, 5, 75, 9, 10,
|
||||
11, 12, 13, 14, 15, 16, 7, 8, 31, 32,
|
||||
33, 34, 35, 42, 36, 37, 38, 39, 43, 44,
|
||||
45, 46, 47, 52, 48, 49, 50, 51, 53, 54,
|
||||
55, 56, 57, 62, 64, 65, 41, 66, 67, 68,
|
||||
69, 76, 77, 78, 70, 71, 72, 73, 74, 79,
|
||||
80, 81, 82, 83, 6, 63, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
9, 10, 11, 12, 13, 14, 15, 16,
|
||||
};
|
||||
short yycheck[] = { 125,
|
||||
17, 266, 267, 266, 267, 257, 40, 125, 258, 259,
|
||||
260, 261, 262, 263, 264, 265, 123, 266, 44, 40,
|
||||
40, 40, 40, 123, 40, 40, 40, 40, 59, 59,
|
||||
59, 59, 59, 267, 59, 59, 59, 59, 266, 266,
|
||||
266, 266, 266, 266, 41, 41, 63, 41, 41, 41,
|
||||
41, 266, 266, 266, 44, 44, 44, 44, 41, 266,
|
||||
41, 41, 41, 41, 2, 42, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, 258, 259, 260, 261, 262, 263, 264, 265,
|
||||
258, 259, 260, 261, 262, 263, 264, 265,
|
||||
};
|
||||
#define YYFINAL 2
|
||||
#ifndef YYDEBUG
|
||||
#define YYDEBUG 0
|
||||
#endif
|
||||
#define YYMAXTOKEN 267
|
||||
#if YYDEBUG
|
||||
char *yyname[] = {
|
||||
"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,"'('","')'",0,0,"','",0,0,0,0,0,0,0,0,0,0,0,0,0,0,"';'",0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"'{'",0,"'}'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"MACHINE",
|
||||
"STATE","DEFAULT","ENTRY","IDLE","EXIT","TRANSITION","ACTION","TERMINATE",
|
||||
"IDENTIFIER","CONSTANT",
|
||||
};
|
||||
char *yyrule[] = {
|
||||
"$accept : machines",
|
||||
"machines : machine",
|
||||
"machines : machines machine",
|
||||
"machine : machine_decl '{' state_items '}'",
|
||||
"state_items : state_item",
|
||||
"state_items : state_items state_item",
|
||||
"state_item : state",
|
||||
"state_item : statement",
|
||||
"state : state_decl '{' state_items '}'",
|
||||
"statement : default ';'",
|
||||
"statement : entry ';'",
|
||||
"statement : idle ';'",
|
||||
"statement : exit ';'",
|
||||
"statement : transition ';'",
|
||||
"statement : action ';'",
|
||||
"statement : timetransition ';'",
|
||||
"statement : timeaction ';'",
|
||||
"statement : terminate ';'",
|
||||
"machine_decl : MACHINE '(' IDENTIFIER ',' CONSTANT ')'",
|
||||
"state_decl : STATE '(' IDENTIFIER ')'",
|
||||
"default : DEFAULT '(' IDENTIFIER ')'",
|
||||
"entry : ENTRY '(' IDENTIFIER ')'",
|
||||
"idle : IDLE '(' IDENTIFIER ')'",
|
||||
"exit : EXIT '(' IDENTIFIER ')'",
|
||||
"transition : TRANSITION '(' IDENTIFIER ',' IDENTIFIER ')'",
|
||||
"action : ACTION '(' IDENTIFIER ',' IDENTIFIER ')'",
|
||||
"timetransition : TRANSITION '(' CONSTANT ',' IDENTIFIER ')'",
|
||||
"timeaction : ACTION '(' CONSTANT ',' IDENTIFIER ')'",
|
||||
"terminate : TERMINATE '(' IDENTIFIER ')'",
|
||||
};
|
||||
#endif
|
||||
#ifdef YYSTACKSIZE
|
||||
#undef YYMAXDEPTH
|
||||
#define YYMAXDEPTH YYSTACKSIZE
|
||||
#else
|
||||
#ifdef YYMAXDEPTH
|
||||
#define YYSTACKSIZE YYMAXDEPTH
|
||||
#else
|
||||
#define YYSTACKSIZE 500
|
||||
#define YYMAXDEPTH 500
|
||||
#endif
|
||||
#endif
|
||||
int yydebug;
|
||||
int yynerrs;
|
||||
int yyerrflag;
|
||||
int yychar;
|
||||
short *yyssp;
|
||||
YYSTYPE *yyvsp;
|
||||
YYSTYPE yyval;
|
||||
YYSTYPE yylval;
|
||||
short yyss[YYSTACKSIZE];
|
||||
YYSTYPE yyvs[YYSTACKSIZE];
|
||||
#define yystacksize YYSTACKSIZE
|
||||
#define YYABORT goto yyabort
|
||||
#define YYREJECT goto yyabort
|
||||
#define YYACCEPT goto yyaccept
|
||||
#define YYERROR goto yyerrlab
|
||||
int
|
||||
yyparse()
|
||||
{
|
||||
register int yym, yyn, yystate;
|
||||
#if YYDEBUG
|
||||
register char *yys;
|
||||
extern char *getenv();
|
||||
|
||||
if (yys = getenv("YYDEBUG"))
|
||||
{
|
||||
yyn = *yys;
|
||||
if (yyn >= '0' && yyn <= '9')
|
||||
yydebug = yyn - '0';
|
||||
}
|
||||
#endif
|
||||
|
||||
yynerrs = 0;
|
||||
yyerrflag = 0;
|
||||
yychar = (-1);
|
||||
|
||||
yyssp = yyss;
|
||||
yyvsp = yyvs;
|
||||
*yyssp = yystate = 0;
|
||||
|
||||
yyloop:
|
||||
if (yyn = yydefred[yystate]) goto yyreduce;
|
||||
if (yychar < 0)
|
||||
{
|
||||
if ((yychar = yylex()) < 0) yychar = 0;
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
{
|
||||
yys = 0;
|
||||
if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
|
||||
if (!yys) yys = "illegal-symbol";
|
||||
printf("%sdebug: state %d, reading %d (%s)\n",
|
||||
YYPREFIX, yystate, yychar, yys);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
|
||||
yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
|
||||
{
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
printf("%sdebug: state %d, shifting to state %d\n",
|
||||
YYPREFIX, yystate, yytable[yyn]);
|
||||
#endif
|
||||
if (yyssp >= yyss + yystacksize - 1)
|
||||
{
|
||||
goto yyoverflow;
|
||||
}
|
||||
*++yyssp = yystate = yytable[yyn];
|
||||
*++yyvsp = yylval;
|
||||
yychar = (-1);
|
||||
if (yyerrflag > 0) --yyerrflag;
|
||||
goto yyloop;
|
||||
}
|
||||
if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
|
||||
yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
|
||||
{
|
||||
yyn = yytable[yyn];
|
||||
goto yyreduce;
|
||||
}
|
||||
if (yyerrflag) goto yyinrecovery;
|
||||
#ifdef lint
|
||||
goto yynewerror;
|
||||
#endif
|
||||
yynewerror:
|
||||
yyerror("syntax error");
|
||||
#ifdef lint
|
||||
goto yyerrlab;
|
||||
#endif
|
||||
yyerrlab:
|
||||
++yynerrs;
|
||||
yyinrecovery:
|
||||
if (yyerrflag < 3)
|
||||
{
|
||||
yyerrflag = 3;
|
||||
for (;;)
|
||||
{
|
||||
if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
|
||||
yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
|
||||
{
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
printf("%sdebug: state %d, error recovery shifting\
|
||||
to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
|
||||
#endif
|
||||
if (yyssp >= yyss + yystacksize - 1)
|
||||
{
|
||||
goto yyoverflow;
|
||||
}
|
||||
*++yyssp = yystate = yytable[yyn];
|
||||
*++yyvsp = yylval;
|
||||
goto yyloop;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
printf("%sdebug: error recovery discarding state %d\n",
|
||||
YYPREFIX, *yyssp);
|
||||
#endif
|
||||
if (yyssp <= yyss) goto yyabort;
|
||||
--yyssp;
|
||||
--yyvsp;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (yychar == 0) goto yyabort;
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
{
|
||||
yys = 0;
|
||||
if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
|
||||
if (!yys) yys = "illegal-symbol";
|
||||
printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
|
||||
YYPREFIX, yystate, yychar, yys);
|
||||
}
|
||||
#endif
|
||||
yychar = (-1);
|
||||
goto yyloop;
|
||||
}
|
||||
yyreduce:
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
printf("%sdebug: state %d, reducing by rule %d (%s)\n",
|
||||
YYPREFIX, yystate, yyn, yyrule[yyn]);
|
||||
#endif
|
||||
yym = yylen[yyn];
|
||||
yyval = yyvsp[1-yym];
|
||||
switch (yyn)
|
||||
{
|
||||
case 3:
|
||||
#line 37 "hsm.y"
|
||||
{
|
||||
parseEndMachine();
|
||||
}
|
||||
break;
|
||||
case 8:
|
||||
#line 51 "hsm.y"
|
||||
{
|
||||
parseEndState();
|
||||
}
|
||||
break;
|
||||
case 18:
|
||||
#line 68 "hsm.y"
|
||||
{
|
||||
parseBeginMachine(yyvsp[-3].string,(int)yyvsp[-1].constant);
|
||||
}
|
||||
break;
|
||||
case 19:
|
||||
#line 74 "hsm.y"
|
||||
{
|
||||
parseBeginState(yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 20:
|
||||
#line 80 "hsm.y"
|
||||
{
|
||||
parseDefault(yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 21:
|
||||
#line 86 "hsm.y"
|
||||
{
|
||||
parseEntry(yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 22:
|
||||
#line 92 "hsm.y"
|
||||
{
|
||||
parseIdle(yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 23:
|
||||
#line 98 "hsm.y"
|
||||
{
|
||||
parseExit(yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 24:
|
||||
#line 104 "hsm.y"
|
||||
{
|
||||
parseTransition(yyvsp[-3].string,yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 25:
|
||||
#line 110 "hsm.y"
|
||||
{
|
||||
parseAction(yyvsp[-3].string,yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 26:
|
||||
#line 116 "hsm.y"
|
||||
{
|
||||
parseTimeTransition(yyvsp[-3].constant,yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 27:
|
||||
#line 122 "hsm.y"
|
||||
{
|
||||
parseTimeAction(yyvsp[-3].constant,yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
case 28:
|
||||
#line 128 "hsm.y"
|
||||
{
|
||||
parseTerminate(yyvsp[-1].string);
|
||||
}
|
||||
break;
|
||||
#line 414 "hsm-parser.tab.c"
|
||||
}
|
||||
yyssp -= yym;
|
||||
yystate = *yyssp;
|
||||
yyvsp -= yym;
|
||||
yym = yylhs[yyn];
|
||||
if (yystate == 0 && yym == 0)
|
||||
{
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
printf("%sdebug: after reduction, shifting from state 0 to\
|
||||
state %d\n", YYPREFIX, YYFINAL);
|
||||
#endif
|
||||
yystate = YYFINAL;
|
||||
*++yyssp = YYFINAL;
|
||||
*++yyvsp = yyval;
|
||||
if (yychar < 0)
|
||||
{
|
||||
if ((yychar = yylex()) < 0) yychar = 0;
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
{
|
||||
yys = 0;
|
||||
if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
|
||||
if (!yys) yys = "illegal-symbol";
|
||||
printf("%sdebug: state %d, reading %d (%s)\n",
|
||||
YYPREFIX, YYFINAL, yychar, yys);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (yychar == 0) goto yyaccept;
|
||||
goto yyloop;
|
||||
}
|
||||
if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
|
||||
yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
|
||||
yystate = yytable[yyn];
|
||||
else
|
||||
yystate = yydgoto[yym];
|
||||
#if YYDEBUG
|
||||
if (yydebug)
|
||||
printf("%sdebug: after reduction, shifting from state %d \
|
||||
to state %d\n", YYPREFIX, *yyssp, yystate);
|
||||
#endif
|
||||
if (yyssp >= yyss + yystacksize - 1)
|
||||
{
|
||||
goto yyoverflow;
|
||||
}
|
||||
*++yyssp = yystate;
|
||||
*++yyvsp = yyval;
|
||||
goto yyloop;
|
||||
yyoverflow:
|
||||
yyerror("yacc stack overflow");
|
||||
yyabort:
|
||||
return (1);
|
||||
yyaccept:
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#define MACHINE 257
|
||||
#define STATE 258
|
||||
#define DEFAULT 259
|
||||
#define ENTRY 260
|
||||
#define IDLE 261
|
||||
#define EXIT 262
|
||||
#define TRANSITION 263
|
||||
#define ACTION 264
|
||||
#define TERMINATE 265
|
||||
#define IDENTIFIER 266
|
||||
#define CONSTANT 267
|
||||
typedef union
|
||||
{
|
||||
char string[258];
|
||||
float constant;
|
||||
} YYSTYPE;
|
||||
extern YYSTYPE yylval;
|
||||
@@ -0,0 +1,86 @@
|
||||
%{
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "hsm-parser.tab.h"
|
||||
|
||||
int lineno = 1;
|
||||
|
||||
void yyerror(char* s);
|
||||
|
||||
|
||||
%}
|
||||
|
||||
|
||||
%%
|
||||
|
||||
/* whitespace */
|
||||
|
||||
[ \t]+ ; /* skip whitespace */
|
||||
|
||||
|
||||
|
||||
/* comments */
|
||||
|
||||
"//".* ; /* skip C++ comments */
|
||||
|
||||
|
||||
/* punctuation */
|
||||
|
||||
"," { return ','; }
|
||||
";" { return ';'; }
|
||||
"{" { return '{'; }
|
||||
"}" { return '}'; }
|
||||
"(" { return '('; }
|
||||
")" { return ')'; }
|
||||
|
||||
|
||||
/* keywords */
|
||||
|
||||
[Mm][Aa][Cc][Hh][Ii][Nn][Ee] { return MACHINE; }
|
||||
[Ss][Tt][Aa][Tt][Ee] { return STATE; }
|
||||
[Dd][Ee][Ff][Aa][Uu][Ll][Tt] { return DEFAULT; }
|
||||
[Ee][Nn][Tt][Rr][Yy] { return ENTRY; }
|
||||
[Ii][Dd][Ll][Ee] { return IDLE; }
|
||||
[Ee][Xx][Ii][Tt] { return EXIT; }
|
||||
[Tt][Rr][Aa][Nn][Ss][Ii][Tt][Ii][Oo][Nn] { return TRANSITION; }
|
||||
[Aa][Cc][Tt][Ii][Oo][Nn] { return ACTION; }
|
||||
[Tt][Ee][Rr][Mm][Ii][Nn][Aa][Tt][Ee] { return TERMINATE; }
|
||||
|
||||
|
||||
/* identifiers */
|
||||
|
||||
[a-zA-Z_][a-zA-Z_0-9]* { if ( strlen(yytext) > 256 ) printf("%d: truncating symbol '%s' to 256 characters\n",lineno,yytext); strncpy(yylval.string,yytext,256); return IDENTIFIER; }
|
||||
|
||||
|
||||
/* constants */
|
||||
|
||||
[0-9]+\.?[0-9]* { yylval.constant = (float)atof(yytext); return CONSTANT; }
|
||||
|
||||
|
||||
/* newlines, etc. */
|
||||
|
||||
\n { lineno++; }
|
||||
. ; /* ignore */
|
||||
|
||||
|
||||
%%
|
||||
|
||||
|
||||
|
||||
int yywrap()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
void yyerror(char* s)
|
||||
{
|
||||
printf("\n%d: %s at %s\n", lineno, s, yytext);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
%{
|
||||
|
||||
|
||||
extern int yylex();
|
||||
extern void yyerror(char* s);
|
||||
|
||||
#include "main.h"
|
||||
|
||||
%}
|
||||
|
||||
%union
|
||||
{
|
||||
char string[258];
|
||||
float constant;
|
||||
}
|
||||
|
||||
%token MACHINE
|
||||
%token STATE
|
||||
%token DEFAULT
|
||||
%token ENTRY
|
||||
%token IDLE
|
||||
%token EXIT
|
||||
%token TRANSITION
|
||||
%token ACTION
|
||||
%token TERMINATE
|
||||
|
||||
%token <string> IDENTIFIER
|
||||
%token <constant> CONSTANT
|
||||
|
||||
%%
|
||||
|
||||
machines: machine
|
||||
| machines machine
|
||||
;
|
||||
|
||||
machine: machine_decl '{' state_items '}'
|
||||
{
|
||||
parseEndMachine();
|
||||
}
|
||||
;
|
||||
|
||||
state_items: state_item
|
||||
| state_items state_item
|
||||
;
|
||||
|
||||
state_item: state
|
||||
| statement
|
||||
;
|
||||
|
||||
state: state_decl '{' state_items '}'
|
||||
{
|
||||
parseEndState();
|
||||
}
|
||||
;
|
||||
|
||||
statement: default ';'
|
||||
| entry ';'
|
||||
| idle ';'
|
||||
| exit ';'
|
||||
| transition ';'
|
||||
| action ';'
|
||||
| timetransition ';'
|
||||
| timeaction ';'
|
||||
| terminate ';'
|
||||
;
|
||||
|
||||
machine_decl: MACHINE '(' IDENTIFIER ',' CONSTANT ')'
|
||||
{
|
||||
parseBeginMachine($3,(int)$5);
|
||||
}
|
||||
;
|
||||
|
||||
state_decl: STATE '(' IDENTIFIER ')'
|
||||
{
|
||||
parseBeginState($3);
|
||||
}
|
||||
;
|
||||
|
||||
default: DEFAULT '(' IDENTIFIER ')'
|
||||
{
|
||||
parseDefault($3);
|
||||
}
|
||||
;
|
||||
|
||||
entry: ENTRY '(' IDENTIFIER ')'
|
||||
{
|
||||
parseEntry($3);
|
||||
}
|
||||
;
|
||||
|
||||
idle: IDLE '(' IDENTIFIER ')'
|
||||
{
|
||||
parseIdle($3);
|
||||
}
|
||||
;
|
||||
|
||||
exit: EXIT '(' IDENTIFIER ')'
|
||||
{
|
||||
parseExit($3);
|
||||
}
|
||||
;
|
||||
|
||||
transition: TRANSITION '(' IDENTIFIER ',' IDENTIFIER ')'
|
||||
{
|
||||
parseTransition($3,$5);
|
||||
}
|
||||
;
|
||||
|
||||
action: ACTION '(' IDENTIFIER ',' IDENTIFIER ')'
|
||||
{
|
||||
parseAction($3,$5);
|
||||
}
|
||||
;
|
||||
|
||||
timetransition: TRANSITION '(' CONSTANT ',' IDENTIFIER ')'
|
||||
{
|
||||
parseTimeTransition($3,$5);
|
||||
}
|
||||
;
|
||||
|
||||
timeaction: ACTION '(' CONSTANT ',' IDENTIFIER ')'
|
||||
{
|
||||
parseTimeAction($3,$5);
|
||||
}
|
||||
;
|
||||
|
||||
terminate: TERMINATE '(' IDENTIFIER ')'
|
||||
{
|
||||
parseTerminate($3);
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
|
||||
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
[Project]
|
||||
FileName=hsmc.dev
|
||||
Name=hsmc
|
||||
UnitCount=11
|
||||
Type=1
|
||||
Ver=1
|
||||
ObjFiles=
|
||||
Includes=
|
||||
Libs=
|
||||
PrivateResource=
|
||||
ResourceIncludes=
|
||||
MakeIncludes=
|
||||
Compiler=
|
||||
CppCompiler=
|
||||
Linker=
|
||||
IsCpp=1
|
||||
Icon=
|
||||
ExeOutput=
|
||||
ObjectOutput=
|
||||
OverrideOutput=0
|
||||
OverrideOutputName=hsmc.exe
|
||||
HostApplication=
|
||||
Folders=
|
||||
CommandLine=../example/parsetest/parsetest.hsm
|
||||
IncludeVersionInfo=0
|
||||
SupportXPThemes=0
|
||||
CompilerSet=0
|
||||
CompilerSettings=0000000000000001000
|
||||
UseCustomMakefile=0
|
||||
CustomMakefile=
|
||||
|
||||
[Unit1]
|
||||
FileName=autofile.h
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit2]
|
||||
FileName=config.h
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit3]
|
||||
FileName=getopt.cpp
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit4]
|
||||
FileName=getopt.h
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit5]
|
||||
FileName=hsm-lexer.c
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit6]
|
||||
FileName=hsm-parser.tab.c
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit7]
|
||||
FileName=hsm-parser.tab.h
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit8]
|
||||
FileName=machine.h
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit9]
|
||||
FileName=main.cpp
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit10]
|
||||
FileName=main.h
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit11]
|
||||
FileName=state.h
|
||||
CompileCpp=1
|
||||
Folder=hsmc
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[VersionInfo]
|
||||
Major=0
|
||||
Minor=1
|
||||
Release=1
|
||||
Build=1
|
||||
LanguageID=1033
|
||||
CharsetID=1252
|
||||
CompanyName=
|
||||
FileVersion=
|
||||
FileDescription=Developed using the Dev-C++ IDE
|
||||
InternalName=
|
||||
LegalCopyright=
|
||||
LegalTrademarks=
|
||||
OriginalFilename=
|
||||
ProductName=
|
||||
ProductVersion=
|
||||
AutoIncBuildNr=0
|
||||
|
||||
@@ -0,0 +1,488 @@
|
||||
#ifndef __machine_h__
|
||||
#define __machine_h__
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include "state.h"
|
||||
|
||||
class Machine
|
||||
{
|
||||
public:
|
||||
|
||||
// data
|
||||
std::string name;
|
||||
|
||||
std::vector<State> states;
|
||||
std::map<std::string,int> statemap;
|
||||
|
||||
std::set<std::string> event_refs;
|
||||
std::set<std::string> state_refs;
|
||||
std::set<std::string> action_refs;
|
||||
|
||||
// construction
|
||||
Machine();
|
||||
|
||||
// add data
|
||||
int AddState(std::string name);
|
||||
int EndState();
|
||||
bool AddDefault(std::string state);
|
||||
bool AddEntry(std::string action);
|
||||
bool AddIdle(std::string action);
|
||||
bool AddExit(std::string action);
|
||||
bool AddTransition(char* event,char* state);
|
||||
bool AddAction(char* event,char* action);
|
||||
bool AddTimeTransition(float time,char* state);
|
||||
bool AddTimeAction(float time,char* action);
|
||||
bool AddTerminate(char* event);
|
||||
|
||||
// get data
|
||||
std::string CurStateName();
|
||||
int GetMaxDepth();
|
||||
int GetTimerDepth();
|
||||
|
||||
// finalize machine
|
||||
bool EndMachine();
|
||||
|
||||
// settings
|
||||
void SetQueueSize(int size);
|
||||
int GetQueueSize();
|
||||
|
||||
private:
|
||||
|
||||
int curstate;
|
||||
int maxdepth;
|
||||
int depth;
|
||||
int timerdepth;
|
||||
int queuesize;
|
||||
|
||||
int CountTimers(int sid);
|
||||
|
||||
bool Validate();
|
||||
};
|
||||
|
||||
|
||||
// initialize a machine
|
||||
Machine::Machine()
|
||||
{
|
||||
// set current state to invalid
|
||||
curstate = -1;
|
||||
|
||||
// default depth
|
||||
maxdepth = 0;
|
||||
depth = 0;
|
||||
}
|
||||
|
||||
int Machine::AddState(std::string name)
|
||||
{
|
||||
// make sure this isn't a duplicate state
|
||||
if ( statemap.find(name) != statemap.end() )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// figure out our new state id
|
||||
int id = (int)states.size();
|
||||
|
||||
// add this state to our name -> id map
|
||||
statemap[name] = id;
|
||||
|
||||
// tell parent about this new child state
|
||||
if ( curstate >= 0 )
|
||||
{
|
||||
states[curstate].child.push_back(id);
|
||||
}
|
||||
|
||||
// create new state
|
||||
State s;
|
||||
s.name = name;
|
||||
|
||||
// tell current state about parent
|
||||
s.parent = curstate;
|
||||
|
||||
// add state to list and make current
|
||||
states.push_back(s);
|
||||
|
||||
// record depth
|
||||
depth++;
|
||||
if ( depth > maxdepth )
|
||||
{
|
||||
maxdepth = depth;
|
||||
}
|
||||
|
||||
// make this new state the current state
|
||||
curstate = id;
|
||||
return curstate;
|
||||
}
|
||||
|
||||
int Machine::EndState()
|
||||
{
|
||||
// dec depth
|
||||
depth--;
|
||||
|
||||
// move curstate to parent
|
||||
if ( curstate >= 0 )
|
||||
{
|
||||
curstate = states[curstate].parent;
|
||||
}
|
||||
return curstate;
|
||||
}
|
||||
|
||||
bool Machine::AddDefault(std::string state)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ( states[curstate].defaultstate != "" )
|
||||
{
|
||||
// default state already set for this state
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
states[curstate].defaultstate = state;
|
||||
state_refs.insert(state);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool Machine::AddEntry(std::string action)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// add entry action and reference
|
||||
states[curstate].entry.push_back(action);
|
||||
action_refs.insert(action);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Machine::AddIdle(std::string action)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// add idle action and reference
|
||||
states[curstate].idle.push_back(action);
|
||||
action_refs.insert(action);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Machine::AddExit(std::string action)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// add exit action and reference
|
||||
states[curstate].exit.push_back(action);
|
||||
action_refs.insert(action);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Machine::AddTransition(char* event,char* state)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// make certain a transition doesn't already exist for this event
|
||||
if ( states[curstate].transition.find(event) != states[curstate].transition.end() )
|
||||
{
|
||||
// transition already exists for this event
|
||||
return false;
|
||||
}
|
||||
|
||||
// add transition
|
||||
states[curstate].transition[event] = state;
|
||||
|
||||
// add references
|
||||
event_refs.insert(event);
|
||||
state_refs.insert(state);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Machine::AddAction(char* event,char* action)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// add action
|
||||
states[curstate].action.insert( std::pair<std::string,std::string>(event,action) );
|
||||
|
||||
// add references
|
||||
event_refs.insert(event);
|
||||
action_refs.insert(action);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Machine::AddTimeTransition(float time,char* state)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// add timeout
|
||||
states[curstate].timetransition.insert( std::pair<float,std::string>(time,state) );
|
||||
|
||||
// add references
|
||||
state_refs.insert(state);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Machine::AddTimeAction(float time,char* action)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// add timeout
|
||||
states[curstate].timeaction.insert( std::pair<float,std::string>(time,action) );
|
||||
|
||||
// add references
|
||||
action_refs.insert(action);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Machine::AddTerminate(char* event)
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ( states[curstate].terminate.find(event) != states[curstate].terminate.end() )
|
||||
{
|
||||
// already exists
|
||||
return false;
|
||||
}
|
||||
|
||||
// add terminate
|
||||
states[curstate].terminate.insert(event);
|
||||
|
||||
// add reference
|
||||
event_refs.insert(event);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string Machine::CurStateName()
|
||||
{
|
||||
if ( curstate < 0 )
|
||||
{
|
||||
return "";
|
||||
}
|
||||
else
|
||||
{
|
||||
return states[curstate].name;
|
||||
}
|
||||
}
|
||||
|
||||
int Machine::GetMaxDepth()
|
||||
{
|
||||
return maxdepth;
|
||||
}
|
||||
|
||||
int Machine::GetTimerDepth()
|
||||
{
|
||||
return timerdepth;
|
||||
}
|
||||
|
||||
bool Machine::EndMachine()
|
||||
{
|
||||
// now let's validate it
|
||||
return Validate();
|
||||
}
|
||||
|
||||
int Machine::CountTimers(int sid)
|
||||
{
|
||||
int count = 0;
|
||||
while ( sid >= 0 )
|
||||
{
|
||||
count += (int)states[sid].timers.size();
|
||||
sid = states[sid].parent;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
bool Machine::Validate()
|
||||
{
|
||||
bool ok = true;
|
||||
|
||||
// make sure all referenced states are defined
|
||||
for ( std::set<std::string>::iterator it = state_refs.begin(); it != state_refs.end(); it++ )
|
||||
{
|
||||
if ( statemap.find( *it ) == statemap.end() )
|
||||
{
|
||||
ok = false;
|
||||
printf(
|
||||
"Error: State '%s' referenced but not defined in Machine '%s'\n",
|
||||
(*it).c_str(),name.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// make sure events listed as terminate are not also listed in actions or transitions
|
||||
for ( std::vector<State>::iterator it = states.begin(); it != states.end(); it++ )
|
||||
{
|
||||
// for convenience
|
||||
State& s = (*it);
|
||||
|
||||
// check each "terminate event and make sure it's not in the action or transition list
|
||||
for ( std::set<std::string>::iterator term = s.terminate.begin(); term != s.terminate.end(); term++ )
|
||||
{
|
||||
// look in action & transitions lists
|
||||
if ( s.action.find(*term) != s.action.end() || s.transition.find(*term) != s.transition.end() )
|
||||
{
|
||||
ok = false;
|
||||
printf(
|
||||
"Error: Terminate event '%s' is not unique in State '%s', Machine '%s'\n",
|
||||
(*term).c_str(), s.name.c_str(), name.c_str()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// make sure all of the state names are NOT event names
|
||||
for ( std::vector<State>::iterator it = states.begin(); it != states.end(); it++ )
|
||||
{
|
||||
State& s = (*it);
|
||||
|
||||
// look for this name in the event list
|
||||
if ( event_refs.find(s.name) != event_refs.end() )
|
||||
{
|
||||
ok = false;
|
||||
printf(
|
||||
"Error: State-Event name collision on '%s' in Machine '%s'\n",
|
||||
s.name.c_str(),
|
||||
name.c_str()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// make certain that no timed actions or transitions exist after shortest timed
|
||||
// transition in each state
|
||||
for ( std::vector<State>::iterator it = states.begin(); it != states.end(); it++ )
|
||||
{
|
||||
State& s = (*it);
|
||||
|
||||
// if no timed transitions we're done
|
||||
if ( s.timetransition.size() == 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// find shortest timed transition
|
||||
std::multimap<float,std::string>::iterator ttit = s.timetransition.begin();
|
||||
float shorttime = (*ttit).first;
|
||||
std::string shorttrans = (*ttit).second;
|
||||
|
||||
// now iterate timed transitions and warn about remaining
|
||||
for ( ttit++; ttit != s.timetransition.end(); ttit++ )
|
||||
{
|
||||
printf(
|
||||
"Error: Timed Transition '%s/%f' superceded by '%s/%f' in State '%s', Machine '%s'\n",
|
||||
(*ttit).second.c_str(),
|
||||
(*ttit).first,
|
||||
shorttrans.c_str(),
|
||||
shorttime,
|
||||
s.name.c_str(),
|
||||
name.c_str()
|
||||
);
|
||||
s.timetransition.erase(ttit);
|
||||
ok = false;
|
||||
}
|
||||
|
||||
// iterate through timed actions and warn about anything over
|
||||
std::multimap<float,std::string>::iterator tait;
|
||||
for ( tait = s.timeaction.begin(); tait != s.timeaction.end(); tait++ )
|
||||
{
|
||||
if ( (*tait).first >= shorttime )
|
||||
{
|
||||
printf(
|
||||
"Error: Timed Action '%s/%f' superceded by '%s/%f' in State '%s', Machine '%s'\n",
|
||||
(*tait).second.c_str(),
|
||||
(*tait).first,
|
||||
shorttrans.c_str(),
|
||||
shorttime,
|
||||
s.name.c_str(),
|
||||
name.c_str()
|
||||
);
|
||||
s.timeaction.erase(tait);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now assign timers (value+id) for each state
|
||||
for ( std::vector<State>::iterator it = states.begin(); it != states.end(); it++ )
|
||||
{
|
||||
State& s = (*it);
|
||||
|
||||
// now count up timer values for this state
|
||||
std::set<float> timervalues;
|
||||
for ( std::multimap<float,std::string>::iterator ttit = s.timetransition.begin(); ttit != s.timetransition.end(); ttit++ )
|
||||
{
|
||||
timervalues.insert( (*ttit).first );
|
||||
}
|
||||
for ( std::multimap<float,std::string>::iterator tait = s.timeaction.begin(); tait != s.timeaction.end(); tait++ )
|
||||
{
|
||||
timervalues.insert( (*tait).first );
|
||||
}
|
||||
|
||||
// assign timer id's to values
|
||||
int ptimers = CountTimers(s.parent);
|
||||
for ( std::set<float>::iterator myit = timervalues.begin(); myit != timervalues.end(); myit++ )
|
||||
{
|
||||
s.timers[ptimers++] = *myit;
|
||||
}
|
||||
}
|
||||
|
||||
// determine max timer depth
|
||||
timerdepth = 0;
|
||||
for ( unsigned int i=0;i<states.size();i++ )
|
||||
{
|
||||
int depth = CountTimers(i);
|
||||
if ( depth > timerdepth )
|
||||
{
|
||||
timerdepth = depth;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
void Machine::SetQueueSize(int size)
|
||||
{
|
||||
queuesize = size;
|
||||
}
|
||||
|
||||
int Machine::GetQueueSize()
|
||||
{
|
||||
return queuesize;
|
||||
}
|
||||
|
||||
#endif // __machine_h__
|
||||
|
||||
|
||||
+1387
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,29 @@
|
||||
#ifndef __main_h__
|
||||
#define __main_h__
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif // defined(__cplusplus)
|
||||
|
||||
void parseEndMachine();
|
||||
void parseEndState();
|
||||
void parseBeginMachine(char* name,int eventsize);
|
||||
void parseBeginState(char* name);
|
||||
void parseDefault(char* state);
|
||||
void parseEntry(char* action);
|
||||
void parseIdle(char* action);
|
||||
void parseExit(char* action);
|
||||
void parseTransition(char* event,char* state);
|
||||
void parseAction(char* event,char* action);
|
||||
void parseTimeTransition(float time,char* state);
|
||||
void parseTimeAction(float time,char* action);
|
||||
void parseTerminate(char* event);
|
||||
|
||||
int yyparse();
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif // defined(__cplusplus)
|
||||
|
||||
#endif // __main_h__
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef __state_h__
|
||||
#define __state_h__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
class State
|
||||
{
|
||||
public:
|
||||
|
||||
std::string name;
|
||||
|
||||
int parent;
|
||||
std::vector<int> child;
|
||||
|
||||
std::string defaultstate;
|
||||
std::vector<std::string> entry;
|
||||
std::vector<std::string> idle;
|
||||
std::vector<std::string> exit;
|
||||
std::map<std::string,std::string> transition;
|
||||
std::multimap<std::string,std::string> action;
|
||||
std::set<std::string> terminate;
|
||||
std::multimap<float,std::string> timetransition;
|
||||
std::multimap<float,std::string> timeaction;
|
||||
|
||||
std::map<int,float> timers;
|
||||
};
|
||||
|
||||
|
||||
#endif // __state_h__
|
||||
|
||||
|
||||
Reference in new issue
Block a user