aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-05 04:47:56 +1000
committerDavid Walter Seikel2012-01-05 04:47:56 +1000
commitd4b977d0904a1dea37922dac60d3bb37f9769230 (patch)
treee5f23ddd6a6dd880f770c897fa9c11e67c4c79cf
parentAdd a compiler stub. (diff)
downloadSledjHamr-d4b977d0904a1dea37922dac60d3bb37f9769230.zip
SledjHamr-d4b977d0904a1dea37922dac60d3bb37f9769230.tar.gz
SledjHamr-d4b977d0904a1dea37922dac60d3bb37f9769230.tar.bz2
SledjHamr-d4b977d0904a1dea37922dac60d3bb37f9769230.tar.xz
Add a simple flex + btyacc stub.
Will be fleshed out soon with LSL grammer.
-rw-r--r--.gitignore5
-rwxr-xr-xLuaSL/build.sh27
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.c63
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h55
-rw-r--r--LuaSL/src/LuaSL_lexer.l31
-rw-r--r--LuaSL/src/LuaSL_parser.c86
-rw-r--r--LuaSL/src/LuaSL_parser_param.h38
-rw-r--r--LuaSL/src/LuaSL_type_parser.h29
-rw-r--r--LuaSL/src/LuaSL_yaccer.y36
-rw-r--r--LuaSL/src/btyacc-c.ske939
10 files changed, 1308 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index f9743a0..8555fd6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,8 @@
1*.edj 1*.edj
2*.o 2*.o
3LuaSL/LuaSL 3LuaSL/LuaSL
4LuaSL/LuaSL_parser
5LuaSL/src/*.tab.*
6LuaSL/src/LuaSL_lexer.c
7LuaSL/src/LuaSL_lexer.h
8
diff --git a/LuaSL/build.sh b/LuaSL/build.sh
index 6aba3a0..9f2420b 100755
--- a/LuaSL/build.sh
+++ b/LuaSL/build.sh
@@ -51,7 +51,7 @@ names="LuaSL_main LuaSL_compile LuaSL_utilities"
51 51
52EDJE_FLAGS="-id images -fd fonts" 52EDJE_FLAGS="-id images -fd fonts"
53 53
54rm -f LuaSL *.o *.edj 54rm -f ../LuaSL ../LuaSL_parser *.o *.edj LuaSL_lexer.h LuaSL_lexer.c LuaSL_yaccer.h LuaSL_yaccer.tab.c
55command="edje_cc $EDJE_FLAGS LuaSL.edc ../LuaSL.edj" 55command="edje_cc $EDJE_FLAGS LuaSL.edc ../LuaSL.edj"
56echo $command 56echo $command
57$command 57$command
@@ -69,3 +69,28 @@ command="gcc $CFLAGS -o ../LuaSL $objects $LDFLAGS $libs"
69echo $command 69echo $command
70$command 70$command
71 71
72
73
74names="LuaSL_parser LuaSL_LSL_tree LuaSL_lexer LuaSL_yaccer.tab"
75
76command="flex --outfile=LuaSL_lexer.c --header-file=LuaSL_lexer.h LuaSL_lexer.l"
77echo $command
78$command
79
80command="btyacc -d -b LuaSL_yaccer -S btyacc-c.ske LuaSL_yaccer.y"
81echo $command
82$command
83
84objects=""
85for i in $names
86do
87 command="gcc $CFLAGS -c -o ../$i.o $i.c"
88 echo $command
89 $command
90 objects="$objects ../$i.o"
91done
92
93command="gcc $CFLAGS -o ../LuaSL_parser $objects $LDFLAGS $libs"
94echo $command
95$command
96
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c
new file mode 100644
index 0000000..78da8e7
--- /dev/null
+++ b/LuaSL/src/LuaSL_LSL_tree.c
@@ -0,0 +1,63 @@
1/*
2 * Implementation of functions used to build the abstract syntax tree.
3 */
4
5#include "LuaSL_LSL_tree.h"
6#include <stdlib.h>
7
8/**
9 * @brief Allocates space for expression
10 * @return The expression or NULL if not enough memory
11 */
12static SExpression* allocateExpression()
13{
14 SExpression* b = malloc(sizeof *b);
15
16 if( b == NULL ) return NULL;
17
18 b->type = eVALUE;
19 b->value = 0;
20
21 b->left = NULL;
22 b->right = NULL;
23
24 return b;
25}
26
27SExpression* createNumber(int value)
28{
29 SExpression* b = allocateExpression();
30
31 if( b == NULL ) return NULL;
32
33 b->type = eVALUE;
34 b->value = value;
35
36 return b;
37}
38
39SExpression *createOperation(
40 EOperationType type,
41 SExpression *left,
42 SExpression *right)
43{
44 SExpression* b = allocateExpression();
45
46 if( b == NULL ) return NULL;
47
48 b->type = type;
49 b->left = left;
50 b->right = right;
51
52 return b;
53}
54
55void deleteExpression(SExpression *b)
56{
57 if (b == NULL) return;
58
59 deleteExpression(b->left);
60 deleteExpression(b->right);
61
62 free(b);
63}
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
new file mode 100644
index 0000000..6d89672
--- /dev/null
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -0,0 +1,55 @@
1/*
2 * Definition of the structure used to build the abstract syntax tree.
3 */
4#ifndef __EXPRESSION_H__
5#define __EXPRESSION_H__
6
7/**
8 * @brief The operation type
9 */
10typedef enum tagEOperationType
11{
12 eVALUE,
13 eMULTIPLY,
14 ePLUS
15}EOperationType;
16
17/**
18 * @brief The expression structure
19 */
20typedef struct tagSExpression
21{
22 EOperationType type;///< type of operation
23
24 int value;///< valid only when type is eVALUE
25 struct tagSExpression* left; ///< left side of the tree
26 struct tagSExpression* right;///< right side of the tree
27}SExpression;
28
29/**
30 * @brief It creates an identifier
31 * @param value The number value
32 * @return The expression or NULL in case of no memory
33 */
34SExpression* createNumber(int value);
35
36/**
37 * @brief It creates an operation
38 * @param type The operation type
39 * @param left The left operand
40 * @param right The right operand
41 * @return The expression or NULL in case of no memory
42 */
43SExpression* createOperation(
44 EOperationType type,
45 SExpression *left,
46 SExpression *right);
47
48/**
49 * @brief Deletes a expression
50 * @param b The expression
51 */
52void deleteExpression(SExpression *b);
53
54#endif // __EXPRESSION_H__
55
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l
new file mode 100644
index 0000000..f55d458
--- /dev/null
+++ b/LuaSL/src/LuaSL_lexer.l
@@ -0,0 +1,31 @@
1%{
2
3#include "LuaSL_type_parser.h"
4#include "LuaSL_yaccer.tab.h"
5
6%}
7
8%option reentrant noyywrap never-interactive nounistd
9%option bison-bridge
10
11LPAREN "("
12RPAREN ")"
13PLUS "+"
14MULTIPLY "*"
15
16NUMBER [0-9]+
17WS [ \r\n\t]*
18
19%%
20
21{WS} { /* Skip blanks. */ }
22{NUMBER} { sscanf(yytext,"%d",&yylval->value); return TOKEN_NUMBER; }
23
24{MULTIPLY} { return TOKEN_MULTIPLY; }
25{PLUS} { return TOKEN_PLUS; }
26{LPAREN} { return TOKEN_LPAREN; }
27{RPAREN} { return TOKEN_RPAREN; }
28. { }
29
30%%
31
diff --git a/LuaSL/src/LuaSL_parser.c b/LuaSL/src/LuaSL_parser.c
new file mode 100644
index 0000000..9e1d3ca
--- /dev/null
+++ b/LuaSL/src/LuaSL_parser.c
@@ -0,0 +1,86 @@
1#include "LuaSL_parser_param.h"
2#include "LuaSL_yaccer.tab.h"
3
4
5int yyparse(void *param);
6
7static int initParserParam(SParserParam* param)
8{
9 int ret = 0;
10
11 ret = yylex_init(&param->scanner);
12 param->expression = NULL;
13
14 return ret;
15}
16
17static int destroyParserParam(SParserParam* param)
18{
19 return yylex_destroy(param->scanner);
20}
21
22SExpression *getAST(const char *expr)
23{
24 SParserParam p;
25 YY_BUFFER_STATE state;
26
27 if ( initParserParam(&p) )
28 {
29 // couldn't initialize
30 return NULL;
31 }
32
33 state = yy_scan_string(expr, p.scanner);
34
35 if ( yyparse(&p) )
36 {
37 // error parsing
38 return NULL;
39 }
40
41 yy_delete_buffer(state, p.scanner);
42
43 destroyParserParam(&p);
44
45 return p.expression;
46}
47
48int evaluate(SExpression *e)
49{
50 switch(e->type)
51 {
52 case eVALUE:
53 return e->value;
54 case eMULTIPLY:
55 return evaluate(e->left) * evaluate(e->right);
56 case ePLUS:
57 return evaluate(e->left) + evaluate(e->right);
58 default:
59 // shouldn't be here
60 return 0;
61 }
62}
63
64int yyerror(const char *msg)
65{
66 fprintf(stderr,"Error:%s\n",msg); return 0;
67}
68
69int main(void)
70{
71 SExpression *e = NULL;
72 char test[]=" 4 + 2*10 + 3*( 5 + 1 )";
73 int result = 0;
74
75 e = getAST(test);
76
77 result = evaluate(e);
78
79 printf("Result of '%s' is %d\n", test, result);
80
81 deleteExpression(e);
82
83 return 0;
84}
85
86
diff --git a/LuaSL/src/LuaSL_parser_param.h b/LuaSL/src/LuaSL_parser_param.h
new file mode 100644
index 0000000..944a671
--- /dev/null
+++ b/LuaSL/src/LuaSL_parser_param.h
@@ -0,0 +1,38 @@
1/*
2 * ParserParam.h
3 * Definitions of the parameters for the reentrant functions
4 * of flex (yylex) and bison (yyparse)
5 */
6
7// Since in this sample we use the reentrant version of both flex and yacc we are forced to provide parameters for the yylex function, when called from yyparse.
8
9#ifndef __PARSERPARAM_H__
10#define __PARSERPARAM_H__
11
12#ifndef YY_NO_UNISTD_H
13#define YY_NO_UNISTD_H 1
14#endif // YY_NO_UNISTD_H
15
16#include "LuaSL_type_parser.h"
17#include "LuaSL_lexer.h"
18#include "LuaSL_LSL_tree.h"
19
20/**
21 * @brief structure given as argument to the reentrant 'yyparse' function.
22 */
23typedef struct tagSParserParam
24{
25 yyscan_t scanner;
26 SExpression *expression;
27}SParserParam;
28
29// the parameter name (of the reentrant 'yyparse' function)
30// data is a pointer to a 'SParserParam' structure
31#define YYPARSE_PARAM data
32
33// the argument for the 'yylex' function
34#define YYLEX_PARAM ((SParserParam*)data)->scanner
35
36#endif // __PARSERPARAM_H__
37
38
diff --git a/LuaSL/src/LuaSL_type_parser.h b/LuaSL/src/LuaSL_type_parser.h
new file mode 100644
index 0000000..66c3f5d
--- /dev/null
+++ b/LuaSL/src/LuaSL_type_parser.h
@@ -0,0 +1,29 @@
1/*
2 * TypeParser.h
3 * Definition of the structure used internally by the parser and lexer
4 * to exchange data.
5 */
6
7#ifndef __TYPE_PARSER_H__
8#define __TYPE_PARSER_H__
9
10#include "LuaSL_LSL_tree.h"
11
12/**
13 * @brief The structure used by flex and bison
14 */
15typedef union tagTypeParser
16{
17 SExpression *expression;
18 int value;
19}STypeParser;
20
21// define the type for flex and bison
22#define YYSTYPE STypeParser
23
24int yyerror(const char *msg);
25
26
27#endif // __TYPE_PARSER_H__
28
29
diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y
new file mode 100644
index 0000000..07f1fd7
--- /dev/null
+++ b/LuaSL/src/LuaSL_yaccer.y
@@ -0,0 +1,36 @@
1%{
2
3#include "LuaSL_parser_param.h"
4#include "LuaSL_yaccer.tab.h"
5
6%}
7
8%define api.pure
9
10%left '+' TOKEN_PLUS
11%left '*' TOKEN_MULTIPLY
12
13%token TOKEN_LPAREN
14%token TOKEN_RPAREN
15%token TOKEN_PLUS
16%token TOKEN_MULTIPLY
17
18%token <value> TOKEN_NUMBER
19
20%type <expression> expr
21
22%%
23
24input:
25 expr { ((SParserParam*)data)->expression = $1; }
26 ;
27
28expr:
29 expr TOKEN_PLUS expr { $$ = createOperation( ePLUS, $1, $3 ); }
30 | expr TOKEN_MULTIPLY expr { $$ = createOperation( eMULTIPLY, $1, $3 ); }
31 | TOKEN_LPAREN expr TOKEN_RPAREN { $$ = $2; }
32 | TOKEN_NUMBER { $$ = createNumber($1); }
33;
34
35%%
36
diff --git a/LuaSL/src/btyacc-c.ske b/LuaSL/src/btyacc-c.ske
new file mode 100644
index 0000000..ac0fcd5
--- /dev/null
+++ b/LuaSL/src/btyacc-c.ske
@@ -0,0 +1,939 @@
1/* -*- C -*-
2
3 The banner used here should be replaced with an #ident directive if
4 the target C compiler supports #ident directives.
5
6 If the skeleton is changed, the banner should be changed so that
7 the altered version can easily be distinguished from the original. */
8
9%% banner
10/* -*- C -*-
11
12 @(#)btyaccpar, based on byacc 1.8 (Berkeley), modified for rentrant flex.
13
14*/
15#define YYBTYACC 1
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21typedef int Yshort;
22
23%% tables
24
25#ifndef _YACC_EXTERN_
26# define _YACC_EXTERN_ "C"
27#endif
28
29extern _YACC_EXTERN_ Yshort yylhs[];
30extern _YACC_EXTERN_ Yshort yylen[];
31
32/* idx: current state; entry: non-zero if to reduce regardless of lookahead*/
33extern _YACC_EXTERN_ Yshort yydefred[];
34
35extern _YACC_EXTERN_ Yshort yydgoto[];
36
37/* idx: current state; entry: non-zero if shifting|reducing possible
38 in this state - in that case, yycheck[entry + lookahead] indicates
39 whether to perform the action for this lookahead. */
40extern _YACC_EXTERN_ Yshort yysindex[];
41extern _YACC_EXTERN_ Yshort yyrindex[];
42
43/* yycindex idx: current state; entry: non-zero if shift/reduce
44 conflicts for this state - in that case, yycheck[entry + lookahead]
45 indicates whether there's a conflict for this lookahead */
46extern _YACC_EXTERN_ Yshort yycindex[];
47extern _YACC_EXTERN_ Yshort yycheck[];
48
49extern _YACC_EXTERN_ Yshort yygindex[];
50extern _YACC_EXTERN_ Yshort yytable[];
51extern _YACC_EXTERN_ Yshort yyctable[];
52
53#if YYDEBUG
54/* idx: token code; entry: spelling of token */
55extern _YACC_EXTERN_ char *yyname[];
56
57extern _YACC_EXTERN_ char *yyrule[];
58#endif
59
60%% header
61
62/* YYPOSN is user-defined text position type. */
63#ifndef YYPOSN
64# define YYPOSN int
65#endif
66
67#ifdef YYREDUCEPOSNFUNC
68# define YYCALLREDUCEPOSN(e) \
69 if(reduce_posn) { \
70 YYREDUCEPOSNFUNC(yyps->pos, &(yyps->psp)[1-yym], &(yyps->vsp)[1-yym],\
71 yym, yyps->psp - yyps->ps, yychar, yyposn, e); \
72 reduce_posn = 0; \
73 }
74
75# ifndef YYCALLREDUCEPOSNARG
76# define YYCALLREDUCEPOSNARG (yyps->val)
77# endif
78
79
80# define YYPOSNARG(n) ((yyps->psp)[1-yym+(n)-1])
81# define YYPOSNOUT (yyps->pos)
82#endif
83
84/* If delete function is not defined by the user, do not delete. */
85#ifndef YYDELETEVAL
86# define YYDELETEVAL(v, t)
87#endif
88
89/* If delete function is not defined by the user, do not delete. */
90#ifndef YYDELETEPOSN
91# define YYDELETEPOSN(v, t)
92#endif
93
94#define YYEMPTY (-1)
95#define yyclearin (yychar=YYEMPTY)
96
97#define yyerrok (yyps->errflag=0)
98
99#ifndef YYSTACKGROWTH
100# define YYSTACKGROWTH 32
101#endif
102
103#ifndef YYDEFSTACKSIZE
104# define YYDEFSTACKSIZE 12
105#endif
106
107#ifdef YYDEBUG
108int yydebug = 0;
109#endif
110
111int yynerrs;
112
113/* These value/posn are taken from the lexer */
114YYSTYPE yylval;
115YYPOSN yyposn;
116
117/* These value/posn of the root non-terminal are returned to the caller */
118YYSTYPE yyretlval;
119YYPOSN yyretposn;
120
121#define YYABORT goto yyabort
122#define YYACCEPT goto yyaccept
123#define YYERROR goto yyerrlab
124#define YYVALID do { if (yyps->save) goto yyvalid; } while(0)
125#define YYVALID_NESTED do { if (yyps->save && \
126 yyps->save->save==0) goto yyvalid; } while(0)
127
128struct YYParseState_s {
129 struct YYParseState_s *save; /* Previously saved parser state */
130 int state;
131 int errflag;
132 Yshort *ssp; /* state stack pointer */
133 YYSTYPE *vsp; /* value stack pointer */
134 YYPOSN *psp; /* position stack pointer */
135 YYSTYPE val; /* value as returned by actions */
136 YYPOSN pos; /* position as returned by universal action */
137 Yshort *ss; /* state stack base */
138 YYSTYPE *vs; /* values stack base */
139 YYPOSN *ps; /* position stack base */
140 int lexeme; /* index of conflict lexeme in lexical queue */
141 unsigned int stacksize; /* current maximum stack size */
142 Yshort ctry; /* index in yyctable[] for this conflict */
143};
144typedef struct YYParseState_s YYParseState;
145
146/* Current parser state */
147static YYParseState *yyps = 0;
148
149/* yypath!=NULL: do the full parse, starting at *yypath parser state. */
150static YYParseState *yypath = 0;
151
152/* Base of the lexical value queue */
153static YYSTYPE *yylvals = 0;
154
155/* Current position at lexical value queue */
156static YYSTYPE *yylvp = 0;
157
158/* End position of lexical value queue */
159static YYSTYPE *yylve = 0;
160
161/* The last allocated position at the lexical value queue */
162static YYSTYPE *yylvlim = 0;
163
164/* Base of the lexical position queue */
165static YYPOSN *yylpsns = 0;
166
167/* Current position at lexical position queue */
168static YYPOSN *yylpp = 0;
169
170/* End position of lexical position queue */
171static YYPOSN *yylpe = 0;
172
173/* The last allocated position at the lexical position queue */
174static YYPOSN *yylplim = 0;
175
176/* Current position at lexical token queue */
177static Yshort *yylexp = 0;
178
179static Yshort *yylexemes = 0;
180
181/* For use in generated program */
182#define yytrial (yyps->save)
183#define yyvsp (yyps->vsp)
184#define yyval (yyps->val)
185#define yydepth (yyps->ssp - yyps->ss)
186
187
188/* Local prototypes. */
189int yyparse(void *YYPARSE_PARAM);
190
191static int yyLex1(yyscan_t scanner);
192static int yyExpand();
193static void yySCopy(YYSTYPE *to, YYSTYPE *from, int size);
194static void yyPCopy(YYPOSN *to, YYPOSN *from, int size);
195static void yyMoreStack(YYParseState *yyps);
196static YYParseState *yyNewState(int size);
197static void yyFreeState(YYParseState *p);
198
199
200%% body
201
202
203/* Parser function. Roughly looks like this:
204 int yyparse(void *YYPARSE_PARAM) {
205 yyloop:
206 if (reduce_possible) goto yyreduce;
207 read_token;
208 if (conflict) {
209 handle_conflict;
210 goto yyshift; or goto yyreduce;
211 }
212 if (shift_possible) {
213 yyshift:
214 do_shift;
215 goto yyloop;
216 }
217 if (reduce_possible) goto yyreduce;
218
219 (error handling);
220 goto yyloop;
221
222 yyreduce:
223 BIG_CHUNK_OF_RULE_ACTIONS;
224 goto yyloop;
225
226 (more error handling);
227 }*/
228int yyparse(void *YYPARSE_PARAM) {
229 int yym, yyn, yystate, yychar, yynewerrflag;
230 YYParseState *yyerrctx = 0;
231 int reduce_posn;
232
233# if YYDEBUG
234 char *yys;
235
236 if ((yys = getenv("YYDEBUG"))) {
237 yyn = *yys;
238 if (yyn >= '0' && yyn <= '9')
239 yydebug = yyn - '0';
240 }
241 if (yydebug) {
242 fputs("btyacc[<current state>,<nr of symbols on state stack>]\n",
243 stderr);
244 }
245# endif
246
247 yyps = yyNewState(YYDEFSTACKSIZE);
248 yyps->save = 0;
249 yynerrs = 0;
250 yyps->errflag = 0;
251 yychar = (YYEMPTY);
252
253 yyps->ssp = yyps->ss;
254 yyps->vsp = yyps->vs;
255 yyps->psp = yyps->ps;
256 *(yyps->ssp) = yystate = 0;
257
258
259 /* Main parsing loop */
260 yyloop:
261 if ((yyn = yydefred[yystate])) {
262 goto yyreduce;
263 }
264
265 /* Read one token */
266 if (yychar < 0) {
267 yychar = yyLex1(YYLEX_PARAM);
268 if (yychar < 0) yychar = 0;
269# if YYDEBUG
270 if (yydebug) {
271 yys = 0;
272 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
273 if (!yys) yys = "illegal-symbol";
274 fprintf(stderr, "btyacc[%3d,%2d%s]: read token %d (%s)",
275 yystate, yydepth, (yytrial ? ",trial" : ""), yychar, yys);
276# ifdef YYDBPR
277 fputc('<', stderr);
278 YYDBPR(yylval);
279 fputc('>', stderr);
280# endif
281 fputc('\n', stderr);
282 }
283# endif
284 }
285
286 /* Do we have a conflict? */
287 yyn = yycindex[yystate];
288 if (yyn != 0 && (yyn += yychar) >= 0
289 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) {
290 int ctry;
291
292 if (yypath) {
293 YYParseState *save;
294# if YYDEBUG
295 if (yydebug) {
296 fprintf(stderr, "btyacc[%3d,%2d%s]: CONFLICT: following successful "
297 "trial parse\n", yystate, yydepth, (yytrial ? ",trial" :""));
298 }
299# endif
300 /* Switch to the next conflict context */
301 save = yypath;
302 yypath = save->save;
303 ctry = save->ctry;
304 if (save->state != yystate)
305 goto yyabort;
306 yyFreeState(save);
307
308 } else {
309
310 /* Unresolved conflict - start/continue trial parse */
311 YYParseState *save;
312# if YYDEBUG
313 if (yydebug) {
314 fprintf(stderr, "btyacc[%3d,%2d%s]: CONFLICT. ",
315 yystate, yydepth, (yytrial ? ",trial" : ""));
316 if(yyps->save)
317 fputs("ALREADY in conflict, continuing trial parse.\n", stderr);
318 else
319 fputs("starting trial parse.\n", stderr);
320 }
321# endif
322 save = yyNewState(yyps->ssp - yyps->ss);
323 save->save = yyps->save;
324 save->state = yystate;
325 save->errflag = yyps->errflag;
326 save->ssp = save->ss + (yyps->ssp - yyps->ss);
327 save->vsp = save->vs + (yyps->vsp - yyps->vs);
328 save->psp = save->ps + (yyps->psp - yyps->ps);
329 memcpy(save->ss, yyps->ss, (yyps->ssp - yyps->ss + 1)*sizeof(Yshort));
330 yySCopy(save->vs, yyps->vs, (yyps->ssp - yyps->ss + 1));
331 yyPCopy(save->ps, yyps->ps, (yyps->ssp - yyps->ss + 1));
332 ctry = yytable[yyn];
333 if (yyctable[ctry] == -1) {
334# if YYDEBUG
335 if (yydebug && yychar >= 0)
336 fputs("btyacc[trial]: backtracking 1 token\n", stderr);
337# endif
338 ctry++;
339 }
340 save->ctry = ctry;
341 if (yyps->save == 0) {
342 /* If this is a first conflict in the stack, start saving lexemes */
343 if (!yylexemes) {
344# ifdef __cplusplus
345 yylexemes = new Yshort[YYSTACKGROWTH];
346 yylvals = new YYSTYPE[YYSTACKGROWTH];
347 yylvlim = yylvals + YYSTACKGROWTH;
348 yylpsns = new YYPOSN[YYSTACKGROWTH];
349 yylplim = yylpsns + YYSTACKGROWTH;
350# else
351 yylexemes = (Yshort*)malloc((YYSTACKGROWTH) * sizeof(Yshort));
352 yylvals = (YYSTYPE*)malloc((YYSTACKGROWTH) * sizeof(YYSTYPE));
353 yylvlim = yylvals + YYSTACKGROWTH;
354 yylpsns = (YYPOSN*)malloc((YYSTACKGROWTH) * sizeof(YYPOSN));
355 yylplim = yylpsns + YYSTACKGROWTH;
356# endif
357 }
358 if (yylvp == yylve) {
359 yylvp = yylve = yylvals;
360 yylpp = yylpe = yylpsns;
361 yylexp = yylexemes;
362 if (yychar >= 0) {
363 *yylve++ = yylval;
364 *yylpe++ = yyposn;
365 *yylexp = yychar;
366 yychar = YYEMPTY;
367 }
368 }
369 }
370 if (yychar >= 0) {
371 yylvp--, yylpp--, yylexp--;
372 yychar = YYEMPTY;
373 }
374 save->lexeme = yylvp - yylvals;
375 yyps->save = save;
376 }
377
378 if (yytable[yyn] == ctry) {
379# if YYDEBUG
380 if (yydebug)
381 fprintf(stderr, "btyacc[%3d,%2d%s]: shifting to state %d\n",
382 yystate, yydepth, (yytrial ? ",trial" : ""), yyctable[ctry]);
383# endif
384 if (yychar < 0) {
385 yylvp++; yylpp++; yylexp++;
386 }
387 yychar = YYEMPTY;
388 if (yyps->errflag > 0) --yyps->errflag;
389 yystate = yyctable[ctry];
390 goto yyshift;
391 } else {
392 yyn = yyctable[ctry];
393 goto yyreduce;
394 }
395 } /* End of code dealing with conflicts */
396
397
398 /* Is action a shift? */
399 yyn = yysindex[yystate];
400 if (yyn != 0 && (yyn += yychar) >= 0
401 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) {
402# if YYDEBUG
403 if (yydebug)
404 fprintf(stderr, "btyacc[%3d,%2d%s]: shifting to state %d\n",
405 yystate, yydepth, (yytrial ? ",trial" : ""), yytable[yyn]);
406# endif
407 yychar = YYEMPTY;
408 if (yyps->errflag > 0) --yyps->errflag;
409 yystate = yytable[yyn];
410
411 yyshift:
412 if (yyps->ssp >= yyps->ss + yyps->stacksize - 1) {
413 yyMoreStack(yyps);
414 }
415 *++(yyps->ssp) = yystate;
416 *++(yyps->vsp) = yylval;
417 *++(yyps->psp) = yyposn;
418 goto yyloop;
419 }
420 if ((yyn = yyrindex[yystate]) &&
421 (yyn += yychar) >= 0 &&
422 yyn <= YYTABLESIZE &&
423 yycheck[yyn] == yychar) {
424 yyn = yytable[yyn];
425 goto yyreduce;
426 }
427
428 /* According to the tables, neither shift nor reduce is OK here - error! */
429 if (yyps->errflag) goto yyinrecovery;
430 yynewerrflag = 1;
431 goto yyerrhandler;
432yyerrlab:
433 yynewerrflag = 0;
434yyerrhandler:
435 while (yyps->save) {
436 int ctry;
437 YYParseState *save = yyps->save;
438# if YYDEBUG
439 if (yydebug)
440 fprintf(stderr, "btyacc[%3d,%2d%s]: ERROR, "
441 "CONFLICT BACKTRACKING to state %d, %d tokens\n",
442 yystate, yydepth, (yytrial ? ",trial" : ""),
443 yyps->save->state, yylvp - yylvals - yyps->save->lexeme);
444# endif
445 /* Memorize most forward-looking error state in case
446 it's really an error. */
447 if(yyerrctx==0 || yyerrctx->lexeme<yylvp-yylvals) {
448 /* Free old saved error context state */
449 if(yyerrctx) yyFreeState(yyerrctx);
450 /* Create and fill out new saved error context state */
451 yyerrctx = yyNewState(yyps->ssp - yyps->ss);
452 yyerrctx->save = yyps->save;
453 yyerrctx->state = yystate;
454 yyerrctx->errflag = yyps->errflag;
455 yyerrctx->ssp = yyerrctx->ss + (yyps->ssp - yyps->ss);
456 yyerrctx->vsp = yyerrctx->vs + (yyps->vsp - yyps->vs);
457 yyerrctx->psp = yyerrctx->ps + (yyps->psp - yyps->ps);
458 memcpy (yyerrctx->ss, yyps->ss,
459 (yyps->ssp - yyps->ss + 1) * sizeof(Yshort));
460 yySCopy(yyerrctx->vs, yyps->vs, (yyps->ssp - yyps->ss + 1));
461 yyPCopy(yyerrctx->ps, yyps->ps, (yyps->ssp - yyps->ss + 1));
462 yyerrctx->lexeme = yylvp - yylvals;
463 }
464 yylvp = yylvals + save->lexeme;
465 yylpp = yylpsns + save->lexeme;
466 yylexp = yylexemes + save->lexeme;
467 yychar = YYEMPTY;
468 yyps->ssp = yyps->ss + (save->ssp - save->ss);
469 yyps->vsp = yyps->vs + (save->vsp - save->vs);
470 yyps->psp = yyps->ps + (save->psp - save->ps);
471 memcpy (yyps->ss, save->ss, (yyps->ssp - yyps->ss + 1) * sizeof(Yshort));
472 yySCopy(yyps->vs, save->vs, yyps->vsp - yyps->vs + 1);
473 yyPCopy(yyps->ps, save->ps, yyps->psp - yyps->ps + 1);
474 ctry = ++save->ctry;
475 yystate = save->state;
476 /* We tried shift, try reduce now */
477 if ((yyn = yyctable[ctry]) >= 0) {
478 goto yyreduce;
479 }
480 yyps->save = save->save;
481 yyFreeState(save);
482
483 /* Nothing left on the stack -- error */
484 if (!yyps->save) {
485# if YYDEBUG
486 if (yydebug) {
487 fputs("btyacc[trial]: trial parse FAILED, entering ERROR mode\n",
488 stderr);
489 }
490# endif
491 /* Restore state as it was in the most forward-advanced error */
492 yylvp = yylvals + yyerrctx->lexeme;
493 yylpp = yylpsns + yyerrctx->lexeme;
494 yylexp = yylexemes + yyerrctx->lexeme;
495 yychar = yylexp[-1];
496 yylval = yylvp[-1];
497 yyposn = yylpp[-1];
498 yyps->ssp = yyps->ss + (yyerrctx->ssp - yyerrctx->ss);
499 yyps->vsp = yyps->vs + (yyerrctx->vsp - yyerrctx->vs);
500 yyps->psp = yyps->ps + (yyerrctx->psp - yyerrctx->ps);
501 memcpy (yyps->ss, yyerrctx->ss,
502 (yyps->ssp - yyps->ss + 1) * sizeof(Yshort));
503 yySCopy(yyps->vs, yyerrctx->vs, yyps->vsp - yyps->vs + 1);
504 yyPCopy(yyps->ps, yyerrctx->ps, yyps->psp - yyps->ps + 1);
505 yystate = yyerrctx->state;
506 yyFreeState(yyerrctx);
507 yyerrctx = 0;
508 }
509 yynewerrflag = 1;
510 }
511 if (yynewerrflag) {
512# ifdef YYERROR_DETAILED
513 yyerror_detailed("parse error", yychar, yylval, yyposn);
514# else
515 yyerror("parse error");
516# endif
517 }
518 ++yynerrs;
519 yyinrecovery:
520 if (yyps->errflag < 3) {
521 yyps->errflag = 3;
522 for (;;) {
523 if ((yyn = yysindex[*(yyps->ssp)])
524 && (yyn += YYERRCODE) >= 0
525 && yyn <= YYTABLESIZE
526 && yycheck[yyn] == YYERRCODE) {
527# if YYDEBUG
528 if (yydebug)
529 fprintf(stderr, "btyacc[%3d,%2d%s]: ERROR recovery shifts to "
530 "state %d\n",
531 *(yyps->ssp), yydepth, (yytrial ? ",trial" : ""),
532 yytable[yyn]);
533# endif
534 /* Use label yyerrlab, so that compiler does not warn */
535 if(yyps->errflag != yyps->errflag) goto yyerrlab;
536 yystate = yytable[yyn];
537 goto yyshift;
538 } else {
539# if YYDEBUG
540 if (yydebug)
541 fprintf(stderr,
542 "btyacc[%3d,%2d%s]: ERROR recovery discards this state\n",
543 *(yyps->ssp), yydepth, (yytrial ? ",trial" : ""));
544# endif
545 if (yyps->ssp <= yyps->ss) {
546 goto yyabort;
547 }
548 if(!yytrial) {
549 YYDELETEVAL(yyps->vsp[0],1);
550 YYDELETEPOSN(yyps->psp[0],1);
551 }
552 --(yyps->ssp);
553 --(yyps->vsp);
554 --(yyps->psp);
555 }
556 }
557 } else {
558 if (yychar == 0) goto yyabort;
559# if YYDEBUG
560 if (yydebug) {
561 yys = 0;
562 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
563 if (!yys) yys = "illegal-symbol";
564 fprintf(stderr, "btyacc[%3d,%2d%s]: ERROR recovery discards token "
565 "%d (%s)\n",
566 yystate, yydepth, (yytrial ? ",trial" : ""), yychar, yys);
567 }
568# endif
569 if(!yytrial) {
570 YYDELETEVAL(yylval,0);
571 YYDELETEPOSN(yyposn,0);
572 }
573 yychar = (YYEMPTY);
574 goto yyloop;
575 }
576
577 /* Reduce the rule */
578yyreduce:
579 yym = yylen[yyn];
580# if YYDEBUG
581 if (yydebug) {
582 fprintf(stderr, "btyacc[%3d,%2d%s]: reducing by rule %d (%s)",
583 yystate, yydepth, (yytrial ? ",trial" : ""), yyn, yyrule[yyn]);
584# ifdef YYDBPR
585 if (yym) {
586 int i;
587 fputc('<', stderr);
588 for (i=yym; i>0; i--) {
589 if (i!=yym) printf(", ");
590 YYDBPR((yyps->vsp)[1-i]);
591 }
592 fputc('>', stderr);
593 }
594# endif
595 fputc('\n', stderr);
596 }
597# endif
598 if (yyps->ssp + 1 - yym >= yyps->ss + yyps->stacksize) {
599 yyMoreStack(yyps);
600 }
601
602# ifdef _YACC_DEFAULT_ACTION_
603 /* "$$ = NULL" default action */
604 memset(&yyps->val, 0, sizeof(yyps->val));
605# else
606 /* RA: bison compatibility: default action is '$$ = $1;' */
607 if (yym > 0) yyps->val = (yyps->vsp)[1 - yym];
608# endif
609
610 /* Default reduced position is NULL -- no position at all. No
611 position will be assigned at trial time and if no position
612 handling is present */
613 memset(&yyps->pos, 0, sizeof(yyps->pos));
614
615 reduce_posn = 1;
616
617 switch (yyn) {
618
619%% trailer
620
621 default:
622 break;
623 }
624
625# if YYDEBUG && defined(YYDBPR)
626 if (yydebug) {
627 fputs("btyacc[trial]: reduced, result is ", stderr);
628 YYDBPR(yyps->val);
629 fputc('\n', stderr);
630 }
631# endif
632
633 /* Perform user-defined position reduction */
634# ifdef YYREDUCEPOSNFUNC
635 if(!yytrial) {
636 YYCALLREDUCEPOSN(YYREDUCEPOSNFUNCARG);
637 }
638# endif
639
640 yyps->ssp -= yym;
641 yystate = *(yyps->ssp);
642 yyps->vsp -= yym;
643 yyps->psp -= yym;
644
645 yym = yylhs[yyn];
646 if (yystate == 0 && yym == 0) {
647# if YYDEBUG
648 if (yydebug) {
649 fprintf(stderr,
650 "btyacc[ 0,%2d%s]: reduced, shifting to final state %d\n",
651 yydepth, (yytrial ? ",trial" : ""), YYFINAL);
652 }
653# endif
654 yystate = YYFINAL;
655 *++(yyps->ssp) = YYFINAL;
656 *++(yyps->vsp) = yyps->val;
657 yyretlval = yyps->val; /* return value of root non-terminal to yylval */
658 *++(yyps->psp) = yyps->pos;
659 yyretposn = yyps->pos; /* return value of root position to yyposn */
660 if (yychar < 0) {
661 if ((yychar = yyLex1(YYLEX_PARAM)) < 0) {
662 yychar = 0;
663 }
664# if YYDEBUG
665 if (yydebug) {
666 yys = 0;
667 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
668 if (!yys) yys = "illegal-symbol";
669 fprintf(stderr, "btyacc[%3d,%2d%s]: read %d (%s)\n",
670 YYFINAL, yydepth, (yytrial ? ",trial" : ""), yychar, yys);
671 }
672# endif
673 }
674 if (yychar == 0) goto yyaccept;
675 goto yyloop;
676 }
677
678 if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
679 yyn <= YYTABLESIZE && yycheck[yyn] == yystate) {
680 yystate = yytable[yyn];
681 } else {
682 yystate = yydgoto[yym];
683 }
684# if YYDEBUG
685 if (yydebug)
686 fprintf(stderr, "btyacc[%3d,%2d%s]: reduced, shifting to state %d\n",
687 *(yyps->ssp), yydepth, (yytrial ? ",trial" : ""), yystate);
688# endif
689 if (yyps->ssp >= yyps->ss + yyps->stacksize - 1) {
690 yyMoreStack(yyps);
691 }
692 *++(yyps->ssp) = yystate;
693 *++(yyps->vsp) = yyps->val;
694 *++(yyps->psp) = yyps->pos;
695 goto yyloop;
696
697
698 /* Reduction declares that this path is valid.
699 Set yypath and do a full parse */
700yyvalid:
701 if (yypath) {
702 goto yyabort;
703 }
704 while (yyps->save) {
705 YYParseState *save = yyps->save;
706 yyps->save = save->save;
707 save->save = yypath;
708 yypath = save;
709 }
710# if YYDEBUG
711 if (yydebug)
712 fprintf(stderr, "btyacc[%3d,%2d%s]: CONFLICT trial successful, "
713 "backtracking to state %d, %d tokens\n",
714 yystate, yydepth, (yytrial ? ",trial" : ""),
715 yypath->state, yylvp - yylvals - yypath->lexeme);
716# endif
717 if(yyerrctx) {
718 yyFreeState(yyerrctx); yyerrctx = 0;
719 }
720 yychar = YYEMPTY;
721 yyps->ssp = yyps->ss + (yypath->ssp - yypath->ss);
722 yyps->vsp = yyps->vs + (yypath->vsp - yypath->vs);
723 yyps->psp = yyps->ps + (yypath->psp - yypath->ps);
724 memcpy (yyps->ss, yypath->ss, (yyps->ssp - yyps->ss + 1) * sizeof(Yshort));
725 yySCopy(yyps->vs, yypath->vs, yyps->vsp - yyps->vs + 1);
726 yyPCopy(yyps->ps, yypath->ps, yyps->psp - yyps->ps + 1);
727 yylvp = yylvals + yypath->lexeme;
728 yylpp = yylpsns + yypath->lexeme;
729 yylexp = yylexemes + yypath->lexeme;
730 yystate = yypath->state;
731 goto yyloop;
732
733
734yyabort:
735 {
736 YYSTYPE *pv;
737 YYPOSN *pp;
738
739 if(yyerrctx) {
740 yyFreeState(yyerrctx); yyerrctx = 0;
741 }
742
743 for(pv=yyps->vs; pv<yyps->vsp; pv++) {
744 YYDELETEVAL(*pv,2);
745 }
746
747 for(pp=yyps->ps; pp<yyps->psp; pp++) {
748 YYDELETEPOSN(*pp,2);
749 }
750
751 while (yyps) {
752 YYParseState *save = yyps;
753 yyps = save->save;
754 yyFreeState(save);
755 }
756 while (yypath) {
757 YYParseState *save = yypath;
758 yypath = save->save;
759 yyFreeState(save);
760 }
761 return (1);
762 }
763
764yyaccept:
765 if (yyps->save) goto yyvalid;
766 if(yyerrctx) {
767 yyFreeState(yyerrctx); yyerrctx = 0;
768 }
769 while (yyps) {
770 YYParseState *save = yyps;
771 yyps = save->save;
772 yyFreeState(save);
773 }
774 while (yypath) {
775 YYParseState *save = yypath;
776 yypath = save->save;
777 yyFreeState(save);
778 }
779 return (0);
780}
781
782
783/* Call yylex() unless the token has already been read. */
784static int yyLex1(yyscan_t scanner) {
785 if(yylvp < yylve) {
786 /* we're currently re-reading tokens */
787 yylval = *yylvp++;
788 yyposn = *yylpp++;
789 return *yylexp++;
790 } else if(yyps->save) {
791 /* in trial mode; save scanner results for future parse attempts */
792 if(yylvp == yylvlim)
793 yyExpand();
794 *yylexp = yylex(&yylval, scanner);
795 *yylvp++ = yylval;
796 yylve++;
797 *yylpp++ = yyposn;
798 yylpe++;
799 return *yylexp++;
800 } else { /* normal operation, no conflict encountered */
801 return yylex(&yylval, scanner);
802 }
803}
804
805/* Enlarge lexical value queue */
806static int yyExpand() {
807 int p = yylvp - yylvals;
808 int s = yylvlim - yylvals;
809 s += YYSTACKGROWTH;
810 {
811 Yshort *tl = yylexemes;
812 YYSTYPE *tv = yylvals;
813 YYPOSN *tp = yylpsns;
814# ifdef __cplusplus
815 yylvals = new YYSTYPE[s];
816 yylpsns = new YYPOSN[s];
817 yylexemes = new Yshort[s];
818 memcpy(yylexemes, tl, (s-YYSTACKGROWTH)*sizeof(Yshort));
819 yySCopy(yylvals, tv, s-YYSTACKGROWTH);
820 yyPCopy(yylpsns, tp, s-YYSTACKGROWTH);
821 delete[] tl;
822 delete[] tv;
823 delete[] tp;
824# else
825 yylvals = (YYSTYPE*)malloc(s * sizeof(YYSTYPE));
826 yylpsns = (YYPOSN*)malloc(s * sizeof(YYPOSN));
827 yylexemes = (Yshort*)malloc(s * sizeof(Yshort));
828 memcpy(yylexemes, tl, (s - YYSTACKGROWTH)*sizeof(Yshort));
829 yySCopy(yylvals, tv, s - YYSTACKGROWTH);
830 yyPCopy(yylpsns, tp, s - YYSTACKGROWTH);
831 free(tl);
832 free(tv);
833 free(tp);
834# endif
835 }
836 yylvp = yylve = yylvals + p;
837 yylvlim = yylvals + s;
838 yylpp = yylpe = yylpsns + p;
839 yylplim = yylpsns + s;
840 yylexp = yylexemes + p;
841 return 0;
842}
843
844static void yySCopy(YYSTYPE *to, YYSTYPE *from, int size) {
845 int i;
846 for (i = size-1; i >= 0; i--) {
847 to[i] = from[i];
848 }
849}
850
851static void yyPCopy(YYPOSN *to, YYPOSN *from, int size) {
852 int i;
853 for (i = size-1; i >= 0; i--) {
854 to[i] = from[i];
855 }
856}
857
858static void yyMoreStack(YYParseState *yyps) {
859 int p = yyps->ssp - yyps->ss;
860 Yshort *tss = yyps->ss;
861 YYSTYPE *tvs = yyps->vs;
862 YYPOSN *tps = yyps->ps;
863 int newSize = yyps->stacksize + YYSTACKGROWTH;
864
865# ifdef __cplusplus
866 yyps->ss = new Yshort [newSize];
867 yyps->vs = new YYSTYPE[newSize];
868 yyps->ps = new YYPOSN [newSize];
869 memcpy(yyps->ss, tss, yyps->stacksize * sizeof(Yshort));
870 yySCopy(yyps->vs, tvs, yyps->stacksize);
871 yyPCopy(yyps->ps, tps, yyps->stacksize);
872 yyps->stacksize += YYSTACKGROWTH;
873 delete[] tss;
874 delete[] tvs;
875 delete[] tps;
876# else
877 yyps->ss = (Yshort*) malloc(newSize * sizeof(Yshort));
878 yyps->vs = (YYSTYPE*)malloc(newSize * sizeof(YYSTYPE));
879 yyps->ps = (YYPOSN*) malloc(newSize * sizeof(YYPOSN));
880 memcpy(yyps->ss, tss, yyps->stacksize * sizeof(Yshort));
881 yySCopy(yyps->vs, tvs, yyps->stacksize);
882 yyPCopy(yyps->ps, tps, yyps->stacksize);
883 yyps->stacksize += YYSTACKGROWTH;
884 free(tss);
885 free(tvs);
886 free(tps);
887# endif
888 yyps->ssp = yyps->ss + p;
889 yyps->vsp = yyps->vs + p;
890 yyps->psp = yyps->ps + p;
891# if YYDEBUG
892 if (yydebug)
893 fprintf(stderr, "btyacc: stack size increased to %d\n", yyps->stacksize);
894# endif
895}
896
897
898#ifdef __cplusplus
899
900static YYParseState *yyNewState(int size) {
901 YYParseState *p = new YYParseState;
902 p->stacksize = size+4;
903 p->ss = new Yshort [size + 4];
904 p->vs = new YYSTYPE[size + 4];
905 p->ps = new YYPOSN [size + 4];
906 memset(&p->vs[0], 0, (size+4)*sizeof(YYSTYPE));
907 memset(&p->ps[0], 0, (size+4)*sizeof(YYPOSN));
908 return p;
909}
910
911static void yyFreeState(YYParseState *p) {
912 delete[] p->ss;
913 delete[] p->vs;
914 delete[] p->ps;
915 delete p;
916}
917
918#else /* not __cplusplus */
919
920static YYParseState *yyNewState(int size) {
921 YYParseState *p = (YYParseState*)malloc(sizeof(YYParseState));
922
923 p->stacksize = size+4;
924 p->ss = (Yshort*) malloc((size + 4) * sizeof(Yshort));
925 p->vs = (YYSTYPE*)malloc((size + 4) * sizeof(YYSTYPE));
926 p->ps = (YYPOSN*) malloc((size + 4) * sizeof(YYPOSN));
927 memset(&p->vs[0], 0, (size+4)*sizeof(YYSTYPE));
928 memset(&p->ps[0], 0, (size+4)*sizeof(YYPOSN));
929 return p;
930}
931
932static void yyFreeState(YYParseState *p) {
933 free(p->ss);
934 free(p->vs);
935 free(p->ps);
936 free(p);
937}
938
939#endif