?? shell.yacc
字號:
%{/* shell.yacc - grammar for VxWorks shell *//* Copyright 1984-2001 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------07m,16oct01,jn use symFindSymbol for symbol lookups07l,27nov01,pch Provide floating-point exclusion based on _WRS_NO_TGT_SHELL_FP definition instead of testing a specific CPU type.07k,23oct01,fmk Do not call symFindByValue and print symbol name if symbol value = -1 SPR 2225407j,04sep98,cdp apply 07i for all ARM CPUs with ARM_THUMB==TRUE.07i,30jul97,cdp for ARM7TDMI_T, force calls to be in Thumb state.07h,31may96,ms added in patch for SPR 4439.07g,19mar95,dvs removed tron references.07f,02mar95,yao removed floating point temporarily for PPC403.07e,19mar95,dvs removed tron references.07d,13feb93,kdl changed cplusLib.h to private/cplusLibP.h (SPR #1917).07c,03sep92,wmd modified addArg() to pass floats correcty for the i960.07b,03sep92,rrr reduced max function arguments from 16 to 12 (for i960).07a,31aug92,kdl fixed passing of more than 10 parameters during funcCall(); changed MAX_ARGS to MAX_SHELL_ARGS.06z,19aug92,jmm fixed problem with not recognizing <= (SPR 1517)06y,01aug92,srh added C++ demangling idiom to printSym. added include of cplusLib.h.06x,20jul92,jmm added group parameter to symAdd call06w,23jun92,kdl increased max function arguments from 10 to 16.06v,22jun92,jmm backed out 6u change, now identical to gae's 21dec revision06u,22jun92,jmm added group parameter to symAdd06t,21dec91,gae more ANSI cleanups.06s,19nov91,rrr shut up some warnings.06r,05oct91,rrr changed strLib.h to string.h06q,02jun91,del added I960 parameter alignment fixes.06p,10aug90,kdl added forward declarations for functions returning VOID.06o,10jul90,dnw spr 738: removed checking of access (checkAccess, chkLvAccess) Access checking did vxMemProbe with 4 byte read, which caused problems with memory or devices that couldn't do 4 byte reads but could do other types of access. Access checking was actually a throw-back to a time when the shell couldn't recover from bus errors, but it can now. So I just deleted the access checking. lint clean-up, esp to allow VOID to be void one day.06n,10dec89,jcf symbol table type now a SYM_TYPE.06m,09aug89,gae fixed copyright notice.06l,30jul89,gae changed obsolete sysMemProbe to vxMemProbe. 06k,07jul88,jcf changed malloc to match new declaration.06j,30may88,dnw changed to v4 names.06i,01apr88,gae made it work with I/O system changes -- io{G,S}etGlobalStd().06h,20feb88,dnw lint06g,14dec87,dnw removed checking for odd byte address access. changed printing format of doubles from "%f" to "%g". 06f,18nov87,gae made assignment to be of type specified by rhs.06e,07nov87,gae fixed undefined symbol bug. fixed history bug by redirecting LED I/O.06d,03nov87,ecs documentation.06c,28oct87,gae got rid of string type.06b,06oct87,gae split off "execution" portion to shellExec.c. changed to use conventional C type casting. provided more info for invalid yacc operations. allowed expressions to be function addresses.06a,01jun87,gae added interpretation of bytes, words, floats, doubles; expressions can now be "typed" a la assembler, .[bwfdls]. fixed redirection bug with ">>" and "<<".05i,16jul87,ecs fixed newSym so that new symbols will be global.05h,01apr87,gae made assign() not print "new value" message (duplicated normal "value" message for expressions.05g,25apr87,gae fixed bug in assign() that allowed memory corruption. checked h() parameter for greater than or equal to zero. improved redirection detection. now parse assignments correctly as expressions.05f,01apr87,ecs added include of strLib.h.05e,20jan87,jlf documentation.05d,14jan87,gae got rid of unused curLineNum. h() now has parameter, if non-zero then resets history to that size.05c,20dec86,dnw changed to not get include files from default directories.05b,18dec86,gae made history initialization only happen on first start of shell. Added neStmt to fix empty stmt assignment bug.05a,17dec86,gae use new shCmd() in execShell() to do Korn shell-like input.04q,08dec86,dnw changed shell.slex.c to shell_slex.c for VAX/VMS compatiblity. jlf fixed a couple bugs causing problems mainly on Heurikon port.04p,24nov86,llk deleted SYSTEM conditional compiles.04o,08oct86,gae added C assignment operators; allowed multiple assignment. STRINGs are no longer temporary. Added setShellPrompt().04n,27jul86,llk added standard error fd, setOrigErrFd.04m,17jun86,rdc changed memAllocates to mallocs.04l,08apr86,dnw added call to vxSetTaskBreakable to make shell unbreakable. changed sstLib calls to symLib.04k,02apr86,rdc added routines setOrigInFd and setOrigOutFd.04j,18jan86,dnw removed resetting (flushing) for standard in/out upon restarts; this is now done more appropriately by the shell restart routine in dbgLib....deleted pre 86 history - see RCS*//*DESCRIPTIONThis is the parser for the VxWorks shell, written in yacc.It provides the basic programmer's interface to VxWorks.It is a C expression interpreter, containing no built-in commands. SEE ALSO: "Shell"*/#include "vxWorks.h"#include "sysSymTbl.h"#include "errno.h"#include "errnoLib.h"#include "ioLib.h"#include "taskLib.h"#include "stdio.h"#include "private/cplusLibP.h"#define YYSTYPE VALUE /* type of parse stack */#define MAX_SHELL_LINE 128 /* max chars on line typed to shell */#define MAX_SHELL_ARGS 30 /* max number of args on stack */#define MAX_FUNC_ARGS 12 /* max number of args to any one function */ /* NOTE: The array indices in funcCall() * must agree with MAX_FUNC_ARGS!! */#define BIN_OP(op) rvOp((getRv(&yypvt[-2], &tmpVal1)), op, \ getRv(&yypvt[-0], &tmpVal2))#define RV(value) (getRv (&(value), &tmpVal2))#define NULLVAL (VALUE *) NULL#define CHECK if (semError) YYERROR#define SET_ERROR semError = TRUEtypedef enum /* TYPE */ { T_UNKNOWN, T_BYTE, T_WORD,#ifndef _WRS_NO_TGT_SHELL_FP T_INT, T_FLOAT, T_DOUBLE#else /* _WRS_NO_TGT_SHELL_FP */ T_INT#endif /* _WRS_NO_TGT_SHELL_FP */ } TYPE;typedef enum /* SIDE */ { LHS, RHS, FHS /* function: rhs -> lhs */ } SIDE;typedef struct /* VALUE */ { SIDE side; TYPE type; union { int *lv; /* pointer to any of the below */ char byte; short word; int rv; char *string;#ifndef _WRS_NO_TGT_SHELL_FP float fp; double dp;#endif /* _WRS_NO_TGT_SHELL_FP */ } value; } VALUE;IMPORT int redirInFd;IMPORT int redirOutFd;LOCAL BOOL semError; /* TRUE = semantic error found */LOCAL VALUE tmpVal1; /* used by BIN_OP above for expression evaluation */LOCAL VALUE tmpVal2; /* used by BIN_OP above for expression evaluation */LOCAL int argStack [MAX_SHELL_ARGS]; /* arguments to functions */LOCAL int nArgs; /* number of args currently on argStack */LOCAL BOOL usymFlag; /* TRUE = U_SYMBOL has been seen */LOCAL VALUE usymVal; /* value of U_SYMBOL which has been seen */LOCAL BOOL spawnFlag; /* TRUE if spawn is first parameter in argStack[] */%}%start line%token NL 0%token T_SYMBOL D_SYMBOL U_SYMBOL NUMBER CHAR STRING FLOAT%token OR AND EQ NE GE LE INCR DECR ROT_LEFT ROT_RIGHT UMINUS PTR TYPECAST%token ENDFILE LEX_ERROR%left '=' MULA DIVA MODA ADDA SUBA SHLA SHRA ANDA ORA XORA%right '?' ':'%left OR%left AND%left '|'%left '^'%left '&'%left EQ NE%left GE LE '>' '<'%left ROT_LEFT ROT_RIGHT%left '+' '-'%left '*' '/' '%'%left INCR DECR%left UNARY /* supplies precedence for unary operators */%left PTR '[' '('%left TYPECAST%%line : stmt | stmt ';' line ;stmt : /* empty */ | expr { printValue (&$1); CHECK; } ;expr : D_SYMBOL | T_SYMBOL { $1.side = RHS; setRv (&$$, &$1); } | STRING { $$ = $1; $$.value.rv = newString((char*)$1.value.rv); CHECK; } | CHAR | NUMBER | FLOAT | '(' expr ')' { $$ = $2; } | expr '(' arglist ')' { $$ = funcCall (&$1, &$3); CHECK; } | typecast expr %prec TYPECAST { typeConvert (&$2, $1.type, $1.side); $$ = $2; CHECK; } | '*' expr %prec UNARY { VALUE tmp; (void)getRv (&$2, &tmp); setLv (&$$, &tmp); CHECK; } | '&' expr %prec UNARY { $$.value.rv = (int)getLv (&$2); $$.type = T_INT; $$.side = RHS; } | '-' expr %prec UNARY { rvOp (RV($2), UMINUS, NULLVAL); } | '!' expr %prec UNARY { rvOp (RV($2), '!', NULLVAL); } | '~' expr %prec UNARY { rvOp (RV($2), '~', NULLVAL); } | expr '?' expr ':' expr { setRv (&$$, RV($1)->value.rv ? &$3 : &$5); } | expr '[' expr ']' { BIN_OP ('+'); typeConvert (&$$, T_INT, RHS); setLv (&$$, &$$); } | expr PTR expr { BIN_OP ('+'); typeConvert (&$$, T_INT, RHS); setLv (&$$, &$$); } | expr '+' expr { BIN_OP ('+'); } | expr '-' expr { BIN_OP ('-'); } | expr '*' expr { BIN_OP ('*'); } | expr '/' expr { BIN_OP ('/'); } | expr '%' expr { BIN_OP ('%'); } | expr ROT_RIGHT expr { BIN_OP (ROT_RIGHT); } | expr ROT_LEFT expr { BIN_OP (ROT_LEFT); } | expr '&' expr { BIN_OP ('&'); } | expr '^' expr { BIN_OP ('^'); } | expr '|' expr { BIN_OP ('|'); } | expr AND expr { BIN_OP (AND); } | expr OR expr { BIN_OP (OR); } | expr EQ expr { BIN_OP (EQ); } | expr NE expr { BIN_OP (NE); } | expr GE expr { BIN_OP (GE); } | expr LE expr { BIN_OP (LE); } | expr '>' expr { BIN_OP ('>'); } | expr '<' expr { BIN_OP ('<'); } | INCR expr %prec UNARY { rvOp (RV($2), INCR, NULLVAL); assign (&$2, &$$); CHECK; } | DECR expr %prec UNARY { rvOp (RV($2), DECR, NULLVAL); assign (&$2, &$$); CHECK; } | expr INCR %prec UNARY { VALUE tmp; tmp = $1; rvOp (RV($1), INCR, NULLVAL); assign (&$1, &$$); CHECK; $$ = tmp; } | expr DECR %prec UNARY { VALUE tmp; tmp = $1; rvOp (RV($1), DECR, NULLVAL); assign (&$1, &$$); CHECK; $$ = tmp; } | expr ADDA expr { BIN_OP (ADDA); assign (&$1, &$$); CHECK;} | expr SUBA expr { BIN_OP (SUBA); assign (&$1, &$$); CHECK;} | expr ANDA expr { BIN_OP (ANDA); assign (&$1, &$$); CHECK;} | expr ORA expr { BIN_OP (ORA); assign (&$1, &$$); CHECK;} | expr MODA expr { BIN_OP (MODA); assign (&$1, &$$); CHECK;} | expr XORA expr { BIN_OP (XORA); assign (&$1, &$$); CHECK;} | expr MULA expr { BIN_OP (MULA); assign (&$1, &$$); CHECK;} | expr DIVA expr { BIN_OP (DIVA); assign (&$1, &$$); CHECK;} | expr SHLA expr { BIN_OP (SHLA); assign (&$1, &$$); CHECK;} | expr SHRA expr { BIN_OP (SHRA); assign (&$1, &$$); CHECK;} | expr '=' expr { assign (&$1, &$3); $$ = $1; } | U_SYMBOL /* the following is to allow "undef sym" error msg, * instead of "syntax err" when U_SYM is not followed * by proper assignment (see yyerror() below) */ { usymFlag = TRUE; usymVal = $1; } '=' expr { if ($1.type != T_UNKNOWN) { printf ("typecast of lhs not allowed.\n"); YYERROR; } else { $$ = newSym ((char *)$1.value.rv, $4.type); CHECK; assign (&$$, &$4); CHECK; } usymFlag = FALSE; } ;arglist : /* empty */ { $$ = newArgList (); } | neArglist ;neArglist: expr /* non-empty arglist */ { $$ = newArgList (); addArg (&$$, &$1); CHECK; } | neArglist ',' expr /* ',' is required */ { addArg (&$1, &$3); CHECK; } ;typecast: '(' TYPECAST ')' { $2.side = RHS; $$ = $2; } | '(' TYPECAST '(' ')' ')' { $2.side = FHS; $$ = $2; } ;%%#include "a_out.h"#include "ctype.h"#include "stdlib.h"#include "string.h"#include "symLib.h"#include "shell_slex_c"/* forward declarations */LOCAL int newString ();LOCAL VALUE *getRv ();LOCAL int *getLv ();LOCAL VALUE evalExp ();#ifndef _WRS_NO_TGT_SHELL_FPLOCAL void doubleToInts ();#endif /* _WRS_NO_TGT_SHELL_FP */LOCAL void setRv ();LOCAL void typeConvert ();LOCAL BOOL checkLv ();LOCAL BOOL checkRv ();/********************************************************************************* yystart - initialize local variables** NOMANUAL*/void yystart (line) char *line; { lexNewLine (line); semError = FALSE; usymFlag = FALSE; nArgs = 0; spawnFlag = FALSE; }/********************************************************************************* yyerror - report error** This routine is called by yacc when an error is detected.*/LOCAL void yyerror (string) char *string; { if (semError) /* semantic errors have already been reported */ return; /* print error depending on what look-ahead token is */ switch (yychar) { case U_SYMBOL: /* U_SYM not at beginning of line */ printf ("undefined symbol: %s\n", (char *) yylval.value.rv); break; case LEX_ERROR: /* lex should have already reported the problem */ break; default: if (usymFlag) /* leading U_SYM was followed by invalid assign */ printf ("undefined symbol: %s\n", (char *)usymVal.value.rv); else printf ("%s\n", string); break; } }/********************************************************************************* rvOp - sets rhs of yyval to evaluated expression*/LOCAL void rvOp (pY1, op, pY2) VALUE *pY1; int op; VALUE *pY2; { VALUE yy; yy = evalExp (pY1, op, pY2); setRv (&yyval, &yy); }/********************************************************************************* assign - make assignment of new value to a cell*/LOCAL void assign (pLv, pRv) FAST VALUE *pLv; /* lhs to be assigned into */ FAST VALUE *pRv; /* rhs value */ { VALUE val; /* verify that lv can be assigned to, then make the assignment */ if (checkLv (pLv) && checkRv (pRv)) { (void)getRv (pRv, &val); /* make value agree in type */ pLv->type = pRv->type; typeConvert (&val, pLv->type, RHS); switch (pLv->type) { case T_BYTE: * (char *)getLv (pLv) = val.value.byte; break; case T_WORD: * (short *)getLv (pLv) = val.value.word; break; case T_INT: *getLv (pLv) = val.value.rv; break;#ifndef _WRS_NO_TGT_SHELL_FP case T_FLOAT: * (float *)getLv (pLv) = val.value.fp; break; case T_DOUBLE: * (double *)getLv (pLv) = val.value.dp; break;#endif /* _WRS_NO_TGT_SHELL_FP */ default: printf ("bad assignment.\n"); SET_ERROR; } } else { printf ("bad assignment.\n"); SET_ERROR; } }/********************************************************************************* newString - allocate and copy a string*/LOCAL int newString (string) char *string; { int length = strlen (string) + 1; char *address = (char *) malloc ((unsigned) length); if (address == NULL) { printf ("not enough memory for new string.\n"); SET_ERROR; } else bcopy (string, address, length); return ((int)address); }/********************************************************************************* newSym - allocate a new symbol and add to symbol table*/LOCAL VALUE newSym (name, type) char *name; TYPE type; { VALUE value; void *address = (void *) malloc (sizeof (double)); if (address == NULL) { printf ("not enough memory for new variable.\n"); SET_ERROR; } else if (symAdd (sysSymTbl, name, (char *) address, (N_BSS | N_EXT), symGroupDefault) != OK) { free ((char *) address); printf ("can't add '%s' to system symbol table - error = 0x%x.\n", name, errnoGet()); SET_ERROR; } else { printf ("new symbol \"%s\" added to symbol table.\n", name); value.side = LHS; value.type = type; value.value.lv = (int *) address; } return (value); }/********************************************************************************* printSym - print symbolic value*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -