Skip to content

Commit

Permalink
expr parser: switch bison and flex to pure API
Browse files Browse the repository at this point in the history
Now inputs and outputs are passed thrugh yyparse() explicitly.
  • Loading branch information
trofi committed Sep 3, 2022
1 parent c377b79 commit 5af00cd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 60 deletions.
40 changes: 24 additions & 16 deletions src/eparse.y
@@ -1,3 +1,11 @@
%define api.pure full
%param {yyscan_t scanner}
%parse-param {REG * expval}

%code requires {
typedef void * yyscan_t;
}

%{
/*
* Simulator Expression Parser
Expand All @@ -20,32 +28,31 @@
*
*/

#include "eparse.gen.h"
#include "escan.gen.h"

#include "std.h"
#include "types.h"
#include "memui.h"
#include "ui.h"

REG expval;
ADDR thash(ADDR);

extern char *yytext;
extern int yylineno;
extern int yylex (void );
static REG convert(char);
static REG rd8(REG);
static REG strtonum(char *, int);
static void yyerror(char *);
static void yyerror(yyscan_t scanner, REG * expval, char *);

%}

%code requires {
#include "types.h" /* REG used in tokens */
}

%union {
char *str;
REG val;
}
%union {
char *str;
REG val;
}

%left <str> ','
%token <str> '='
Expand Down Expand Up @@ -78,8 +85,8 @@ static void yyerror(char *);
%type <val> based

%%
expr : '!' cntexp { expval = $2; }
| '#' numexp { expval = $2; }
expr : '!' cntexp { *expval = $2; }
| '#' numexp { *expval = $2; }

cntexp : count { $$ = $1; }
| ISYM { $$ = $1; }
Expand All @@ -94,14 +101,14 @@ cntexp : count { $$ = $1; }
| cntexp '/' cntexp { if ($3)
$$ = $1 / $3;
else {
yyerror("division by zero");
yyerror(scanner, expval, "division by zero");
YYABORT;
}
}
| cntexp '%' cntexp { if ($3)
$$ = $1 % $3;
else {
yyerror("division by zero");
yyerror(scanner, expval, "division by zero");
YYABORT;
}
}
Expand Down Expand Up @@ -137,14 +144,14 @@ numexp : number { $$ = $1; }
| numexp '/' numexp { if ($3)
$$ = $1 / $3;
else {
yyerror("division by zero");
yyerror(scanner, expval, "division by zero");
YYABORT;
}
}
| numexp '%' numexp { if ($3)
$$ = $1 % $3;
else {
yyerror("division by zero");
yyerror(scanner, expval, "division by zero");
YYABORT;
}
}
Expand Down Expand Up @@ -211,7 +218,8 @@ static REG strtonum(char *str, int base)
return num;
}

static void yyerror(char *s)
static void yyerror(yyscan_t scanner, REG * expval, char *s)
{
(void)expval;
cmdErr("%s\n", s);
}
56 changes: 22 additions & 34 deletions src/escan.l
@@ -1,3 +1,13 @@
%option warn
%option noyywrap
%option nodefault
%option reentrant
%option bison-bridge

%{
//#include "parse.expr.h"
%}

%{
/*
* Simulator Expression Scanner
Expand Down Expand Up @@ -26,26 +36,6 @@
#include "eparse.gen.h"
#include "ui.h"

char *expptr;

#if defined FLEX_SCANNER

#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) { \
int c = *expptr++; \
result = ( c == 0 ) ? YY_NULL : ( buf[ 0 ] = c, 1 ); \
}
#define YY_NO_UNPUT

#else /* !FLEX_SCANNER */

#undef input
#undef unput
#define input() (*expptr++)
#define unput(c) (*--expptr = c)

#endif /* !FLEX_SCANNER */

static struct isym *symptr;
static ADDR ofs;

Expand All @@ -59,59 +49,59 @@ static REG getVal(struct isym *psym);
}

