2005-10-17 20:42:47 +00:00
|
|
|
%{
|
2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2005-10-17 20:42:47 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
This file must be translated to C and modified to build everywhere.
|
|
|
|
|
|
|
|
Run bison like this:
|
|
|
|
|
|
|
|
bison --yacc --name-prefix=cmExpr_yy --defines=cmExprParserTokens.h -ocmExprParser.cxx cmExprParser.y
|
|
|
|
|
|
|
|
Modify cmExprParser.cxx:
|
2016-10-14 19:08:48 +00:00
|
|
|
- "#if 0" out yyerrorlab block in range ["goto yyerrlab1", "yyerrlab1:"]
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
2016-10-14 19:08:48 +00:00
|
|
|
#define YYDEBUG 1
|
2005-10-17 20:42:47 +00:00
|
|
|
#include "cmExprParserHelper.h" /* Interface to parser object. */
|
|
|
|
#include "cmExprLexer.h" /* Interface to lexer object. */
|
|
|
|
#include "cmExprParserTokens.h" /* Need YYSTYPE for YY_DECL. */
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
/* Forward declare the lexer entry point. */
|
|
|
|
YY_DECL;
|
|
|
|
|
2016-10-14 19:08:48 +00:00
|
|
|
/* Helper function to forward error callback from parser. */
|
|
|
|
static void cmExpr_yyerror(yyscan_t yyscanner, const char* message);
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
/* Disable some warnings in the generated code. */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning (disable: 4102) /* Unused goto label. */
|
|
|
|
# pragma warning (disable: 4065) /* Switch statement contains default but no case. */
|
|
|
|
#endif
|
|
|
|
%}
|
|
|
|
|
|
|
|
/* Generate a reentrant parser object. */
|
2016-10-14 19:08:48 +00:00
|
|
|
%define api.pure
|
|
|
|
|
|
|
|
/* Configure the parser to use a lexer object. */
|
|
|
|
%lex-param {yyscan_t yyscanner}
|
|
|
|
%parse-param {yyscan_t yyscanner}
|
|
|
|
|
|
|
|
%define parse.error verbose
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* Tokens */
|
|
|
|
%token exp_PLUS
|
|
|
|
%token exp_MINUS
|
|
|
|
%token exp_TIMES
|
|
|
|
%token exp_DIVIDE
|
|
|
|
%token exp_MOD
|
|
|
|
%token exp_SHIFTLEFT
|
|
|
|
%token exp_SHIFTRIGHT
|
|
|
|
%token exp_OPENPARENT
|
|
|
|
%token exp_CLOSEPARENT
|
|
|
|
%token exp_OR;
|
|
|
|
%token exp_AND;
|
|
|
|
%token exp_XOR;
|
|
|
|
%token exp_NOT;
|
|
|
|
%token exp_NUMBER;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* grammar */
|
|
|
|
%%
|
|
|
|
|
|
|
|
|
2016-10-14 19:25:14 +00:00
|
|
|
start:
|
|
|
|
exp {
|
|
|
|
cmExpr_yyget_extra(yyscanner)->SetResult($<Number>1);
|
|
|
|
}
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
exp:
|
2016-10-14 19:25:14 +00:00
|
|
|
bitwiseor {
|
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
|
|
|
| exp exp_OR bitwiseor {
|
|
|
|
$<Number>$ = $<Number>1 | $<Number>3;
|
|
|
|
}
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
bitwiseor:
|
2016-10-14 19:25:14 +00:00
|
|
|
bitwisexor {
|
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
|
|
|
| bitwiseor exp_XOR bitwisexor {
|
|
|
|
$<Number>$ = $<Number>1 ^ $<Number>3;
|
|
|
|
}
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
bitwisexor:
|
2016-10-14 19:25:14 +00:00
|
|
|
bitwiseand {
|
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
|
|
|
| bitwisexor exp_AND bitwiseand {
|
|
|
|
$<Number>$ = $<Number>1 & $<Number>3;
|
|
|
|
}
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
bitwiseand:
|
2016-10-14 19:25:14 +00:00
|
|
|
shift {
|
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
|
|
|
| bitwiseand exp_SHIFTLEFT shift {
|
|
|
|
$<Number>$ = $<Number>1 << $<Number>3;
|
|
|
|
}
|
|
|
|
| bitwiseand exp_SHIFTRIGHT shift {
|
|
|
|
$<Number>$ = $<Number>1 >> $<Number>3;
|
|
|
|
}
|
2005-10-17 20:53:11 +00:00
|
|
|
|
|
|
|
shift:
|
2016-10-14 19:25:14 +00:00
|
|
|
term {
|
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
|
|
|
| shift exp_PLUS term {
|
|
|
|
$<Number>$ = $<Number>1 + $<Number>3;
|
|
|
|
}
|
|
|
|
| shift exp_MINUS term {
|
|
|
|
$<Number>$ = $<Number>1 - $<Number>3;
|
|
|
|
}
|
2005-10-17 20:42:47 +00:00
|
|
|
|
|
|
|
term:
|
2016-10-14 19:28:51 +00:00
|
|
|
unary {
|
2016-10-14 19:25:14 +00:00
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
2016-10-14 19:28:51 +00:00
|
|
|
| term exp_TIMES unary {
|
2016-10-14 19:25:14 +00:00
|
|
|
$<Number>$ = $<Number>1 * $<Number>3;
|
|
|
|
}
|
2016-10-14 19:28:51 +00:00
|
|
|
| term exp_DIVIDE unary {
|
2016-10-14 19:25:14 +00:00
|
|
|
$<Number>$ = $<Number>1 / $<Number>3;
|
|
|
|
}
|
2016-10-14 19:28:51 +00:00
|
|
|
| term exp_MOD unary {
|
2016-10-14 19:25:14 +00:00
|
|
|
$<Number>$ = $<Number>1 % $<Number>3;
|
|
|
|
}
|
2005-10-17 20:42:47 +00:00
|
|
|
|
2016-10-14 19:28:51 +00:00
|
|
|
unary:
|
|
|
|
factor {
|
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
|
|
|
| exp_PLUS unary {
|
|
|
|
$<Number>$ = + $<Number>2;
|
|
|
|
}
|
|
|
|
| exp_MINUS unary {
|
|
|
|
$<Number>$ = - $<Number>2;
|
|
|
|
}
|
|
|
|
|
2005-10-17 20:42:47 +00:00
|
|
|
factor:
|
2016-10-14 19:25:14 +00:00
|
|
|
exp_NUMBER {
|
|
|
|
$<Number>$ = $<Number>1;
|
|
|
|
}
|
|
|
|
| exp_OPENPARENT exp exp_CLOSEPARENT {
|
|
|
|
$<Number>$ = $<Number>2;
|
|
|
|
}
|
2005-10-17 20:42:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|
|
|
|
/* End of grammar */
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2016-10-14 19:08:48 +00:00
|
|
|
void cmExpr_yyerror(yyscan_t yyscanner, const char* message)
|
2005-10-17 20:42:47 +00:00
|
|
|
{
|
2016-10-14 19:08:48 +00:00
|
|
|
cmExpr_yyget_extra(yyscanner)->Error(message);
|
2005-10-17 20:42:47 +00:00
|
|
|
}
|
|
|
|
|