(0b|0B)[01]+ {
yylval.str = &yytext[2];
yylval->str = &yytext[2];
return BIN;
}

(0o|0O)[0-7]+ {
yylval.str = &yytext[2];
yylval->str = &yytext[2];
return OCT;
}

(0d|0D)[0-9]+ {
yylval.str = &yytext[2];
yylval->str = &yytext[2];
return DEC;
}

(0x|0X)[0-9a-fA-F]+ {
yylval.str = &yytext[2];
yylval->str = &yytext[2];
return HEX;
}

[0-9]+ {
yylval.str = yytext;
yylval->str = yytext;
return NOBASE;
}

[a-fA-F][0-9a-fA-F]* {
if ((symptr = isymVLkp((char *)yytext)) != NULL) {
yylval.val = getVal(symptr);
yylval->val = getVal(symptr);
return ISYM;
} else if (symNametoAddr((char *)yytext, &ofs)) {
yylval.val = ofs;
yylval->val = ofs;
return SYM;
} else {
yylval.str = yytext;
yylval->str = yytext;
return NPFXHEX;
}
}

[a-zA-Z$_?.][a-zA-Z$_0-9?.@]* {
if ((symptr = isymVLkp((char *)yytext)) != NULL) {
yylval.val = getVal(symptr);
yylval->val = getVal(symptr);
return ISYM;
} else if (symNametoAddr((char *)yytext, &ofs)) {
yylval.val = ofs;
yylval->val = ofs;
return SYM;
} else {
yylval.str = yytext;
yylval->str = yytext;
cmdErr("Unrecognized symbol name: %s\n", yytext);
return UKNWN;
}
}

[0-9a-fA-F]+ {
yylval.str = yytext;
yylval->str = yytext;
return NPFXHEX;
}

Expand Down Expand Up @@ -157,8 +147,6 @@ static REG getVal(struct isym *psym);

%%

int yywrap() { return 1; }

/* XXX - copied from ssDCmd.h */
#ifdef NEW_MP
extern unsigned viewPid;
Expand Down
30 changes: 20 additions & 10 deletions src/ssDPrs.c
Expand Up @@ -26,17 +26,18 @@
#include <stdarg.h>
#include <string.h>

#include "ssDPrs.h"

#include "eparse.gen.h"
#include "escan.gen.h"

#include "std.h"
#include "types.h"
#include "ssDCmd.h"
#include "ui.h"

#define ARGLEN 4000

extern char *expptr;
extern REG expval;
int yyparse(void);

/*--------------------------------------------------------------------------
* evaluate$Expression - Evaluate the arithmetic expression pointed at by
* the 'expr' string and return the expression value in 'retval'. Numbers
Expand All @@ -46,6 +47,7 @@ int yyparse(void);
BOOL evalExpr(const char *expr, unsigned base, REG *retval)
{
char expstr[ARGLEN+2];
BOOL result;

if (strlen(expr) >= ARGLEN) {
cmdErr("More than %d characters in expression: %.10s...\n",
Expand All @@ -56,16 +58,24 @@ BOOL evalExpr(const char *expr, unsigned base, REG *retval)
(void)sprintf(expstr, "! %s", expr);
else
(void)sprintf(expstr, "# %s", expr);
expptr = expstr;
if (!yyparse()) {
*retval = expval;
return YES;

yyscan_t scanner;
YY_BUFFER_STATE buf;
yylex_init (&scanner);
buf = yy_scan_string (expstr, scanner);

if (yyparse(scanner, retval) == 0) {
result = YES;
} else {
result = NO;
/* XXX - this will fail for long expressions since the cmdErr string
can only be 100 total chars */
cmdErr("Illegal expression: %s\n", expr);
return NO;
cmdErr("Illegal expression: '%s' ('%s')\n", expr, expstr);
}

yy_delete_buffer(buf, scanner);
yylex_destroy (scanner);
return result;
}

/*--------------------------------------------------------------------------
Expand Down

0 comments on commit 5af00cd

Please sign in to comment.